网页下拉菜单的默认值

发布于 2024-10-11 00:54:44 字数 659 浏览 2 评论 0原文

这可能解释起来很复杂。我建立了一个时间表系统,员工可以将他们自己的时间表记录插入到 mysql 数据库中。这工作正常,但我给了他们编辑他们插入的项目的选项,所以我有一个下拉列表,从 mysql 显示所有可供他们选择的项目编号,但我也想将下拉列表的默认值设置为他们在插入记录时选择的项目编号(即,为了避免记住它是什么项目),

这是我的代码:

$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>';

该记录存储在名为时间表的表中,因此我可以从中进行选择,但我'我不确定如何在上面的下拉列表中将正确的项目编号设置为默认值?

这有道理吗?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

心安伴我暖 2024-10-18 00:54:44

将项目编号存储在变量中,例如$projnum,并将其与每个值进行比较。

$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>';

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>';
玩物 2024-10-18 00:54:44
    $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>";
    }
    $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>";
    }
白芷 2024-10-18 00:54:44

更改

selected='yes'

selected='selected'

Change

selected='yes'

To

selected='selected'
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文