hi i am wanting to know how many <items> there is cant seem to do it im new to php thought about counting them maybe putting them in a array or somthing also tried to loop it through and if it didnt fine one break the loop and then i have the number

		$xml=("popular.xml");


	$xmlDoc = new DOMDocument();
	$xmlDoc->load($xml);


	$x=$xmlDoc->getElementsByTagName('item');

for ($i=0;  ; $i++)
	{

	$item_title=$x->item($i)->getElementsByTagName('title')
	->item(0)->childNodes->item(0)->nodeValue;

	if ($item_title == "") 
	{
    break;
	}
}

echo $i;

so echo $i should have the number because if theres no title then it breaks ?

anyone know why it doesnt work

Fatal error: Call to a member function getElementsByTagName() on a non-object in C:\xampp\htdocs\Yavee\test.php on line 15
    10 days later
    11 days later
    14 days later

    Try changing this:

    for ($i=0;  ; $i++)

    to this:

    for ($i=0; $i < $x->length; $i++)

    The DOMNodeList object has a $length property (manual page: [man]domnodelist[/man]).

    Alternatively, just don't use a for() loop - do a [man]foreach/man on the node list instead.

      why wouldnt you just use the simpleXML methods?

      
      $file = simplexml_load_file('popular.xml');
      
      $xml = new SimpleXMLElement($file);
      
      echo count($xml->item);
      
        Write a Reply...