Jump to content

[SOLVED] Trouble splitting an address into two fields (works on most records)


richardw

Recommended Posts

My goal is to split one address field that has Street Number and Street into two fields.

 

case type 1 address: 102 Bowen St

might return "102 B" as the Street number and the Street name as  "owen St"

 

case type 2 address: 102-112 Reservoir ave

might return "102" as the Street number and the Street name as  "-112 Reservoir ave"

or  1022-1024 Hope St  might return "1022-10" as the Street number and the Street name as "24 Hope St"

 

Most of the records process fine, I am perplexed as to why a few records to not seperate correctly.

 

<?php
$sql = "SELECT * FROM sidewalks WHERE street_nmbr = '' ";  // so as to only select the out of format records
$result = mysql_query($sql);
while ($row = mysql_fetch_assoc($result)) {
			$id = $row["id"];
		$str_len = $row["street"];
		 $i = 0;
	    for ($i = 0; $i < $str_len; $i++) {
   $current_char = substr($str,$i,1);
   	if ($current_char == ' ') {
	    $cutlength = $i;
		break;
		} else {
		}
		$st_nmbr = (substr($row["street"], 0, $cutlength));
		$st_name = (substr($row["street"], $cutlength, $str_len));
		//  when it's working I will update the records
		//  mysql_query("UPDATE sidewalks SET nmbr1 = '$st_nmbr', streetn2 =  '$st_name' WHERE id =$id");  
echo " Street Number: ", $st_nmbr; 
echo " -  Street ", $st_name;
?>

 

Any help will be greatly appreciated.  :)

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.