2013-03-02 50 views
0

我有一個運行MySQL查詢並返回一些數據的PHP腳本。我想要做的是用下拉菜單控制返回的結果。添加列表選擇器到mysql查詢

下拉菜單中需要有以下信息供學生選擇。

SELECT courseid FROM course_students其中userid = $用戶

下面是當前的代碼,我.....我想弄清楚如何將上述用它:

<?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> 

回答

0

我相信你需要像這樣

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

祝您好運!


在查詢中還選擇表ID,以便用它作爲「選項」

+0

我正在將這一代碼,但我有一些麻煩,使用它的價值。 – 2013-03-02 19:19:54

+0

我更新了我認爲代碼應該喜歡的問題,它可能很粗糙,因爲我沒有編輯在PHP編輯器....我得到這個錯誤解析錯誤:語法錯誤,意外的'<'在/ home/emscompl /public_html/modules/mod_clinicalschedule/mod_clinicalschedule.php on line 8 – 2013-03-02 19:32:07

+0

我已經清除了php錯誤,但是下拉並沒有填充給定的代碼... – 2013-03-03 17:12:18