|
|
|||||||||
|
|||||||||
|
|||||||||
| |
|||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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 |
|
#2
|
|||
|
|||
|
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:
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:
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 is now an array containing 'New York': PHP Code:
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:
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. |
![]() |
| Viewing: Dev Articles Community Forums > Programming > PHP Development > PHP drop down menus help |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|