Tricky solution needed for newbie

I will post the script if needed, but it might be an easy fix for someone other then myself that is just starting out with php.

I have a zip code script that searches by radius for any zip codes within a set radius. (standard search by location type of search).
the way I have it set up is that the script first finds all the zip codes within a mile radius of a set zip code then passes the results into my sql to find any ( let’s say store) locations near by. It is all within a loop…with the sql being in the loop. So lets say someone types zip 03108 within 5 miles and the zip code script starts the loop. It passes 03108 to the sql statement which returns any locations within that zip code of 03108. Then will find the nearest zip code (lets say 03109) and passes it to sql statement which returns any locations within that zip (03109) code. Then the zip code script sends the next nearest zip (03110) code to the sql statement to find any locations within that zip. So on and so on till all the zip codes within the 5 mile radius are found.
Now it works great like it is, but I am trying to find a way to “error” the loop if no groups are found within the complete radius. Meaning no result from any of the loops.
I have tried

<?php if (mysql_num_rows($result) == 0) {
    echo "No Location found. Please expand your search and try again";
   
}?>

after the sql within the loop, but if the first time thru results are found it continues to loop and if the next time thru no result are found it will echo no groups found and then loop again…so on and so on. So i could have results that look like this:

location A at zip code 03108
NO LOCATION FOUND (03109)
location B at zip code 03110
NO LOCATION FOUND (03111)
NO LOCATION FOUND (03112)
location C at zip code 03113

I want results like:

location A at zip code 03108
location B at zip code 03110
location C at zip code 03113

Or result like this if no locations found in all of the loops.
“NO LOCATIONS FOUND”

Hope it is not that confusing. Any help would be great…thanks


$z = new zipcode_class;

?>
<form action="" method="post">

Search for Yoga Studios
<label for="mile"></label>
<select name="mile" value="">
    <option>1</option>
    <option>5</option>
    <option>10</option>
    <option>15</option>
</select>
miles from
<label for="zip">*Zip Code (required)</label>
<input name="zip" value="" type="text" />
<input name="search" type="submit" />
</form>

 <?php
if (isset($_POST['search'])){
  		$mile = $_POST['mile'];
		$zip = $_POST['zip'];

//sets variable for mile"s" so if resaults are 1 mile, it is not 1 miles.		
if ($mile == 1){
	$s = "";
}else{
	$s = "s";
}

$zips = $z->get_zips_in_range($zip, $mile, _ZIPS_SORT_BY_DISTANCE_ASC, true); 
if ($zips === false){ echo 'Please enter a valid 5-digit ZIP code. Check for accuracy and try again.'.$z->last_error;
}else{
			echo "<h3>Yoga Groups within $mile mile$s of $zip</h3>";

   foreach ($zips as $key => $value) { 
//      echo "Zip code <b>$key</b> is <b>$value</b> miles away from <b>97214</b>.<br />";
				
	$sql =("SELECT
			yogabp_groups.name,
			yogabp_groups.slug,
			yogabp_groups.description,
			yogabp_groups_extra_details.state,
			yogabp_groups_extra_details.street_address,
			yogabp_groups_extra_details.city,
			yogabp_groups_extra_details.group_id
			FROM
			zip_code
			Inner Join yogabp_groups_extra_details ON yogabp_groups_extra_details.area_code = 			zip_code.zip_code
			Inner Join yogabp_groups ON yogabp_groups.id = 																yogabp_groups_extra_details.group_id
			WHERE
			zip_code.zip_code IN ($key)");
				
$result = mysql_query($sql);

if (mysql_num_rows($result) == 0) {
    echo "No  groups found in your area. Please expand your search and try again";
   
}

while ($row = mysql_fetch_array($result)){
	
}

//print_r(get_defined_vars());		
   $name = $row['name'];
   $state = $row['state'];
   $street = $row['street_address'];
   $city = $row['city'];
?>
 
   <table  cellspacing="5"  cellpadding="5" border="1">
  <tr>
    <td><?php echo $name?></td>
    <td><?php echo $state?></td>
    <td><?php echo $street ?></td>
    <td><?php echo $city?></td>
  </tr>
</table>

<?php

I got it. Took sql out of the loop and inserted the array values into it as a comma delimited list in the WHERE IN.
Works great!!



 <?php
if (isset($_POST['search'])){
  		$mile = $_POST['mile'];
		$zip = $_POST['zip'];

//sets variable for mile"s" so if resaults are 1 mile, it is not 1 miles.		
if ($mile == 1){
	$s = "";
}else{
	$s = "s";
}


$zips = $z->get_zips_in_range($zip, $mile, _ZIPS_SORT_BY_DISTANCE_ASC, true);

if ($zips === false){ echo 'Please enter a valid 5-digit ZIP code. Check for accuracy and try again.'.$z->last_error;
}else{
			echo "<h3>Yoga Groups within $mile mile$s of $zip</h3>";

	$zips_temp = (array_keys($zips)); //saves array keys (zip codes) only to new array
	$zip_codes = implode(",",$zips_temp);//makes new array into a comma list and passes list to sql 
					
			
	$sql =("SELECT
			yogabp_groups.name,
			yogabp_groups.slug,
			yogabp_groups.description,
			yogabp_groups_extra_details.state,
			yogabp_groups_extra_details.street_address,
			yogabp_groups_extra_details.city,
			yogabp_groups_extra_details.group_id
			FROM
			zip_code
			Inner Join yogabp_groups_extra_details ON yogabp_groups_extra_details.area_code = 			zip_code.zip_code
			Inner Join yogabp_groups ON yogabp_groups.id = 																yogabp_groups_extra_details.group_id
			WHERE
			zip_code.zip_code IN ($zip_codes)");

  
				
$result = mysql_query($sql);

if (mysql_num_rows($result) == 0) {//if no rows found...echo 
   echo "No Yoga groups found in your area. Please expand your search and try again";
   
}

while ($row = mysql_fetch_array($result)){

   $name = $row['name'];
   $state = $row['state'];
   $street = $row['street_address'];
   $city = $row['city'];
?>
   <table  cellspacing="5"  cellpadding="5" border="1">
  <tr>
    <td><?php echo $name?></td>
    <td><?php echo $state?></td>
    <td><?php echo $street ?></td>
    <td><?php echo $city?></td>
  </tr>
</table>

<?php
}}}}