|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| ||||||||||||||||||||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||||
|
|||||
|
Multiple Queries needed for separate drop downs?
I created two drop down lists from a database, I'm not sure if I did it the right way. Whether I need two queries or not. I'm getting the info for list 1 from table A and the info for list 2 from table B. Is it ok to create 2 separate queries for this or is there a way I can do it with one? Below is what I did:
php Code:
Where I think I'm getting confused is the data doesn't have a common field so I don't know how to select it at the same time(if that's even possible). What I did works, but I'm thinking there's a better way to go about it. What if I wanted to do a third drop down from a different table? |
|
#2
|
|||
|
|||
|
RE: Multiple Queries needed for separate drop downs?
As the data is unrelated, it is perfectly OK to use two queries. Why would you want to use just one? My guess is that even if you did manage to somehow squeeze them into one query, it wouldn't be any faster or better...
|
|
#3
|
|||||
|
|||||
|
RE: Multiple Queries needed for separate drop downs?
In this kinda situation the simplest thing to do is write a single function that returns an array of the restults you need to populate your dropdowns
Something like... php Code:
OK - this is a bit simplistic but it'll do the job... Now all you gotta do is call the function twice, once for each drop-down you want $dd1 = get_data("fld", "tab", "name = 'joe'"); $dd1 now is now an array of 0 or more entries with the data you want in it change yer for clause to use count($dd1) rather than $num_results1 and pick the data out of the $dd1 array - $dd1['name'] etc I'd actually do exactly the same kinda thing with the dropdown code - bung it in a function then you only got one lot of it. Enhance this a little and you can write a nice little function that'll create you a dropdown from a table with one call - just more params to pass to the function.. Also it's pretty pointless selecting * - just select the fields you want |
|
#4
|
|||
|
|||
|
RE: Multiple Queries needed for separate drop downs?
Thanks for your replies gentlemen. Being new to both PHP and MySQL I just wasn't sure if it was ok to have multiple queries like that. tchala, thank you for that code snippet, I've just gotten brave enough to start writing my own functions and that gave me more insight. Much appreciated.
Thanks again. |
![]() |
| Viewing: Codewalkers Forums > Other Technologies > Programming Theory > Multiple Queries needed for separate drop downs? |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|