|
|
|||||||||
|
|||||||||
|
|||||||||
| |
|||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
|
|
SlickEdit: Code in over 40 languages across 7 platforms. SlickEdit’s unmatched power, speed, and flexibility allows even the most accomplished developers to write better code faster. Download a free trial today! |
|
#1
|
|||
|
|||
|
How to populate a drop down with an array?
Hi,
This seems like it should be a pretty easy question, but I just can't seem to figure it out. If I have an html page echoed IN A FUNCTION how do I populate a drop down menu in that page with the values from an array. I'm guessing that because the page is in a function then I'd write something like this : <select name=\"UserShow\"> <option value=\"0\">Select an Item</option> $my_list </select> But what's the PHP code to get the array to make each of its value an option in the drop down? It's driving me crazy, there's got to be an answer somewhere. I know how to do this when the html page isn't in a function, but I really want to keep it echoed in the function. Thanks for any help |
|
#2
|
|||
|
|||
|
What difference does it make whether its in a function or not? Sorry think I'm missing the point here...
-KM- |
|
#3
|
|||
|
|||
|
Quote:
Well having the html page echoed in a function means that I can't populate the dropdown with this method: <select name=\"UserShow\"> <option value=\"0\">Select an Item</option> <?PHP foreach($my_list as $key => $value) { echo "<option value=\"$key\">$value</option>\n"; } ?> </select> So I'm trying to find out if I can achieve the same effect by just using a variable i.e. <select name=\"UserShow\"> <option value=\"0\">Select an Item</option> $my_list </select> and having the foreach loop somewhere else in the script. Hope that makes sense. If you know how you achieve this I would really appreciate it. |
|
#4
|
|||
|
|||
|
If you have the same variables and the same code surely it should produce the same result whether it is in a function or not?
-KM- |
|
#5
|
||||
|
||||
|
Yeah, functions can output just as easily.
If you don't WANT to output from the function, you can simple parse it all to a string, return the string from the function, and then echo the entire string at once. |
![]() |
| Viewing: Dev Articles Community Forums > Programming > PHP Development > How to populate a drop down with an array? |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|