使用mysql数据并放入下拉列表

时间:2011-12-01 22:36:40

标签: php mysql drop-down-menu

我一直在尝试将计数查询放入下拉列表,因为我正在创建一个评级类型系统,并根据数据库中的行数我想返回一个比例例如1-5

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

        }

    }

}

?>

我尝试了很多不同的方法,但我已经没有想法了。

1 个答案:

答案 0 :(得分: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;