timmah1 Posted April 1, 2012 Share Posted April 1, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/260110-limiting-results-based-on-drop-down-within-foreach-loop/ Share on other sites More sharing options...
Drummin Posted April 1, 2012 Share Posted April 1, 2012 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); } Quote Link to comment https://forums.phpfreaks.com/topic/260110-limiting-results-based-on-drop-down-within-foreach-loop/#findComment-1333170 Share on other sites More sharing options...
timmah1 Posted April 1, 2012 Author Share Posted April 1, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/260110-limiting-results-based-on-drop-down-within-foreach-loop/#findComment-1333172 Share on other sites More sharing options...
Drummin Posted April 1, 2012 Share Posted April 1, 2012 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); Quote Link to comment https://forums.phpfreaks.com/topic/260110-limiting-results-based-on-drop-down-within-foreach-loop/#findComment-1333175 Share on other sites More sharing options...
Drummin Posted April 1, 2012 Share Posted April 1, 2012 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"; Quote Link to comment https://forums.phpfreaks.com/topic/260110-limiting-results-based-on-drop-down-within-foreach-loop/#findComment-1333185 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.