Solved

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

Posted on 2016-08-24
5
27 Views
Last Modified: 2016-08-24
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
0
Comment
Question by:stkoontz
  • 3
  • 2
5 Comments
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 41769011
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.
0
 
LVL 2

Author Comment

by:stkoontz
ID: 41769130
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
0
 
LVL 108

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 41769353
Maybe like this?  Still guessing a little since I don't have any way to test, sorry.
echo '<select name="event groups">';
echo '<option value="">Select...</option>';
foreach ($my_array as $sub_array) {
    $k = $sub_array['id_event_group'];
    $v = $sub_array['event_group_desc'];
    echo '<option value="' . $k . '">' . $v . '</option>' . PHP_EOL;
}
echo '</select>';

Open in new window

0
 
LVL 2

Author Closing Comment

by:stkoontz
ID: 41769432
Nice! Thanks Ray.  That was exactly what I needed!

Steve
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 41769456
Great!  Glad it worked out :-)
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now