将列表选择器添加到mysql查询

时间:2013-03-02 03:13:42

标签: php html mysql sorting

我有一个运行MySQL查询并返回一些数据的php脚本。我想要做的是通过下拉列表控制返回的结果。

下拉列表需要在其中包含以下信息供学生选择。

SELECT courseid FROM course_students WHERE userid = $ user

以下是我当前的代码.....我试图弄清楚如何将上述内容与之合并:

    <?php
defined('_JEXEC') or die;

$db = JFactory::getDBO();
$user = & JFactory::getUser();
$userid = $user->get('id');
$course = "SELECT 
  c.coursename
FROM
  courses AS `c`  
  LEFT OUTER JOIN course_students AS s 
    ON s.courseid = c.id 
WHERE s.userid = '1285'";
$courses= mysql_fetch_array($course);
?>
Please Select Course for Results:
<select> 
<?php foreach($courses as $c){ 
   echo "<option value='$c'>".$c."</option>";
} ?>
</select>
<?php
$sql =
    "SELECT 
  as.name,
  DATE_FORMAT(s.starttime, '%b %d, %Y %H:%i') AS TIME,
  s.totalhours AS hrs 
FROM
  clinicalsites AS `as` 
  LEFT OUTER JOIN clinical_dashboard AS s 
    ON s.clinicalsite = as.id 
WHERE s.userid = $userid 
  AND starttime > NOW()
AND courseid = $c";

$db->setQuery($sql);

$rows = $db->loadObjectList();
?>
<style>
    table, td, th
    {
        border-bottom:1px solid black;
        text-align:center;
    }
    th {
        background-color:#000000;
        color: #FFF;
    }


    .upcoming {
        font-weight: bold;
    }
    .complete {
        font-weight: bold;
        color: #060;
    }
    .pending {
        font-weight: bold;
        color: #F00;
    }
    .approved {
        color: #C60;
    }
</style>

<table width="396">
    <tr>
        <th width="162">Type of Hours</th>
        <th width="70">Pending</th>
        <th width="74">Approved</th> 
        <th width="70">Complete</th>
    </tr>
</table>
<table>

    <tr>
        <td width="162">Total Hospital Hours</td>
        <td class="pending" width="70"><?php
            $q2 = "SELECT 
  SUM(totalhours) AS sum2 
FROM
  clinical_dashboard 
WHERE userid = $userid 
  AND clinicaltype = 'Hospital' 
  AND instructorapproval = '1'
AND courseid = $c";
            $result2 = mysql_query($q2);
            $row2 = mysql_fetch_assoc($result2);

            echo $row2['sum2']
            ?></td>
        <td class="approved" width="74">
            <?php
            $q3 = "SELECT 
  SUM(totalhours) AS sum3 
FROM
  clinical_dashboard 
WHERE userid = $userid 
  AND clinicaltype = 'Hospital' 
  AND instructorapproval = '2'
AND courseid = $c";
            $result = mysql_query($q);
            $row3 = mysql_fetch_assoc($result);

            echo $row3['sum2']
            ?>
        </td>
        <td class="complete" width="70">
            <?php
            $q4 = "SELECT 
  SUM(totalhours) AS sum4 
FROM
  clinical_dashboard 
WHERE userid = $userid 
  AND clinicaltype = 'Hospital' 
  AND instructorapproval = '2' 
  AND STATUS = '2'
AND courseid = $c";
            $result = mysql_query($q4);
            $row4 = mysql_fetch_assoc($result);

            echo $row4['sum4']
            ?>
        </td>
    </tr>
    <tr>
        <td>Total Pre-Hosptial Hours</td>
        <td class="pending" ><?php
            $q = "SELECT 
  SUM(totalhours) AS sum3 
FROM
  clinical_dashboard 
WHERE userid = $userid 
  AND clinicaltype = 'Pre-Hospital' 
  AND instructorapproval = '1'
AND courseid = $c";
            ;
            $result = mysql_query($q);
            $row1 = mysql_fetch_assoc($result);
            echo $row1['sum3']
            ?></td>
        <td class="approved">
            <?php
            $q = "SELECT 
  SUM(totalhours) AS sum3 
FROM
  clinical_dashboard 
WHERE userid = $userid 
  AND clinicaltype = 'Pre-Hospital' 
  AND instructorapproval = '2'
AND courseid = $c";
            $result = mysql_query($q);
            $row1 = mysql_fetch_assoc($result);
            echo $row1['sum3']
            ?></td>
        <td class="complete"><?php
            $q = "SELECT 
  SUM(totalhours) AS sum3 
FROM
  clinical_dashboard 
WHERE userid = $userid 
  AND clinicaltype = 'Pre-Hospital' 
  AND instructorapproval = '2' 
  AND STATUS = '2'
AND courseid = $c";
            $result = mysql_query($q);
            $row1 = mysql_fetch_assoc($result);
            echo $row1['sum3']
            ?></td>
    </tr>
    <?PHP foreach ($rows as $row): ?>
    <?php endforeach ?>
</table>
<p class="upcoming">Upcoming Clinical Schedule</p>
<table width="393" border="1">
    <tr>
        <th width="166">Clinical Site</th>
        <th width="119">Date</th>
        <th width="86">Total Hours</th>
    </tr>
</table>
<table>
    <?PHP foreach ($rows as $row): ?>
        <tr>
            <td width="170"><?php echo $row->name ?></td>
            <td width="124"><?php echo $row->time ?></td>
            <td width="84"><?php echo $row->hrs ?></td>
        </tr>
    <?php endforeach ?>
</table>
<p class="upcoming"></p>

以下是使用select的下拉查询。

$course = "SELECT 
  c.coursename
FROM
  courses AS `c`  
  LEFT OUTER JOIN course_students AS s 
    ON s.courseid = c.id 
WHERE s.userid = '1285'";
$courses= mysql_fetch_array($course);
?>
Please Select Course for Results:
<select> 
<?php foreach($courses as $c){ 
   echo "<option value='$c'>".$c."</option>";
} ?>
</select>

1 个答案:

答案 0 :(得分:0)

我相信你需要这样的东西

<select> 
<? foreach($courses as $c){ 
   echo "<option value=".$c['table_id'].">".$c['coursename']."</option>";
} ?>
</select>

祝你好运!


同时在查询中选择表ID,以便将其用作“选项”

的值