Nate_23 Posted July 13, 2011 Share Posted July 13, 2011 Hey, I am wanting to make a script that will create a dropdown menu with different types of items (Shoes, Socks, Pants, ...) and how many items there are of that type (125, 44, 56, ...). I'm wondering if there is a way to check a mysql field for different several entries. Currently, I am using many queries like: $sql1 = "SELECT * FROM items WHERE clothing='Shoes'"; $result1 = mysql_query($sql1) or die ("Couldn't get results."); $num1 = mysql_num_rows($result1); $sql2 = "SELECT * FROM items WHERE clothing='Socks'"; $result2 = mysql_query($sql2) or die ("Couldn't get results."); $num2 = mysql_num_rows($result2); $sql3 = "SELECT * FROM items WHERE clothing='Pants'"; $result3 = mysql_query($sql3) or die ("Couldn't get results."); $num3 = mysql_num_rows($result3); Is there a way to get all that done with one query? Is there a way to tell when the mysql field changes and then use a function? Thanks. Link to comment https://forums.phpfreaks.com/topic/241934-dynamic-drop-down-menu/ Share on other sites More sharing options...
fenway Posted July 14, 2011 Share Posted July 14, 2011 Sure. "SELECT * FROM items WHERE clothing IN ('Pants','Socks','Shoes')" Link to comment https://forums.phpfreaks.com/topic/241934-dynamic-drop-down-menu/#findComment-1242517 Share on other sites More sharing options...
Nate_23 Posted July 14, 2011 Author Share Posted July 14, 2011 Oops, I perhaps made that look simpler that I had meant to. After getting the items from the database, I need to separate them as well. Which is why I had had them queried individually. What I'm hoping to have is something similar to this: <?php $sql = "SELECT * FROM items"; $result = mysql_query($sql) or die ("Couldn't get results."); $num = mysql_num_rows($result); while($row = mysql_fetch_assoc($result)){ $clothingtype = $row['clothingtype']; /// separate clothingtype into alike types (all pants, all socks, etc. together). /// should be 125 items with the value "shoes", 44 "Socks", 56 "Pants } echo "<form name='myform'> <select name='option1' size='1'> <option value='$clothingtype1'>$clothingtype1 $numberofclothingtype1 items</option> <option value='$clothingtype2'>$clothingtype2 $numberofclothingtype2 items</option> <option value='$clothingtype3'>$clothingtype3 $numberofclothingtype3 items</option> .... etc"; Link to comment https://forums.phpfreaks.com/topic/241934-dynamic-drop-down-menu/#findComment-1242547 Share on other sites More sharing options...
PFMaBiSmAd Posted July 14, 2011 Share Posted July 14, 2011 You would 'remember' what the last value was using a variable ($last_type) and when you detect a change in the value, close any previous section and start a new section - <?php $sql = "SELECT * FROM items ORDER BY clothingtype"; // get the data you want in the order that you want it $result = mysql_query($sql) or die ("Couldn't get results."); $num = mysql_num_rows($result); $last_type = NULL; // initialize to a value that will never exists as data while($row = mysql_fetch_assoc($result)){ // detect if there is a change in the type if($last_type != $row['clothingtype']){ // detect if not the first section if($last_type != NULL){ // this is not the first section, code to close out the previous section goes here... } // code to start a new section goes here... $last_type = $row['clothingtype']; // remember the new type } // code to output the data within the section goes here... } // detectif there were any sections at all and close out the last one if needed if($last_type != NULL){ // code to close out the last section goes here... } ?> Link to comment https://forums.phpfreaks.com/topic/241934-dynamic-drop-down-menu/#findComment-1242551 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.