从数据库中提取数据时创建下拉列表

时间:2014-04-18 22:56:19

标签: php mysql sql database drop-down-menu

您好我想在下拉列表中显示我从数据库中提取的类别。目前它们被拉动并且只是在我创建的菜单栏中显示在一条长行中。我到目前为止的代码是:

    <?php
    /*
    Displaying List of Categories from the Table - Category and that is limited to 6
    */
    $qry=mysql_query("SELECT * FROM category ", $con);
    if(!$qry)
    {
    die("Query Failed: ". mysql_error());
    }

    /* Fetching datas from the field "category" and article id is transfered to articles.php file */
    while($row=mysql_fetch_array($qry))
    {
    echo "&nbsp;<a href=articles.php?cat=".$row['category'].">".$row['category']."</a>

    &nbsp;&nbsp;&nbsp;&nbsp;";
    }
    ?>

任何帮助将不胜感激 感谢

1 个答案:

答案 0 :(得分:1)

将您的while语句更改为:

echo '<select name="list">';
while($row=mysql_fetch_array($qry))
{
    echo '<option>'.$row['category'].'</option>';
}
echo '</select>';