Solved

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

Posted on 2016-08-24
5
44 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 110

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 110

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 110

Expert Comment

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

Featured Post

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
This article discusses four methods for overlaying images in a container on a web page
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

632 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