samusk Posted October 13, 2007 Share Posted October 13, 2007 I have a php/mysql driven website. On my search bar I have the option of searching by zip code. I have the function to find the radius of the surrounding zip codes. But I want to display the result along with my information from the other table. An example would be: I want to search for ads around my zip code for a range of 20 miles. The class I have will find all the zips and get their distances plus will sort them by closest to farthest. My problem is I want to display the records according to the distance provided. I heard of doing a sort with php but I am not sure which route to go. I hope someone has found out how to do it, because this is the only thing left to do in order to launch the site. Quote Link to comment https://forums.phpfreaks.com/topic/73098-display-results-according-to-distance/ Share on other sites More sharing options...
redarrow Posted October 13, 2007 Share Posted October 13, 2007 Why not use a database join and dispaly all the results in it correct place.... Quote Link to comment https://forums.phpfreaks.com/topic/73098-display-results-according-to-distance/#findComment-368647 Share on other sites More sharing options...
cmgmyr Posted October 13, 2007 Share Posted October 13, 2007 I had to do the same type of thing. What I did was enter in all of the values into an array (name, address, phone, zip, distance). Then I re-sorted the array by distance then outputted the information. Quote Link to comment https://forums.phpfreaks.com/topic/73098-display-results-according-to-distance/#findComment-368648 Share on other sites More sharing options...
samusk Posted October 13, 2007 Author Share Posted October 13, 2007 I do have a database with the zip, lat, long. what kind of sort should I use to do this? I put all the information into an array like you did, but I want to be sure this will work right. Explain what kind of sort you did cmgmyr. Quote Link to comment https://forums.phpfreaks.com/topic/73098-display-results-according-to-distance/#findComment-368652 Share on other sites More sharing options...
cmgmyr Posted October 13, 2007 Share Posted October 13, 2007 Here is my code that I used: <?php $end = ''; $zip_from = $_SESSION['zipcode']; $zips = $z->get_zips_in_range($zip_from, $_SESSION['radius']); $zip = ''; $x = 1; $count_zips = count($zips); if($count_zips > 0){ $zip = "zip = $zip_from OR "; } foreach ($zips as $key => $value) { $zip .= "zip = $key"; if($x < $count_zips){ $zip .= " OR "; }else{ $zip .= " "; } $x++; } $end .= $zip; $sql = "SELECT * FROM dealers WHERE access = '1' AND search = '1' AND ($end) ORDER BY cname ASC"; $result = $db->query($sql); $numrows = $db->numRows($result); if($numrows > 0){ $dealers = array(); while($row = $db->fetch($result)){ $cname = $row['cname']; $address = $row['address']; $city = $row['city']; $state = $row['state']; $zip = $row['zip']; $phone = $row['phone']; $url = $row['url']; if(strlen($url) > 0){ $url = $func->convertURL($url); $url = "<a href=\"$url\" target=\"_blank\">Visit their Web site</a><br />"; } $map = "$address $city, $state $zip"; $map = str_replace(' ', '+', $map); $miles = $z->get_distance($zip_from, $zip); $address = "$address <br />$city, $state $zip<br />$phone"; $dealers[] = array("name" => $cname, "address" => $address, "map" => $map, "miles" => $miles, "url" => $url, "zip" => $zip); } //Get dealers in the selected zip and sort out others $dealers1 = array(); $dealers2 = array(); $count = count($dealers); for($x=0;$x<$count;$x++){ $name = $dealers[$x]['name']; $address = $dealers[$x]['address']; $map = $dealers[$x]['map']; $miles = $dealers[$x]['miles']; $url = $dealers[$x]['url']; $zip = $dealers[$x]['zip']; if($_SESSION['zipcode'] == $zip){ $dealers1[] = array("name" => $name, "address" => $address, "map" => $map, "miles" => $miles, "url" => $url, "zip" => $zip); }else{ $dealers2[] = array("name" => $name, "address" => $address, "map" => $map, "miles" => $miles, "url" => $url, "zip" => $zip); } } $dealers2 = $func->sortMultiArray($dealers2, 'miles'); $dealers = array(); $dealers = array_merge($dealers1, $dealers2); } ?> And how to sort a multi-dimentional array <?php function sortMultiArray($array, $index, $order='asc', $natsort=FALSE, $case_sensitive=FALSE){ if(is_array($array) && count($array)>0) { foreach(array_keys($array) as $key) $temp[$key]=$array[$key][$index]; if(!$natsort) ($order=='asc')? asort($temp) : arsort($temp); else { ($case_sensitive)? natsort($temp) : natcasesort($temp); if($order!='asc') $temp=array_reverse($temp,TRUE); } foreach(array_keys($temp) as $key) (is_numeric($key))? $sorted[]=$array[$key] : $sorted[$key]=$array[$key]; return $sorted; } return $array; } ?> $dealers1 are the records where the zip code matches so the distance would be 0 miles. $dealers2 are the records where it needs to sort by distance. I hope this helps you out. Quote Link to comment https://forums.phpfreaks.com/topic/73098-display-results-according-to-distance/#findComment-368655 Share on other sites More sharing options...
samusk Posted October 13, 2007 Author Share Posted October 13, 2007 thanks for the help. I'm going to try it out and see what it looks like. Quote Link to comment https://forums.phpfreaks.com/topic/73098-display-results-according-to-distance/#findComment-368664 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.