mattjones Posted December 21, 2008 Share Posted December 21, 2008 Hello i am using a foreach loop to generate a label in a drop down list and have made an array for the various catagorys. the sql table has three columns [group] which declaires the catagorys, [name], and [variable name] for the options in the list. The problem is the fist loop is working and printing out the catagory name and all the options under it but then when the loop goes through the second time the catagory is printed but the if statement appears to be ignored and none of the options are printing. the loop continues on to the end printing out just the catagorys (lables) and not the options apart from on the first loop. should the variable $val change each time to make the if statement true because that is what i was expecting? foreach ($catagory as $val) { echo"<optgroup label=\"$val\">"; if ($row['group'] == $val) { echo " <option value=\"http://www.musicandmediadirectory.co.uk/directory/business-directory.php?business=$row[variable_value]\">$row[name]</option> "; } while ($row = mysql_fetch_array($bus_result)) { if ($row['group'] == $val) { echo "<option value=\"http://www.musicandmediadirectory.co.uk/directory/business-directory.php?business=$row[variable_value]\">$row[name]</option>"; } } } Quote Link to comment https://forums.phpfreaks.com/topic/137922-foreach-variables/ Share on other sites More sharing options...
Mad Mick Posted December 21, 2008 Share Posted December 21, 2008 The first time you go through the foreach loop $row['group']=something which is checked against $val. You then start the while loop - this continues until mysql_fetch_array returns nothing i.e. $row is now empty. The foreach runs again but now $row['group'] is empty so will never equal $val. Seems complicated and I'm not sure exactly what you are trying to do, so can't offer much more advice... Oh and its category not catagory Quote Link to comment https://forums.phpfreaks.com/topic/137922-foreach-variables/#findComment-720936 Share on other sites More sharing options...
mattjones Posted December 22, 2008 Author Share Posted December 22, 2008 thanks i hink i get it. i am trying to populate a drop down list but i am not sure of the best way to do it. I thought this way would work but obviously not. Mabey there is an easier way you could help me with. I will give you an example of the list i want using the following table. GROUP NAME VARIABLE fizzy drinks Coca Cola coca-cola fizzy drinks Tango tango alcohol Stella Artois stella-artois alcohol Vodka vodka hot drinks Hot Chocolate hot-chocolate hot drinks Coffee coffee I would like to make a drop down list with the GROUP as a label (unselectable), the NAME as the selectable items in the list and the VARIABLEs as the values in the list. i.e <optgroup label="GROUP"> <option value="VARIABLE">NAME</option> so that the list looks like this. fizzy drinks Coca Cola Tango alcohol Stella Artois Vodka hot drinks Hot Chocolate Coffee origionally i made an array with the group names in it ($category = array('fizzy drinks', 'alcohol', 'hot drinks')) and used the code from the fist post but it did not work. Any help would be appreciated. Matt Quote Link to comment https://forums.phpfreaks.com/topic/137922-foreach-variables/#findComment-721301 Share on other sites More sharing options...
Mad Mick Posted December 22, 2008 Share Posted December 22, 2008 Well this might do it - could probably be tidied up for the optgroup bits but hopefully it gives a good idea... $sql="SELECT * FROM tablename ORDER BY groupname"; $result=mysql_query($sql); $oldgroup=""; $first=1; while ($row=mysql_fetch_assoc($result)){ if ($row['groupname']!=$oldgroup){ if (!$first) echo "</optgroup>\n"; echo "<optgroup label=\"".$row['groupname']."\">\n"; $oldgroup=$row['groupname']; $first=0; } echo "<option value=\"".$row['id']."\">".$row['name']."</option>\n"; } echo "</optgroup>"; Quote Link to comment https://forums.phpfreaks.com/topic/137922-foreach-variables/#findComment-721419 Share on other sites More sharing options...
mattjones Posted December 22, 2008 Author Share Posted December 22, 2008 wow it worked, still trying to work out how. Sort of getting it but not 100% also does not print out first option for some reason. Was doing this with my code too that is why i had that extra identical if statement before the while loop. Thanks very much though.. Matt Quote Link to comment https://forums.phpfreaks.com/topic/137922-foreach-variables/#findComment-721499 Share on other sites More sharing options...
mattjones Posted December 22, 2008 Author Share Posted December 22, 2008 I thought i could add an extra row to the top of the mysql table with the group name the same as the first group. Like a dummy row so php could ignore it (like it seems to do) and start printing the options including the first one. But i dont know how to insert a row at the top of the database. matt Quote Link to comment https://forums.phpfreaks.com/topic/137922-foreach-variables/#findComment-721606 Share on other sites More sharing options...
Mad Mick Posted December 23, 2008 Share Posted December 23, 2008 Tested this code out and all options are displayed, nothing missing... Don't try to fiddle the database but see if you can understand why a row is missing. Quote Link to comment https://forums.phpfreaks.com/topic/137922-foreach-variables/#findComment-721811 Share on other sites More sharing options...
mattjones Posted December 23, 2008 Author Share Posted December 23, 2008 I really cant see why it is not printing the first option. This is very strange if it is working for you, i am confused. The first catagory label is being displayed but it seems like the option is not set until the second loop but i cannot see why this would be. it is not leaving a blank space or anything it is printing the first category title then going straight to the second option then works as expected from there on. matt Quote Link to comment https://forums.phpfreaks.com/topic/137922-foreach-variables/#findComment-721839 Share on other sites More sharing options...
MadTechie Posted December 23, 2008 Share Posted December 23, 2008 mattjones, Mad Mick's code look sound.. can you post your code incase you missed something Quote Link to comment https://forums.phpfreaks.com/topic/137922-foreach-variables/#findComment-721876 Share on other sites More sharing options...
mattjones Posted December 23, 2008 Author Share Posted December 23, 2008 as far as i can see it is more or less the same. $bus_result = mysql_query("SELECT * FROM regions ORDER BY `country`, `region`"); $row = mysql_fetch_array($bus_result); $oldgroup=""; $first=1; while ($row=mysql_fetch_assoc($bus_result)) { if ($row['country']!=$oldgroup) { if (!$first) echo "</optgroup>\n"; echo "<optgroup label=\"".$row['country']."\">\n"; $oldgroup=$row['country']; $first=0; } echo "<option value=\"#".$row['region_variable']."\">".$row['region']."</option>\n"; } echo "</optgroup> Quote Link to comment https://forums.phpfreaks.com/topic/137922-foreach-variables/#findComment-722180 Share on other sites More sharing options...
MadTechie Posted December 23, 2008 Share Posted December 23, 2008 Yeah but your missing the first row because of this line $row = mysql_fetch_array($bus_result); remove it shoild fix the problem $bus_result = mysql_query("SELECT * FROM regions ORDER BY `country`, `region`"); // $row = mysql_fetch_array($bus_result); //<---REMOVE $oldgroup=""; $first=1; while ($row=mysql_fetch_assoc($bus_result)) { if ($row['country']!=$oldgroup) { if (!$first) echo "</optgroup>\n"; echo "<optgroup label=\"".$row['country']."\">\n"; $oldgroup=$row['country']; $first=0; } echo "<option value=\"#".$row['region_variable']."\">".$row['region']."</option>\n"; } echo "</optgroup> Quote Link to comment https://forums.phpfreaks.com/topic/137922-foreach-variables/#findComment-722380 Share on other sites More sharing options...
mattjones Posted December 23, 2008 Author Share Posted December 23, 2008 Thank you very much for your help mad mick and mad techie. It now works perfectly. what does that line actually do then? I thought it just pulled the results out and set them into an array. Matt Quote Link to comment https://forums.phpfreaks.com/topic/137922-foreach-variables/#findComment-722487 Share on other sites More sharing options...
MadTechie Posted December 23, 2008 Share Posted December 23, 2008 it does but then you don't use the row ..... then the loop starts and it pulls the next record as a associative array until no more records exist.. if that makes sence Quote Link to comment https://forums.phpfreaks.com/topic/137922-foreach-variables/#findComment-722568 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.