简体   繁体   English

使用数组在PHP中填充下拉列表

[英]Populating Drop-Down in PHP using Array

I am trying to populate a drop-down menu with values from an array. 我正在尝试使用数组中的值填充下拉菜单。

I have tried to follow other answers but the syntax doesn't seem to be working. 我尝试遵循其他答案,但是语法似乎不起作用。 (I'm still relatively new to PHP). (我对PHP还是比较陌生的)。

The following code which I am working on was produced by someone else. 我正在处理的以下代码是由其他人生成的。

$sqlite_query = "SELECT * FROM dis_kind";
$result = $db->query($sqlite_query);
$array = $result->fetchArray();

$output = "<select name=\"kind\" class=\"dis\" >\n";
$output .= "<option value=\"$this->wildcard_value\"></option>\n";

foreach ($result as $array) {
    $value = $array['kind'];
    $output .= "<option value=\"";
    $output .= $value;
    $output .= "\">";
    $output .= $value;
    $output .= " - ";
    $output .= $array['description'];
    $output .= "</option>\n";
}

$output .= "</select>\n";

I don't know why it has been done the way it has but I am stumped as to getting my drop-down to work. 我不知道为什么要按原样进行,但是我对下拉菜单的使用感到沮丧。

Currently, the box appears but is populated with no values. 当前,该框出现,但没有任何值。

Thanks. 谢谢。

Eventually managed to solve the problem myself - before I pulled my hair out! 最终我自己解决了问题-在拔出头发之前!

Instead of using the foreach loop, I used the following: 我没有使用foreach循环,而是使用了以下内容:

while($array = $result->fetchArray()) 
{
    // Output code.
}

Which populated the drop-down with values from the array. 其中使用数组中的值填充了下拉菜单。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM