【问题标题】:How to create an `All` option in drop down menu如何在下拉菜单中创建“全部”选项
【发布时间】:2013-01-26 09:19:07
【问题描述】:

下面的下拉菜单显示选项,其中每个选项的值是来自数据库的StudentId

<select name="student" id="studentsDrop">
<?php
while ( $currentstudentstmt->fetch() ) {
$stu = $dbStudentId;
if(isset($_POST["student"]) && $stu == $_POST["student"]) 
    echo "<option selected='selected' value='$stu'>" . $dbStudentAlias . " - " . $dbStudentForename . " " . $dbStudentSurname . "</option>" . PHP_EOL;
else
    echo "<option value='$stu'>" . $dbStudentAlias . " - " . $dbStudentForename . " " . $dbStudentSurname . "</option>" . PHP_EOL;
}
?>
</select>

但我的问题是如何显示All 选项,如果用户选择此选项,则此选项的值将适用于所有学生 ID。

生成学生值的代码在这里:

【问题讨论】:

  • @OneTrickPony 不会找到学生 ID 为 all 的学生吗?

标签: php


【解决方案1】:

自己把它放在那里:

echo "<option>All</option>";
while ( $currentstudentstmt->fetch() ) {

您可以执行相同的_POST['student'] 检查以查看是否也选择了All

【讨论】:

  • 如果我创建这样的选项,是否会从数据库中查找所有学生?
  • @user1830984 不,您必须对 if ($_POST['student'] == 'All') 进行明确检查,并且不要在查询中使用特定的学生 ID
  • 哦,你是说创建两个查询,一个查询 All 没有针对 studentid 的 where 子句,否则执行另一个查询,其中包含针对特定学生 id 的 WHERE 子句
【解决方案2】:

在大多数情况下,您会使用特殊值(“ALL”或“”或类似的东西)来表示请求了所有 id

【讨论】:

    猜你喜欢
    • 2016-01-06
    • 1970-01-01
    • 1970-01-01
    • 2019-05-25
    • 1970-01-01
    • 2012-03-11
    • 2016-06-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多