Jump to content

Recommended Posts

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>";
				}
			}
}

Link to comment
https://forums.phpfreaks.com/topic/137922-foreach-variables/
Share on other sites

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  ::)

Link to comment
https://forums.phpfreaks.com/topic/137922-foreach-variables/#findComment-720936
Share on other sites

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

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/137922-foreach-variables/#findComment-721301
Share on other sites

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>";

Link to comment
https://forums.phpfreaks.com/topic/137922-foreach-variables/#findComment-721419
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/137922-foreach-variables/#findComment-721499
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/137922-foreach-variables/#findComment-721606
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/137922-foreach-variables/#findComment-721839
Share on other sites

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>

Link to comment
https://forums.phpfreaks.com/topic/137922-foreach-variables/#findComment-722180
Share on other sites

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>

Link to comment
https://forums.phpfreaks.com/topic/137922-foreach-variables/#findComment-722380
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.