Jump to content

Limiting results based on drop-down within foreach loop


timmah1

Recommended Posts

I have a search form that has a drop down

<select name="radius" id="radius">
      <option value="5">5 mi.</option>
      <option value="10">10 mi.</option>
      <option value="15">15 mi.</option>
      <option value="20">20 mi.</option>
      <option value="50">50 mi.</option>
      <option value="100">100 mi.</option>
    </select>

 

I'm trying to limit the results in a foreach loop within what was selected.

 

Meaning, if someone selects 10, results within 10 miles will show

The foreach is

foreach ($stores as $k=>$v) {
            $output = "<h3 style='margin:0;padding:0'><b>".$storeinfo[$k]['MktName']."</b><br>(approx ".$v." miles)</h3>"; 
            $output .= "<p style='margin:0 0 10px 0;padding:0'>".$storeinfo[$k]['LocAddSt']."<br>"; 
            $output .= $storeinfo[$k]['LocAddCity'].", ".$storeinfo[$k]['LocAddState']." ".$storeinfo[$k]['zipcode']."</p>";
            print_r($output);           
        }

 

$v being the distance. So I need to show only the results of $v that are less than $r.

 

How would I go about doing this?

 

Right now, $v displays numbers like 5.04, 173.9 and so forth.

 

Can anybody help me out?

 

Thanks in advance.

 

foreach ($stores as $k=>$v) {
if ($v<$r){
	$output = "<h3 style='margin:0;padding:0'><b>".$storeinfo[$k]['MktName']."</b><br>(approx ".$v." miles)</h3>"; 
	$output .= "<p style='margin:0 0 10px 0;padding:0'>".$storeinfo[$k]['LocAddSt']."<br>"; 
	$output .= $storeinfo[$k]['LocAddCity'].", ".$storeinfo[$k]['LocAddState']." ".$storeinfo[$k]['zipcode']."</p>";
}
 print_r($output);           
}

Thanks

With that it showed only the ones within the radius, but continued to show the last one numerous times, so I just put the print_r inside the if statement

foreach ($stores as $k=>$v) {
if ($v<$r){
	$output = "<h3 style='margin:0;padding:0'><b>".$storeinfo[$k]['MktName']."</b><br>(approx ".$v." miles)</h3>"; 
	$output .= "<p style='margin:0 0 10px 0;padding:0'>".$storeinfo[$k]['LocAddSt']."<br>"; 
	$output .= $storeinfo[$k]['LocAddCity'].", ".$storeinfo[$k]['LocAddState']." ".$storeinfo[$k]['zipcode']."</p>";
               print_r($output); 
}
           
}

 

And it works perfect now, thank you

Actually I would think you'd want it outside the foreach loop, but whatever works.

foreach ($stores as $k=>$v) {

if ($v<$r){

$output = "<h3 style='margin:0;padding:0'><b>".$storeinfo[$k]['MktName']."</b><br>(approx ".$v." miles)</h3>";

$output .= "<p style='margin:0 0 10px 0;padding:0'>".$storeinfo[$k]['LocAddSt']."<br>";

$output .= $storeinfo[$k]['LocAddCity'].", ".$storeinfo[$k]['LocAddState']." ".$storeinfo[$k]['zipcode']."</p>";

}

}

              print_r($output);

Looking at this again, because you're looping with the foreach, you should set the $output first then add to it in the loop then echo it after.

$output = "";
foreach ($stores as $k=>$v) {
   if ($v<$r){
      $output .= "<h3 style='margin:0;padding:0'><b>".$storeinfo[$k]['MktName']."</b><br>(approx ".$v." miles)</h3>";
      $output .= "<p style='margin:0 0 10px 0;padding:0'>".$storeinfo[$k]['LocAddSt']."<br>";
      $output .= $storeinfo[$k]['LocAddCity'].", ".$storeinfo[$k]['LocAddState']." ".$storeinfo[$k]['zipcode']."</p>";
   }
}
echo "$output";

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.