Avatar of stkoontz
stkoontz
Flag for United States of America asked on

PHP Contents of select statement stored in Array and used in Dropdown Menu

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
        )

)

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>';

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
PHP

Avatar of undefined
Last Comment
Ray Paseur

8/22/2022 - Mon
Ray Paseur

Couple of things needed here.

Try using the PDO::FETCH_ASSOC flag like this: $result = $sth->fetchAll(PDO::FETCH_ASSOC);
The goal is to get only the associative array keys, not the unwanted numeric keys.

Next try the foreach() iterator like this:
echo '<select name="event groups">';
echo '<option value="">Select...</option>';
foreach ($my_array as $sub_array) {
    foreach ($sub_array as $k => $v) {
        echo '<option value="' . $k . '">' . $v . '</option>' . PHP_EOL;
    }
}
echo '</select>';

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

ASKER
Ray,  Thanks for the help. We're really close.

I updated the page here...

http://testregister.cenational.org/test/test1.php

How can I get the "[id_event_group] => uh" to be the value and the "[event_group_desc] => Urban Hope" to be the description?

Steve



            [event_group_desc] => Urban Hope
ASKER CERTIFIED SOLUTION
Ray Paseur

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
stkoontz

ASKER
Nice! Thanks Ray.  That was exactly what I needed!

Steve
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
Ray Paseur

Great!  Glad it worked out :-)