0

I am trying to create a for loop that will parse out different elements of xml doc using php. It parses when I put the number of the entry in the line:

$s = $xml->entry[0]->children($namespaces['s']); 

However I tried to replace the number with "i" to count through each of the entries in the xml doc and I get an error (Fatal error: Call to a member function children() on a non-object in /home/content/c/a/s/cashme/html/buylooper/xml.php on line 10). How do I resolve this?

<?php
$url = 'xml-file.xml'; 
$xml = simplexml_load_file($url); 
$namespaces = $xml->entry->getNameSpaces(true); 

for ($i = 0; $i <= 10; $i += 1){

$s = $xml->entry[i]->children($namespaces['s']); 
$title = $s->product->title; 

//print 
echo $retailer;

}
?>
2
  • that means that $xml->entry[i] doesn't exist for some value of i. Commented Jan 28, 2012 at 23:53
  • @Ben: Post that as an answer so sharataka can accept it. Commented Jan 29, 2012 at 0:13

1 Answer 1

2

You need to use $i, instead of i.

As a side note:

This bug should become very obvious on a development machine with:

you would get a warning like Use of undefined constant i - assumed 'i'

(unless you have defined i as a constant somewhere but that's an odd constant name)

Sign up to request clarification or add additional context in comments.

Comments

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.