• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Removing spaces from Postal Codes

#1
[eluser]Unknown[/eluser]
Hi everyone,

I hope someone can help me, I've inherited a system that is using the Google Maps Distance Matrix to search for addresses near to the entered code.

It was written to only work with us zip codes and I am trying to convert it to work with any zip or postal code.

I've discovered through playing that basically it is the spaces in the code stored in the database and then code entered on the form that cause it to break.

So here's how it works (I didn't write this code so I'm not 100% sure)

You enter a zip code like 91320 and it goes and gets all the results from a table, it then sends the 91320 to google as the origin zip and it loops through the zipcode field of the database sending it as the destination, working on distances and then returning a result if the distance is less than 150 miles.

(that's probably not the most efficient way of doing it however I'm not sure how else to do this)

However if you enter a postcode like EH1 1EE it repeats the process however the space gets it stuck and it can't continue and results in nothing being returned.

So I tried changing a database post code entry to eh11ee and search for eh21ee and it does correctly return the distance from google and outputs the distance in miles as it should.

But I don't know how to get the controller to remove the spaces from the zipcode field in the database when it finds a space, and how to remove the space from the input form when it finds it.

So here is my controller:

Code:
function listuserszipcode() {
$this->load->model('distance_model');
  $users = $this->distance_model->get_users();

  $matched_users = array();

  foreach ($users as $row) {
   $teacherzip = $row->zipCode1;

   if($teacherzip != "") {
    $distance = $this->get_distance($this->input->post('zip'), $teacherzip);
    if($distance['number'] < 1500) {
     $matched_users[$row->ID]['data'] = $row;
     $matched_users[$row->ID]['distance'] = $distance['display'];
    
    }
   }
  }
  
  $data['matched_users'] = $matched_users;
  $this->load->view('templates-old/header');
  $this->load->view('directory/distance_results.php', $data);
  $this->load->view('templates-old/footer');
  
  
  
}

function get_distance($z1, $z2) {

  $url = 'http://maps.googleapis.com/maps/api/distancematrix/json?origins=' . $z1 . '&destinations;=' . $z2 . '&sensor=false&units=imperial';
  
  $json = @file_get_contents($url);
  
  $json_o = json_decode($json, true);

  if(sizeof($json_o['rows']) == 1 && sizeof($json_o['rows'][0]['elements']) == 1 && isset($json_o['rows'][0]['elements'][0]['distance']) && isset($json_o['rows'][0]['elements'][0]['distance']['text'])) {

   $data = array(
    "display"  => $json_o['rows'][0]['elements'][0]['distance']['text'],
    "number"  => floatval(str_replace(',','',substr($json_o['rows'][0]['elements'][0]['distance']['text'], 0))),
   );
  

  }  else {
   $data = array(
    "display"  => "200",
    "number"  => 200,
   );  
  }
  
  print_r($data);
  echo($url);
  echo($json);
  return $data;
}

here is the model

Code:
function get_users() {
  $query = $this->db->get('teachersprofiles');
  
  return $query->result();
}


}

and this is the view
Code:
&lt;?php foreach ($matched_users as $row) : ?&gt;
       &lt;?php if(isset($row["data"])) : ?&gt;
       <tr>
        <td>
         &lt;?php echo $row["data"]->firstName . ' ' . $row["data"]->lastName; ?&gt;  <br/>
        </td>
        <td>
         &lt;?php echo $row["data"]->certifiedTeacherLevel; ?&gt;
        </td>
        <td>
         &lt;?php echo $row["data"]->studioName; ?&gt;  <br />
         &lt;?php echo $row["data"]->publicAddress1; ?&gt;  <br />
         &lt;?php echo $row["data"]->publicAddress2; ?&gt;  <br />
         &lt;?php echo $row["data"]->publicAddress3; ?&gt;  <br />
         &lt;?php echo $row["data"]->townCity; ?&gt;<br />
         &lt;?php echo $row["data"]->publicAddress4; ?&gt;
        
         <a href="mailto:&lt;?php echo $row[">email; ?&gt;">&lt;?php echo $row["data"]->email; ?&gt;</a>
        
         &lt;?php echo $row["distance"] ?&gt;
        
       &lt;?php endif; ?&gt;
      &lt;?php endforeach; ?&gt;

Can anyone help with the removing of spaces and suggest whether this is a good way of doing this or not, as I said I didn't write this code I'm just left to fix it and make it work.

I also have the added annoyance that I need it to search against two zip code fields so I have no idea how to try that either :-(

Any help is most appreciated!

Tom

#2
[eluser]CroNiX[/eluser]
probably the easiest is to just use str_replace() and remove any spaces from the 2 zipcode vars
Code:
function get_distance($z1, $z2) {
  $z1 = str_replace(' ', '', $z1);
  $z2 = str_replace(' ', '', $z2);
  //...rest the same


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.