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:
  #1  
Old February 21st, 2005, 11:26 PM
irfreh irfreh is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Feb 2005
Posts: 1 irfreh User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 11 m 43 sec
Reputation Power: 0
PHP drop down menus help

Hello potential saviours,

I have just started to make a test site in php and have come to a bit problem.
I need to make a drop down menu that has been populated from a MySQL databse and
then have the relevant selecion posted over to a result page.

I know that there are examples of drop drown menus on this forum, but what i require is an
understanding of how these are done (like wot does ($row=mysql_fetch_array($result)) mean) because i
have used the example scripts that have been used but i keep getting errors. So in order for me to sort it
i need to understand it first.

If anybody can point me to online tutorials or recources that will teach me how to make a working dynamic
drop down menu, would be greatly appreciated.

Thanks in advanced to who ever replies to this post.

Irf

Reply With Quote
  #2  
Old February 22nd, 2005, 10:20 PM
Madpawn Madpawn is offline
My beat is correct.
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Dec 2004
Posts: 339 Madpawn User rank is Private First Class (20 - 50 Reputation Level)Madpawn User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 2 Days 22 h 3 m 33 sec
Reputation Power: 5
First, remember that a dropdown menu is just another while() loop. The only thing that makes it different is the HTML.

Look over the below and see if it helps:

PHP Code:
//first, connect to your server and place
    //the link identifier into a variable.
    //if, for some reason, this fails,
    //call die() and return mysql's error
    //message
    
$link mysql_connect('hostname''username''password') or die(mysql_error());
    
    
//now, choose which database you'll
    //be retrieving info from
    
mysql_select_db('databasename',$link) or die(mysql_error());
    
    
//formulate your query and place into
    //a variable -- you can pass the query
    //directly into mysql_query(), but it's
    //much easier to deal with dynamic
    //changes and debugging if you have
    //a separate variable
    
$query "SELECT optionname FROM table";
    
    
//send your query to the database
    //this will return a 'resource' 
    //that contains your results.
    //we're going to put that in a new 
    //variable
    
$result mysql_query($query,$link) or die(mysql_error()); 


Now we need to actually get something we can use out of the 'resource' sitting in $result. To do this we're going to use mysql_fetch_array(). This will pull out one row of your result set. So let's say your pulling three city names from the above: New York, Chicago, and L.A. If you call

PHP Code:
 mysql_fetch_array($result


you're creating an array containing 'New York'. Call the same thing again, you get an array containing 'Chicago'.

Of course, you have to place this array into a variable to be able to get anything out of it, so now we've got

PHP Code:
 $row mysql_fetch_array($result); 


$row is now an array containing 'New York':

PHP Code:
//echoes, as you've guessed by now,
    //'New York'
    
echo $row['optionname']; 


Which is all well and good for one row, but we've got three. Therefore, we want to loop through our results. Since mysql_fetch_array() will move itself down the list of rows in your resource, it will eventually reach the end. If you try to call it after that, it will return a boolean FALSE instead of an array. This is convenient for us, because we can use that to combine our array creation with our content loop, giving us

PHP Code:
while( $row mysql_fetch_array($result) )
    {
       echo 
$row['optionname'];
    } 


So the first thing that happens is that $row is set to whatever mysql_fetch_array($result) returns, which will either be an array or a boolean FALSE. If it's an array, the contents of the while() loop will execute, echoing your optionname. Then the while loop starts over, resetting $row to the next value of mysql_fetch_array($result). This gives us another array, so the loop echoes the next optionname. And so on, until you've gone through all your rows, and mysql_fetch_array($result) returns FALSE, telling the while loop there's nothing left and to pack it in.

And that's looping through query results. Making those results a dropdown is only a matter of changing the HTML around $row['optionname']. Just be sure your SELECT opening and closing tags are outside your while(), or you'll repeat them each iteration.
Comments on this post
MadCowDzz agrees: Awesome post! I left this question alone on purpose [laziness]...

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingPHP Development > PHP drop down menus help


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 4 hosted by Hostway
Stay green...Green IT