php - Parsing XML Fail -


i have old site uses xml path, reads , shows desired content.

i used create new page, different xml-url.

<?xml version="1.0" encoding="utf-8" ?> <items>   <item>     <link>http://www.url.to/image.html</link>     <itemname>teapot</itemname>     <city>munich</city>     <countryid>de</countryid>     <desc>       <![cdata[here comes item description]]>     </desc>     <imageurl>http://www.url.to/image.jpg</imageurl>   </item> </items> 

thats how items created. plain , simple.

the php code generates site looks simple this:

<?php     //###################config start#########################     $maxanzahl = 50;     $zaehler = 4;     //###################config end#########################         $feed = simplexml_load_file('http://www.domain.tld/path-to-xml.php&filter1=rule1&filter2=rule2');         $items = $feed->items;         foreach ($items->item $item) {         if ($item->city == "munich") {         echo "<div class='4u'>";         echo "<article class='box style2'>";         echo "<a href='".link."' class='image featured' target='_blank'><img src='".imageurl."' alt=".itemname." /> </a>";         echo "<h3><a href='".link."'>".itemname."</a></h3>";         echo "</article>";         echo "</div>";         }         $zaehler = $zaehler + 1;         if ($zaehler == $maxanzahl) {             break;         }         } ?> 

as see should fine. still, when try load page, tells me warning: invalid argument supplied foreach() in /home/user/public_html/testfolder/index.php on line 31 while line 31 foreach ($items->item $item) {

looks broke original skript @ place, used winmerge compare both files, , differences there kicked variables out of config part, of no use here anymore, different 'tags' needs looking inside xml url.

would able make out of it?

kindly caylean

you must use

$feed = simplexml_load_file('http://www.domain.tld/path-to-xml.php&filter1=rule1&filter2=rule2'); foreach($feed->item $item){ 

root element items not exist in $feed. u must check if $feed->item array or not.

example:

<?xml version="1.0" encoding="utf-8" ?> <items>   <item>     <link>http://www.url.to/image.html</link>     <itemname>teapot</itemname>     <city>munich</city>     <countryid>de</countryid>     <desc>       <![cdata[here comes item description]]>     </desc>     <imageurl>http://www.url.to/image.jpg</imageurl>   </item>   <item>     <link>http://www.url.to/image.html</link>     <itemname>teapot</itemname>     <city>munich</city>     <countryid>de</countryid>     <desc>       <![cdata[here comes item description]]>     </desc>     <imageurl>http://www.url.to/image.jpg</imageurl>   </item> </items> 

right $feed->item simplexmlelement.

in example:

<?xml version="1.0" encoding="utf-8" ?> <items>   <item>     <link>http://www.url.to/image.html</link>     <itemname>teapot</itemname>     <city>munich</city>     <countryid>de</countryid>     <desc>       <![cdata[here comes item description]]>     </desc>     <imageurl>http://www.url.to/image.jpg</imageurl>   </item> </items> 

$feed->item array or 2 simplexmlelements.


Comments

Popular posts from this blog

php - Zend Framework / Skeleton-Application / Composer install issue -

c# - Better 64-bit byte array hash -

python - PyCharm Type error Message -