Page 1 of 1

Populating Drop Down List/Menu Form

Posted: Sun Jan 22, 2006 10:02 pm
by BZorch
I am trying to populate my drop down form fields using PHP and MYSQL. I have search for insight into this issue and have yet found a specific answer to it.

First is this a good idea? It seems like the most efficient and accurate way to create the menu. As I add news records, I will not have to worry about changing the fields.

Basically, I have three form fields. Country, State, City. I then use the results of a query to populate all three each drop down menu. One for Country, one for State, one for City. My problem is that I need to sort each $row[0], $row[1], $row[2] so that the menus are in alphabetical order, but they only sort by one of the three categories. So Country may be in order, but not City or State.

I have been able to create three queries for each category ($result_country, $result_state, $result_city) within the same database connection and then create each form field based on each query using a while loop.

Is this the correct way to do it or is there a php function that I can use to sort the array based on a certain row? Then when I specify it to print from $row[2] I can then sort by that row and for the others sort by their respective row.

Using three separate queries seemed inefficient. It works fine, but once I have a lot of traffic it may not be as efficient.

Any insight would be appreciated.

Thanks.

Posted: Sun Jan 22, 2006 10:10 pm
by feyd
CoderGoblin's tutorial may be of interest... it's in the tutorial board (on this site)

Posted: Mon Jan 23, 2006 8:23 pm
by BZorch
Thanks. The tutorial you suggested seems like it was geared toward using javascript and it does not involving the use of MYSQL to dynamically create the menu.

While reading it, I thought that I might be able to create the one query and then break up the $results into separate arrays using php. Then I would not have to query the database three times. I could query it once, then break it up into three arrays that could be sorted individually. Would this be more efficient than three queries?

Thanks,

Posted: Tue Jan 24, 2006 9:54 am
by Bill H
My personal opinion is that getting one result from the db and breaking it into three PHP arrays is not going to be any faster than getting three db results. MySQL is super fast at producing sorted results, and that's is really what a database is all about.

I would do three queries.