简体   繁体   中英

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).

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:

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

Which populated the drop-down with values from the array.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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