Jump to content

display results according to distance


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
Share on other sites

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.


Link to comment
Share on other sites

Here is my code that I used:

$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 ";
        $zip .= " ";

$end .= $zip;

$sql = "SELECT 
            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);
        $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);
            $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

function sortMultiArray($array, $index, $order='asc', $natsort=FALSE, $case_sensitive=FALSE){ 
        if(is_array($array) && count($array)>0)  
           foreach(array_keys($array) as $key)  
                   ($order=='asc')? asort($temp) : arsort($temp); 
                 ($case_sensitive)? natsort($temp) : natcasesort($temp); 
           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.

Link to comment
Share on other sites

This thread is more than a year old. Are you sure you have something important to add to it?

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

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.