Jump to content

another looping issue


fife

Recommended Posts

I have a select field in my join form which allows users to select one of my speicifed counties.  In the table the county is link to a country.  I am trying to echo in a select statement a loop of all the available counties in england.

here is the code

             <?php
        $qGetCounty = "SELECT * FROM Counties";
	$rGetCounty = mysql_query($qGetCounty);
	       
        echo "
        <select name=\"County\">
<optgroup label=\"England\">
	 ";
	 while ($county = mysql_fetch_assoc($rGetCounty)){
	 if($county['country']=='England') { 
	 echo "  
          <option value= '{$county['county']}'>{$county['county']}</option>
	 ";
	  }}
	  
	          echo "
         <optgroup label=\"Wales\">
	 ";
	 while ($county=='Wales'){
	 echo "  
          <option value= '{$county['county']}'>{$county['county']}</option>
	 ";
	 }
	  
	  
	            echo "
         <optgroup label=\"Scotland\">
	 ";
	 while ($county=='Scotland'){
	 echo "  
          <option value= '{$county['county']}'>{$county['county']}</option>
	 ";
	 }

echo "
        </select>
      "  ;
        ?>

 

It is not erroring but also it is not worlking correctly.  It shows all the counties in England and both the labels for scotland and wales but does not show any counties in these two areas.  I am new to loops so do not know how to fix this.  I have tried changing the line

 

while ($county=='Wales')

 

to

 

while ($county = mysql_fetch_assoc($rGetCounty))

 

But this makes no difference

Link to comment
Share on other sites

Hi

 

You need something like this

 

<?php
$qGetCounty = "SELECT * FROM Counties ORDER BY FIELD(country,'England','Wales','Scotland'), county ";
$rGetCounty = mysql_query($qGetCounty);

echo "<select name=\"County\">";
$PrevCountry = "";
while ($county = mysql_fetch_assoc($rGetCounty))
{
	if ($county['country'] != $PrevCountry)
	{
		$PrevCountry = $county['country'];
		echo "<optgroup label='$PrevCountry'>";
	}
	echo " <option value= '{$county['county']}'>{$county['county']}</option>";
}
echo "</select>" ;
?>

 

Get the data from the table in country order, then loop through it. If the country name changes the put the country out.

 

All the best

 

Keith

Link to comment
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.