Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2016-08-24
5
Medium Priority
?
47 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 111

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 111

Accepted Solution

by:
Ray Paseur earned 2000 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 111

Expert Comment

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

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
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…

721 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