xml_parse()


#1

Hi, I have the the following code, but somehow it doesnt function, i keep getting this error.

Warning: Invalid argument supplied for foreach() in

here is my code, can someone see something wrong with it.
[php]

<?php include_once("Artikel.inc"); class Artikelparser{ var $nummer; var $art; var $aktuell; var $titel; var $preis; function Artikelparser($datei){ $this->art= array(); $xmlFile = implode("", file("artikeldaten.xml")); $parser = xml_parser_create(); xml_set_object($parser, $this); xml_set_element_handler($parser, "startElement","endElement"); xml_set_character_data_handler($parser,"cdata"); xml_parse($parser, $xmlFile); xml_parser_free($parser); } function startElement($parser, $name, $atts){ $this->aktuell= $name; if($name == "ARTIKEL"){ $this->nummer= $atts["NUMMER"]; } } function endElement($parser, $name){ if($name == "ARTIKEL"){ $this->art[$this->nummer]= new Artikel($this->titel, $this->preis); } $this->aktuell=""; } function cdata($parser, $text){ if($this->aktuell== "PREIS") $this->preis = $text; if($this->aktuell== "TITEL") $this->titel = $text; } function getArtikelArray(){ return $this->art; } } [/php] [b]Admin Edit: Added [ PHP ] Code tags for readability (please see http://phphelp.com/forums/viewtopic.php?t=2752 for posting guidelines)[/b]

#2

This code has NO foreach statement.

You also cut the error message off at an important part. the IN section. It will tell you WHAT file and WHAT line to look at (as best it can).


#3

Hi,

The warning came from the line with the for each in the following code.
[php]

Folgende B?cher k?nnen Sie bestellen

<?php $art_parser = new Artikelparser($datei); foreach($art as $nummer->$art_objekt){ print ""; print ""; print "
"; print ""; print $art_objekt->name; print " ".$art_objekt->preis." Euron
 
"; } ?> [/php]

#4

I am not sure if the foreach function can be used in that fashion.

Essentially the foreach will assign it’s value (either the contents or the key depending on the use) into the supplied variables. i.e.

[php]
//Will echo out EACH value from the array variable called $Array
foreach($Array as $value) {
echo $value.’
’;
}

//Will echo out EACH key AND value from the array variable called $Array
foreach($Array as $key=>$value) {
echo $key.’ - ‘.$value.’
’;
}
[/php]

When using the later of the 2 methods… to get the KEY and VALUE you must use the => (note the equal sign) where you are using -> (note the dash). That notation is used for accessing objects.

So as I look at YOUR code foreach($art as $nummer->$art_objekt), I would interpret that you wanted to take EACH value in $art and assign it to an object called $nummer->$art_objekt. I do not know if this is allowed. (or even intended).

Also where is the data array from $art coming from? Is it an array?


#5

hi sorry havent been back in a while but the data array is coming from a xml file


#6

now i get this error

Warning: Invalid argument supplied for foreach() in /webhome/10016550/public_html/PHP02/artikel.php on line 11

and here the artikel.php

<?php include("artikellisteneu.php")?> B?cher bein uns

Folgende B?cher k?nnen Sie bestellen

<?php $art_parser = new Artikelparser($datei); foreach($art as $nummer->$art_objekt){ print ""; print ""; print "
"; print ""; print $art_objekt->name; print " ".$art_objekt->preis." Euron
 
"; } ?> Admin Login   adminLogin
Ben?tigt einen Username und Passwort.

#7

Please see http://phphelp.com/forums/viewtopic.php?t=2752 for posting guidelines.

In the forums, plain text LOSES formatting. This makes CODE very hard to read and subsequently DELAYS responses. Many people actually REFUSE to read code that is not formatted in SOME way. The CODE tags will allow you to do just that. It’s akin to the

 tag in HTML and it keeps all formatting. Additionally the added PHP tags provide the same thing as the CODE tags AND will give syntax highlighting. Although there is NOT a button for the PHP tags you can add them manually by typing the OPENING and CLOSING tags [ PHP ] and [ /PHP ] respectively (Without the spaces)

#8

The syntax on foreach is simply wrong.

You used:
[php]foreach( $list as $key->$value )[/php]

While it should be:
[php]foreach( $list as $key=>$value )[/php]

And please, add the php or code tags to your questions.