Programming Theory
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Codewalkers ForumsOther TechnologiesProgramming Theory

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Codewalkers Forums Sponsor:
Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here
  #1  
Old February 17th, 2004, 08:20 PM
LightningWeb LightningWeb is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Posts: 11 LightningWeb User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
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:
Original - php Code
  1.  
  2. <?
  3. $connection = mysql_connect('localhost', 'user', 'pass');
  4.      
  5.     mysql_select_db('$db');
  6.  
  7.     $query = "SELECT * FROM table_a WHERE name ='" . $name . "'";
  8.  
  9.     $result = mysql_query($query);
  10.      
  11.     $num_results = mysql_num_rows($result);
  12.  
  13. $query2 = "SELECT * FROM table_b WHERE color ='" . $color . "'";
  14.  
  15.     $result2 = mysql_query($query2);
  16.      
  17.     $num_results2 = mysql_num_rows($result2);
  18.  
  19.     mysql_close($connection);
  20.  
  21. echo '<tr><td>';
  22. echo '<select name="name">';
  23.  
  24. for ($i=0; $i <$num_results; $i++)
  25.   {
  26.      $row = mysql_fetch_array($result);
  27.        
  28.      echo "<option value='" . $row['name'] ."'>" . $row['name'] . "</option><br>";     
  29.        }
  30.  
  31.  
  32. echo '</select>';
  33. echo '</td></tr>';
  34. echo '<tr><td>';
  35. echo '<select name="color">';
  36.  
  37. for ($i=0; $i <$num_results2; $i++)
  38.   {
  39.      $row2 = mysql_fetch_array($result2);
  40.        
  41.      echo "<option value='" . $row2['color'] ."'>" . $row2['color'] . "</option><br>";     
  42.        }
  43.  
  44.  
  45. echo '</select>';
  46. echo '</td></tr>';
  47.  
  48. ?> 

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?


Reply With Quote
  #2  
Old February 18th, 2004, 09:07 AM
xs0 xs0 is offline
Codewalkers Novice (500 - 999 posts)
 
Join Date: Apr 2007
Location: Ljubljana, Slovenia
Posts: 760 xs0 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 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...

Reply With Quote
  #3  
Old February 18th, 2004, 10:13 AM
tchala tchala is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Location: Manchester, UK
Posts: 59 tchala User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 2
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:
Original - php Code
  1.  
  2. function get_data($fields, $table, $where)
  3. {
  4. $ret = array();
  5.  
  6. if($link = mysql_pconnect($host, $user, $pass))
  7.   {
  8.   if(mysql_select_db($db))
  9.     {
  10.     $sql = "select $fields from $table where $where";
  11.     if($res = mysql_query($sql, $link))
  12.       {
  13.       if(mysql_num_rows($res) > 0)
  14.         {
  15.         while($row = mysql_fetch_array($res)
  16.           {
  17.           $ret[] = $row;
  18.           }
  19.         mysql_free_result($res);
  20.         }
  21.       }
  22.     }
  23.   }
  24. return $ret;
  25. }

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

Reply With Quote
  #4  
Old February 18th, 2004, 03:06 PM
LightningWeb LightningWeb is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Posts: 11 LightningWeb User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
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.

Reply With Quote
Reply

Viewing: Codewalkers ForumsOther TechnologiesProgramming Theory > Multiple Queries needed for separate drop downs?


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