将动态HTML选项的第一个值设置为空

时间:2014-05-09 03:11:14

标签: php

<select id="section" name="section">
    <?php
    include("Nethost.php");
    $section = "";
    $yr = "";
    $sql = mysql_query("SELECT DISTINCT * FROM section ORDER BY yrlvl, section");
    while ($row = mysql_fetch_array($sql)){
        $section = $row['section'];
        $yr = $row['yrlvl'];
        ?>
        <option value="">Select</option>
        <option <?php $result2 = mysql_query("SELECT section FROM student WHERE idnumber = '$idnumber'");
        if(mysql_num_rows($result2) > 0) { ?>
            selected="selected" <?php } ?> value="<?php print $section; ?>"><?php print $yr; ?> - <?php print $section; ?></option>
    <?php } ?>
</select>

以上是select选项的php代码。填充了数据库表中的数据,如何将第一个值设置为空。我尝试添加Select,但这是结果enter image description here

选项选择不断重复。怎么办?

2 个答案:

答案 0 :(得分:2)

试试这个

<select id="section" name="section">
    <?php
    include("Nethost.php");
    $section = "";
    $yr = "";
    $sql = mysql_query("SELECT DISTINCT * FROM section ORDER BY yrlvl, section");
    ?>
    <option value="">Select</option>
    <?php
    while ($row = mysql_fetch_array($sql)){
        $section = $row['section'];
        $yr = $row['yrlvl'];
        ?>

        <option <?php $result2 = mysql_query("SELECT section FROM student WHERE idnumber = '$idnumber'");
        if(mysql_num_rows($result2) > 0) { ?>
            selected="selected" <?php } ?> value="<?php print $section; ?>"><?php print $yr; ?> - <?php print $section; ?></option>
    <?php } ?>
</select>

答案 1 :(得分:1)

只需移动该选项行&#34; 选择&#34;你的PHP代码:

<select id="section" name="section">
    <option value="">Select</option>
    <?php
    ...your php code
    ?>
</select>