Jump to content

display results according to distance


samusk

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/73098-display-results-according-to-distance/
Share on other sites

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.

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.