php - Trouble loading SimpleXML object for use in foreach loop : Warning: Invalid argument supplied for foreach() -
i'm trying load simple xml object file using simplexml_load_file();
i've been scraping around internet trying handle on error with subsequent foreach loop.
i'm getting invalid foreach argument error:
warning: invalid argument supplied foreach() in c:hosting\bnb\assets\reviews.php on line 2
if try different php logic handling xml object, can different errors, foreach argument has never shown positive, though var_dump has no trouble loading till end of file.
some other errors have been :
parse error: syntax error, unexpected t_foreach in server_path on line 646 or : warning: invalid argument supplied foreach() in server_path on line 648 parse error: syntax error, unexpected '}' in c:hosting\bnb\index.php on line 658
php :
<?php $xml = simplexml_load_file('reviews.xml'); ?> <?php foreach ($xml->bnb->review $n_review): ?> <ul> <li> <div class="avatar"><a href="#"><img src="img/svg/audio.svg" alt=""></a></div> <div class="comment_right clearfix"> <div class="comment_info"> <a href="#"> <?php echo $n_review->location;?></a><span>|</span> <?php echo $n_review->date;?> </div> <p> <?php echo $n_review->quote; ?> </p> </div> </li> </ul> <?php endforeach; ?>
some people mention not using http (as can blocked server settings), or preparing string memory control abstraction can :
//$feed = file_get_contents('absolute_url'); //$items = simplexml_load_string($feed);
somebody else mentioned people may have php version doesn't support anonymous functions. ever necessary prepare new array() or new simplexmlelement()
requested edit: xml :
<bnb> <review> <quote></quote> <location></location> <date></date> </review> <review> <quote></quote> <location></location> <date></date> </review> </bnb>
1. $xml
okay?
if var_dump($xml) giving this:
object(simplexmlelement)#1 (1) { ...
then have loaded xml $xml
.
2. incorrect path in foreach
statement:
$xml
representing root node <bnb>
. statement $xml->bnb->review
translates /bnb/bnb/review
, doesn't exist in xml , hence error-message.
here's right path:
foreach ($xml->review $n_review) { // (...) }
see in action: https://eval.in/510189
Comments
Post a Comment