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.

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

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.