SunQuest
 
           PHP Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingPHP Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
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  
Old September 7th, 2004, 05:26 AM
Slash5 Slash5 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Sep 2004
Posts: 5 Slash5 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
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

Reply With Quote
  #2  
Old September 7th, 2004, 06:01 AM
kode_monkey kode_monkey is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Posts: 367 kode_monkey User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 7 m 21 sec
Reputation Power: 5
What difference does it make whether its in a function or not? Sorry think I'm missing the point here...

-KM-

Reply With Quote
  #3  
Old September 7th, 2004, 07:09 AM
Slash5 Slash5 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Sep 2004
Posts: 5 Slash5 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Quote:
Originally Posted by kode_monkey
What difference does it make whether its in a function or not? Sorry think I'm missing the point here...

-KM-


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.

Reply With Quote
  #4  
Old September 7th, 2004, 07:40 AM
kode_monkey kode_monkey is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Posts: 367 kode_monkey User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 7 m 21 sec
Reputation Power: 5
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-

Reply With Quote
  #5  
Old September 7th, 2004, 11:23 AM
Itsacon's Avatar
Itsacon Itsacon is offline
Command Line Warrior
Click here for more information
 
Join Date: Aug 2004
Location: Sector ZZ9 Plural Z Alpha
Posts: 956 Itsacon User rank is Lance Corporal (50 - 100 Reputation Level)Itsacon User rank is Lance Corporal (50 - 100 Reputation Level)Itsacon User rank is Lance Corporal (50 - 100 Reputation Level)  Folding Points: 650865 Folding Title: Super Ultimate Folder - Level 2Folding Points: 650865 Folding Title: Super Ultimate Folder - Level 2Folding Points: 650865 Folding Title: Super Ultimate Folder - Level 2Folding Points: 650865 Folding Title: Super Ultimate Folder - Level 2Folding Points: 650865 Folding Title: Super Ultimate Folder - Level 2Folding Points: 650865 Folding Title: Super Ultimate Folder - Level 2Folding Points: 650865 Folding Title: Super Ultimate Folder - Level 2
Time spent in forums: 6 Days 8 h 23 m 32 sec
Reputation Power: 4
Send a message via ICQ to Itsacon
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.

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingPHP Development > How to populate a drop down with an array?


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway