Solved

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

Posted on 2016-08-24
5
39 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 109

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 109

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 109

Expert Comment

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

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
comma true 6 37
Currency in SQL? 2 29
Ajax and PHP 9 29
Force PDF to open inline as opposed to dowload 16 21
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
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 …

789 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