0

Hello I am using the NOAA buoy RSS feed to create and insert variables into Mysql. There is a tag entitled:

<georss:point>45.565 -34.123</georss:point>

Which I have broken out as such:

$xmlString = file_get_contents($feed_url); 
$xmlString = str_replace('georss:point','point',$xmlString);  
$xml = new SimpleXMLElement($xmlString); 
$items = $xml->xpath('channel/item'); 
$closeItems = array(); foreach($items as $item) 
{     
$latlng = explode(' ',trim($item->point));
$lat = $latlng[0];
$lng = $latlng[1];  
} 

However the issue that I am running into, the variables for $lat and $lng are the same for all the feed articles. Each article will have a unique $lat and $lng, so I know I am doing something wrong here. Thanks Again,

4
  • Can you post the code that compares or outputs the values of $lat and $lng, as well as any intervening code that may alter their values? Commented Aug 15, 2011 at 3:38
  • Also, do you mean that the two variable values match each other, or that $lat is always one value and $lng is another, but they are the same for every record? Commented Aug 15, 2011 at 3:40
  • Hi George, the first record returned is Ship - 1969-12-31 19:00:00 lat=39.933 lng=-75.142 and the second record is Station 44065 - Entrance To New York Harbor lat=39.933 lng=-75.142 Notice the name is different but the lat and lng variables are the same Commented Aug 15, 2011 at 3:43
  • Replacing the georss namespace is just lazy. SimpleXML is capable of dealing with namespaces, see php.net/manual/en/simplexmlelement.children.php#example-4737 Commented Aug 15, 2011 at 3:56

1 Answer 1

1

The problem is here:

foreach($items as $item) 
{     
    $latlng = explode(' ',trim($item->point));
    $lat = $latlng[0];
    $lng = $latlng[1];  
}

You assign the variables for each record, but never use them inside the loop. Assuming you have more code after the loop, that code will always receive only the last values assigned for $lat and $lng.

To correct the problem, place your record-specific code within the foreach() loop:

foreach($items as $item) 
{     
    $latlng = explode(' ',trim($item->point));
    $lat = $latlng[0];
    $lng = $latlng[1];
    // Do something with $lat and $lng here  
}
Sign up to request clarification or add additional context in comments.

2 Comments

Hi George, doesnt seem to want to work for me. Perhaps I am not understanding correctly. Here is what I placed: foreach($items as $item, $lat as $latlng[0], $lng as $latlng[1])
@matt colley: Please see my updated answer for a description of the place to insert your code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.