使用 PHP 填充 下拉菜单?

发布于 2024-08-25 09:41:55 字数 257 浏览 8 评论 0原文

<select name="select">
      
</select>

我想用数据库中的值填充上述标签。

到目前为止我已经编写了 PHP 代码。

while($row=mysql_fetch_array($result))
{

}

$row 正在获取正确的值。如何将其添加到

<select name="select">
      
</select>

I want to populate the above tag with values from database.

I have written PHP code up to this.

while($row=mysql_fetch_array($result))
{

}

$row is fetching correct values. How to add it to the <select>

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

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

发布评论

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

评论(7

断爱 2024-09-01 09:41:55

像这样的事情怎么样:

echo '<select name="select">';
while($row=mysql_fetch_array($result))
{
    echo '<option value="' . htmlspecialchars($row['column_for_value']) . '">' 
        . htmlspecialchars($row['column_for_label']) 
        . '</option>';
}
echo '</select>';

当然,由您决定 $row 中的哪些项目应用于每个 的值和文本 请

Just make sure you are escaping the data that comes from your DB -- especially if it can contain HTML ; as you are outputting HTML, this can be done with [`htmlspecialchars`][1] or [`htmlentities`][2].

注意这些可能需要一些我在示例中没有使用的附加参数 - 设置这些参数可能很有用,具体取决于您使用的字符集。

What about something like this :

echo '<select name="select">';
while($row=mysql_fetch_array($result))
{
    echo '<option value="' . htmlspecialchars($row['column_for_value']) . '">' 
        . htmlspecialchars($row['column_for_label']) 
        . '</option>';
}
echo '</select>';

Of course, up to you to decide which items from $row should be used for the value and the text of each <option>

Just make sure you are escaping the data that comes from your DB -- especially if it can contain HTML ; as you are outputting HTML, this can be done with [`htmlspecialchars`][1] or [`htmlentities`][2].

Note that those might take a couple of additionnal parameters that I didn't use in my example -- setting those can be useful, depending on the charset you're using.

遥远的绿洲 2024-09-01 09:41:55

您可以通过执行以下操作来查看可用的内容:

var_dump($result);
exit;

这将打印第一个结果及其数组内容。然后您可以看到要使用哪个字段来填充该选项。从那里,您可以执行以下操作:

foreach ($result['field'] as $field) {
   print '<option value="'.$field.'">$field</option>';
}

当然,这是一个非常基本的示例,正​​如其他人指出的那样,您可能希望在将数据放入表单之前清理数据。

You can see whats available to use by doing this in the while:

var_dump($result);
exit;

That will print the first result and its array contents. Then you can see what field you want to use to populate the option. From there, you would do something like:

foreach ($result['field'] as $field) {
   print '<option value="'.$field.'">$field</option>';
}

Of course this is a very basic example, and as others have noted you may want to clean the data before putting it into a form.

故事还在继续 2024-09-01 09:41:55

$selected_value="selected_value";
echo '<select name="select">';
while($row=mysql_fetch_array($result))
{
    if($selected_value==htmlspecialchars($row['column_for_value']))
    $selected=' selected';
    else
    $selected='';
    echo '<option value="'.htmlspecialchars($row['column_for_value']).'"'.$selected.'>'
    .htmlspecialchars($row['column_for_label']).
    '</option>';
}
echo '</select>';

Pascal MARTIN 代码的一些补充,用于自动选择某些预定义值


$selected_value="selected_value";
echo '<select name="select">';
while($row=mysql_fetch_array($result))
{
    if($selected_value==htmlspecialchars($row['column_for_value']))
    $selected=' selected';
    else
    $selected='';
    echo '<option value="'.htmlspecialchars($row['column_for_value']).'"'.$selected.'>'
    .htmlspecialchars($row['column_for_label']).
    '</option>';
}
echo '</select>';

Some addition to Pascal MARTIN code, for auto selection of some predefined value

迷离° 2024-09-01 09:41:55

我编辑了最后一个条目,效果非常好。我现在唯一的麻烦是,一旦用户提交了表单,下拉列表就会变成空白......有谁知道一个简单的解决方案

echo '<select name="course" id="course" >';
                while( $option = mysql_fetch_array($course_results)) {
echo "<option    value=".htmlspecialchars($option['cid']).">".htmlspecialchars($option['cname'])."</option>";
}
echo "</select>";

I editied the last entry to this and it works perfectly. The only hassle I have now is once the user has submitted the form the dropdown goes blank... Does anyone know a simple solution

echo '<select name="course" id="course" >';
                while( $option = mysql_fetch_array($course_results)) {
echo "<option    value=".htmlspecialchars($option['cid']).">".htmlspecialchars($option['cname'])."</option>";
}
echo "</select>";
梦亿 2024-09-01 09:41:55

上述所有答案都可以,但都不正确,需要额外的工作。您不应该使用 echo 来输出到屏幕,也没有必要这样做。下面的示例假设您正在使用包含数据的对象,但您已经明白了。

<select name="sales_person">
        <?php foreach ($sales_people as $sales_person){?>
            <option value="<?=$sales_person->first_name?> <?=$sales_person->last_name?>"><?=$sales_person->first_name?> <?=$sales_person->last_name?></option>
        <?php }?>
        </select>

重点是你不必回显 html

All the above answers will work, but are not proper and require extra work. You should not use echo to output to the screen and you don't have to. The below example assumes you are using objects containing data, but you get the idea.

<select name="sales_person">
        <?php foreach ($sales_people as $sales_person){?>
            <option value="<?=$sales_person->first_name?> <?=$sales_person->last_name?>"><?=$sales_person->first_name?> <?=$sales_person->last_name?></option>
        <?php }?>
        </select>

The point being you don't have to echo out the html

£烟消云散 2024-09-01 09:41:55
echo "<select>";
while( $option = mysql_fetch_array($result)) {
  echo "<option>".htmlspecialchars($option['column'])."</option>";
}
echo "</select>";
echo "<select>";
while( $option = mysql_fetch_array($result)) {
  echo "<option>".htmlspecialchars($option['column'])."</option>";
}
echo "</select>";
风轻花落早 2024-09-01 09:41:55
echo '<select>';
while($row=mysql_fetch_array($result)) {
  echo '<option>'.$row['whatever_index'].'</option>';
}
echo '</select>';

将“whatever_index”替换为您要获取的列名称。

echo '<select>';
while($row=mysql_fetch_array($result)) {
  echo '<option>'.$row['whatever_index'].'</option>';
}
echo '</select>';

Replace 'whatever_index' with the column name you are fetching.

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