Jump to content

Foreach Error


jbradley04

Recommended Posts

So I am running a script in a crontab and I keep getting an error.

The File still executes but it errors out in my log.

The error says:

 

PHP Warning: Invalid argument supplied for foreach() in

/var/www/clients/client1/web/for/PF.php on line 20

 

 

Basically, what I am doing is taking a DB of Addresses that I have and confirming that the zipcodes are correct, if not I am updating them according to Google.

 

The code I have is this:

 

///// CHECK ADDRESSES FROM GOOGLE ///////
$sql = "SELECT * FROM $table2 WHERE `checked` <> 2 LIMIT $limit";
$result = mysql_query($sql);

while ($row = mysql_fetch_array($result))
{
$id = $row['id'];
$street = $row['St'];
$city = $row['City'];
$state = $row['State'];
$oldzip = $row['Zip'];
$address=urlencode($street.",".$city.",".$state);

$gsend = file_get_contents("http://maps.googleapis.com/maps/api/geocode/json?address=".$address."&sensor=true");
$json = json_decode($gsend);
$postal = FALSE;

foreach( $json->results[0]->address_components as $component )
{
if ( in_array('postal_code', $component->types) )
$postal = $component->long_name;
if ( $postal )
{
if ($oldzip != $postal)
{
mysql_query("UPDATE $table2 SET `updated` = 2, `Zip_Code`='$postal' WHERE `id` = '$id'");
}
else
{
mysql_query("UPDATE $table2 SET `updated` = 1 WHERE `id` = '$id'");
}
}
}
mysql_query("UPDATE $table2 SET `checked` = 2 WHERE `id` = '$id'");
}

 

Line 20 is the foreach command line.

Any help would be greatly appreciated!

Thanks

J

Link to comment
Share on other sites

Obviously $json->results[0]->address_components is not an array. I can think of a few problems to look at:

 

1) $result does not contain the values you think it does (i.e. 'id', 'St', 'City', etc.)

 

2) $address is not formatted the way it needs to be

 

3) file_get_contents() is failing. Check that the address you are specifying is correct and that file_get_contents() can retrieve those contents

 

4) The results of json_decode($gsend) does not contain the array you think at $json->results[0]->address_components

 

Check all of the above and I'm sure you'll find the error

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

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

Guest
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.