PHP XML reversed array sizeof() and count() returns incorrect values -


i've been trying figure out why code, simple is, not work way want to.

the problem i'm having getting correct values when trying check sizeof() or count() on reversed simplexml array. i'm making comments form stores comments comments.xml, reads 5 newest comments , lists them newest on top, oldest on bottom.

what have inside comments.xml:

<root>  <entry>   <name>admin</name>   <comment>some nice comment</comment>   <postedon>07.07.2013</postedon>   <postedby>***.***.***.***</postedby>  </entry> </root> 

what have inside .php:

<?php $xml = simplexml_load_file("comments.xml");  $reversearray = (array) $xml; $reversearray = array_reverse($reversearray["entry"]); $limit = sizeof($reversearray); //$limit = count($reversearray);  if($limit > 5){ $limit = 5; }  ($i = 0 ; $i < $limit; $i++){     echo "<div class='panel'>";     echo "<span style='float: right;'>" . $reversearray[$i]->postedon . "</span>";     echo "<span style='float: left;'>" . $reversearray[$i]->name . "</span>";     echo "<hr>";     echo $reversearray[$i]->comment;     echo "<br></div>"; }  ?> 

now problem is, when use 1 entry inside comments.xml, not read it, , prints nothing on page. whenever add entry, shows them both.

i tried add "no comments yet."-code before $limit-check:

if($limit == 0){ echo "<div class='panel'>no comments. :(</div>";} 

and visible until second comment posted.

i hope me out this, running out of ideas.

edit: tried run same code without reversing array , seems working fine.

i managed build workaround since sizeof()/count() did not work array_reverse, , here looks now:

<?php $xmlfile = simplexml_load_file("comments.xml"); $limit = count($xmlfile->entry);  if($limit == 0){ echo "<div class='panel'>no comments. :(</div>";}  if($limit == 1){     echo "<div class='panel'>";     echo "<span style='float: right;'>" . $xmlfile->entry[0]->postedon . "</span>";     echo "<span style='float: left;'>" . $xmlfile->entry[0]->name . "</span>";     echo "<hr>";     echo $xmlfile->entry[0]->comment;     echo "<br></div>";   }else{     $reversearray = (array) $xmlfile;     $reversearray = array_reverse($reversearray["entry"]);      if($limit > 5){ $limit = 5; }      ($i = 0 ; $i < $limit; $i++){         echo "<div class='panel'>";         echo "<span style='float: right;'>" . $reversearray[$i]->postedon . "</span>";         echo "<span style='float: left;'>" . $reversearray[$i]->name . "</span>";         echo "<hr>";         echo $reversearray[$i]->comment;         echo "<br></div>";     } } ?> 

so had make process single entry non-reversed. hope helps others struggle this!


Comments

Popular posts from this blog

javascript - How to get current YouTube IDs via iMacros? -

c# - Maintaining a program folder in program files out of date? -

emulation - Android map show my location didn't work -