Code:
$query = "
SELECT
id
,cb_pofficename
,cb_paddress1
,cb_paddress2
,cb_pcity
,cb_pstate
,cb_pzip
,cb_lat1st
,cb_long1st
,cb_sofficename
,cb_saddress1
,cb_saddress2
,cb_scity
,cb_sstate
,cb_szip
,cb_lat2nd
,cb_long2nd
,cb_tofficename
,cb_taddress1
,cb_taddress2
,cb_tcity
,cb_tstate
,cb_tzip
,cb_lat3rd
,cb_long3rd
FROM
`jos_comprofiler` a
WHERE
cb_findinsearch = 'Yes' AND id = [user_id]
";
$rs = mysql_query ( $query );
$arr = array ();
$count = 0;
while ( $obj = mysql_fetch_object ( $rs ) ) {
$arr[]=$obj;
}
foreach($arr as $a){
if(strlen($a->cb_paddress1)>3){
sleep(.3);
$newlatLong=getLatLong($a->cb_paddress1 . ', ' . (strlen ( $a->cb_paddress2 ) ? $a->cb_paddress2.', ' : '') . $a->cb_pcity .', ' . $a->cb_pstate. ',+' . $a->cb_pzip.' USA');
$latitude1 =$newlatLong['lat'];
$longitude1 = $newlatLong['long'];
if($latitude1&&$longitude1 ){
$count++;
$query ="
UPDATE
jos_comprofiler
SET
cb_lat1st ='$latitude1'
,cb_long1st ='$longitude1'
WHERE
id='".$a->id."'
";
$rs = mysql_query ( $query );
echo '<i>ADDRESS UPDATED '.$count.' :</i> <small>'.str_replace ( " ", "+", $a->cb_paddress1) . ',+' . (strlen ( $a->cb_paddress2) ? str_replace ( " ", "+,", $a->cb_paddress2 ) : '') . ',+' .
str_replace ( " ", "+", $a->cb_pcity ) .
',+' . $a->cb_pstate. ',+' . $a->cb_pzip. ',+USA</small> <b>'.$latitude1.':'.$longitude1 .'</b><br/>';
}
}
if(strlen($a->cb_saddress1)>3){
sleep(.3);
$newlatLong=getLatLong($a->cb_saddress1 . ', ' . (strlen ( $a->cb_saddress2 ) ? $a->cb_saddress2.', ' : '') . $a->cb_scity .', ' . $a->cb_sstate. ',+' . $a->cb_szip.' USA');
$latitude2 =$newlatLong['lat'];
$longitude2 = $newlatLong['long'];
if($latitude2&&$longitude2 ){
$count++;
$query ="
UPDATE
jos_comprofiler
SET
cb_lat2nd ='$latitude2'
,cb_long2nd ='$longitude2'
WHERE
id='".$a->id."'
";
$rs = mysql_query ( $query );
echo '<i>ADDRESS UPDATED '.$count.' :</i> <small>'.str_replace ( " ", "+", $a->cb_saddress1 ) . ',+' . (strlen ( $a->cb_saddress2 ) ? str_replace ( " ", "+,", $a->cb_saddress2 ) : '') . ',+' .
str_replace ( " ", "+", $a->cb_scity ) .
',+' . $a->cb_sstate. ',+' . $a->cb_szip. ',+USA</small> <b>'.$latitude2.':'.$longitude2 .'</b><br/>';
}
}
if(strlen($a->cb_taddress1)>3){
sleep(.3);
$newlatLong=getLatLong($a->cb_taddress1 . ', ' . (strlen ( $a->cb_taddress2 ) ? $a->cb_taddress2.', ' : '') . $a->cb_tcity .', ' . $a->cb_tstate. ',+' . $a->cb_tzip.' USA');
$latitude3 =$newlatLong['lat'];
$longitude3 = $newlatLong['long'];
if($latitude3&&$longitude3 ){
$count++;
$query ="
UPDATE
jos_comprofiler
SET
cb_lat3rd ='$latitude3'
,cb_long3rd ='$longitude3'
WHERE
id='".$a->id."'
";
$rs = mysql_query ( $query );
echo '<i>ADDRESS UPDATED '.$count.':</i> <small>'.str_replace ( " ", "+", $a->cb_taddress1 ) . ',+' . (strlen ( $a->cb_taddress2 ) ? str_replace ( " ", "+,", $a->cb_taddress2 ) : '') . ',+' .
str_replace ( " ", "+", $a->cb_tcity ) .
',+' . $a->cb_tstate. ',+' . $a->cb_tzip. ',+USA </small> <b>'.$latitude3.':'.$longitude3 .'</b><br/>';
}
}
}
function getLatLong($address){
if (!is_string($address))die("All Addresses must be passed as a string");
$_url = sprintf('http://maps.google.com/maps?output=js&q=%s',rawurlencode($address));
$_result = false;
if($_result = file_get_contents($_url)) {
if(strpos($_result,'errortips') > 1 || strpos($_result,'Did you mean:') !== false) return false;
preg_match('!center:\s*{lat:\s*(-?\d+\.\d+),lng:\s*(-?\d+\.\d+)}!U', $_result, $_match);
$_coords['lat'] = $_match[1];
$_coords['long'] = $_match[2];
}
return $_coords;
}