PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Hi,
I wonder if anyone can help with me trying to dynamically populate a dropdown box using PHP. I have searched for over an hour or so and even looked here at the turorials but to no avail.
I have been working with this:
My MySQL database has one table called ‘locations’ and the 2 columns in it are 'Counties_ID' and 'Counties_Name'.
But I’m so new to this that I’m not getting very far. I have done this in ColdFusion ok but I’m trying to transfer this CF site over to PHP so if anyone can point me in the right direction or tell me if I’m close or way off I’d be most grateful.
Thanks a mil
Brian
have you tried this? How is get_locations() defined? (If need be, post its code. Remember to change/remove the username and password passed to mysq on connection, as well as the host name if different from localhost.)
Thanks for getting back to me. I’m not to sure I follow what you mean so I have posted most of the code from the page I’m working on. This has no passwords as I use a separate folder for these to connect to the database etc.
I simply want to populate a dropdown box with a list of placed that are in a table in my database.
I have done this with CF:
$query = \"e;SELECT Counties_ID, Counties_Name FROM locations ORDER BY Counties_Name\"e;;
$result = mysql_query($query) or die(mysql_error());
echo \"e;<select name='selLocations' id='selLocations'>\n\"e;;
while($dbRow = mysql_fetch_array($result)){
echo \"e;<option value='\"e; . $dbRowї\"e;Counties_ID\"e;] . \"e;'>\"e; . $dbRowї\"e;Counties_Name\"e;] .\"e;</option>\n\"e;;
}
echo \"e;</select>\n\"e;;
I have not checked the code in a live environement, so there may be typos/syntax errors. Also, I have not checked for an empty result set, but that is easy enough to add.
Hope that helps.
Last edited by Jean-Yves on Thu Mar 31, 2005 4:46 pm, edited 1 time in total.
Thanks very much Jean, this works a treat once I tweaked the query texts a bit.
The only one thing I notice is that the result leaves out the first row of the table which has an ID of 1 for example I have 1 table with two columns similar to this in my database:
The only thing that I can think of off the top of my head is that you might have set LIMIT 1,9999 or similar on the SQL - this should be 0,9999 (or whatever top value you want)? Unlikely though.