2011-12-01 86 views
2

我一直在试图把计数查询放入下拉列表中,因为我正在创建评分类型系统,并基于数据库中的行数,我想返回一个刻度例如1-5使用MySQL数据并放入一个下拉列表

<?php 
// Ruth Gammack 

session_start(); 
$page = 'index.php'; 
mysql_connect('localhost', 'root', '') or die(mysql_error()); 
mysql_select_db('group4') or die(mysql_error()); 
//echo "Done"; 


    require_once "./includes/choices.inc.php"; 
    if(isset($_GET["action"])){ 
     switch($_GET["action"]) { 
      case "add": 
       addmodule(); 
       break; 

      case "remove": 
       removemodule(); 
       break; 

      case "empty": 
       emptychoice(); 
       break; 
     } 
     reviewChoices(); 
     // close database connection 
     dbClose($conn); 
    } 



function modules(){ 
    $get = mysql_query('SELECT * FROM module'); 
    if (mysql_num_rows($get) == 0){ 
     echo '<p>There are no Modules available at this time please check back later</p>'; 
    } 

    else { 

     while ($get_row = mysql_fetch_assoc($get)) { 
     echo '<p>'.$get_row['moduleID'].'<br />'.$get_row['ModuleName'].'<br />'.$get_row['ModuleDesc'].'<br />'.$get_row['LecturerID'].'</p>'; 
     // printing the list box select command 
     $query_disp=('SELECT moduleID, COUNT(*) FROM module'); 
     $result_disp = mysql_query($query_disp); 
     echo "<select name=”selRating”>"; 

     while($row = mysql_fetch_array($result_disp)) 
     { 
     echo "<option value='" . $row['moduleID'] . "'>" . $row['moduleID'] . "</option>"; 
     /* Option values are added by looping through the array */ 
     } 
     echo "</select>";// Closing of list box 

     } 

    } 

} 

?> 

我已经尝试了许多不同的方法,但我已经用尽了想法。

回答

0

也许这样(未测试)?

$query_disp=('SELECT moduleID, ModuleName, ModuleDesc, LecturerID, COUNT(*) AS cnt FROM module GROUP BY moduleID'); 
$select = "<select name=”selRating”>"; 
while($row = mysql_fetch_array($query_disp)) 
{ 
    echo '<p>'.$get_row['moduleID'].'<br />'.$get_row['ModuleName'].'<br />'.$get_row['ModuleDesc'].'<br />'.$get_row['LecturerID'].'</p>'; 
    $select += "<option value='" . $row['moduleID'] . "'>" . $row['ModuleName'] . " " . $row['cnt'] . "</option>"; 
} 
$select += '</select>';  

echo $select; 
相关问题