开发者

default value of dropdown on webpage

This may be complicated to explain. I have set up a timesheet system where employees can insert into a mysql db their own timesheet records. this works fine but i've given them the option to edit an item they have inserted, so what i have is a dropdown displaying from mysql开发者_StackOverflow社区 all project numbers available for them to pick but i also want to set the default value of the dropdown to the project number which they selected when they inserted the record (ie. to avoid having to remember what project it was)

here is my code:

$result1 = mysql_query("select project_no from `projects`") or die(mysql_error());
            echo '<select name="project" class="project">';
        while($row1 = mysql_fetch_array($result1)){
            echo "<option selected='yes' value=".$row1['project_no'].">".$row1['project_no']."</option>";
        }
            echo '</select>';

The record is stored in a table called timesheets, so i can do a SELECT from that but i'm not sure how to set the right project number as the default value in the above dropdown?

Does that make sense?


Store the project number in a variable, e.g., $projnum, and compare it with each value.

$result1 = mysql_query("select project_no from `projects`") or die(mysql_error());
            echo '<select name="project" class="project">';
        while($row1 = mysql_fetch_array($result1)){
            echo "<option ";
            if ($row1['project_no'] == $projnum) { echo 'selected="selected"; }
            echo " value=".$row1['project_no'].">".$row1['project_no']."</option>";
        }
            echo '</select>';


    $project_to_select = 42;

    while($row1 = mysql_fetch_array($result1)){
        echo "<option " . ($row1['project_no'] == $project_to_select ? "selected='selected'" : "") . " value=".$row1['project_no'].">".$row1['project_no']."</option>";
    }


Change

selected='yes'

To

selected='selected'
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜