I'd like to store the output of an SQL select statement into an array, then use that array to populate a dropdown menu.
I'm using $result = $sth->fetchAll(); to store the contents of 2 fields into the array. Which comes out looking like this...
Array
(
[0] => Array
(
[id_event_group] => uh
[0] => uh
[event_group_desc] => Urban Hope
[1] => Urban Hope
)
[1] => Array
(
[id_event_group] => mo
[0] => mo
[event_group_desc] => Momentum
[1] => Momentum
)
)
Select all Open in new window
When I use the following code to create the dropdown, I just get 'array' 'array' as the options.
echo '<select name="event groups">';
echo '<option value="">Select...</option>';
foreach ($my_array as $k => $v) {
echo '<option value="' . $k . '">' . $v . '</option>';
}
echo '</select>';
Select all Open in new window
If it helps, here's a link to the page.
http://testregister.cenational.org/test/test1.php
Thanks in advance for giving me a hand.
Steve
Try using the PDO::FETCH_ASSOC flag like this: $result = $sth->fetchAll(PDO::FETCH_
The goal is to get only the associative array keys, not the unwanted numeric keys.
Next try the foreach() iterator like this:
Open in new window
The goal here is to iterate over the result set with the first foreach() and iterate over each row of the results set with the second foreach().I can't test any of this, but I think it's right in principle.