Click to See Complete Forum and Search --> : reading XML in reverse order
deathfist
01-24-2008, 01:28 AM
hi, i am making a guestbook section and i'm using php 5 and xml. i'm using the xml file to store the entries on the guestbook. but i have this problem in reading the xml file. i want the file to be read from the last entry to the first. here is the code for calling the xml:
$doc = new DOMDocument();
$doc->load( 'messages.xml' );
$messages = $doc->getElementsByTagName( "message" );
$num = count($messages);
echo $num;
foreach( $messages as $message )
{
$names = $message->getElementsByTagName( "name" );
$name = $names->item(0)->nodeValue;
$texts = $message->getElementsByTagName( "text" );
$text = $texts->item(0)->nodeValue;
$dates = $message->getElementsByTagName( "gdate" );
$date = $dates->item(0)->nodeValue;
echo "<tr> <td>$name<br />$date<br />$text<br /></td></tr>";
}
is there a way to make the foreach function to be read in reverse? thanks in advance!
You may try the array_reverse() php array function:
...
foreach( array_reverse($messages) as $message )
{
...
deathfist
01-24-2008, 05:01 AM
tried it but it gives me this error:
Warning: array_reverse() [function.array-reverse]: The argument should be an array in C:\~act.php on line 175
Warning: Invalid argument supplied for foreach() in C:\~act.php on line 175
:(
Hmm... what about preserving the keys?
foreach( array_reverse($messages,true) as $message )
deathfist
01-24-2008, 11:52 AM
i also receive the same message:
Warning: array_reverse() [function.array-reverse]: The argument should be an array in C:\~1\contact.php on line 178
Warning: Invalid argument supplied for foreach() in C:\~1\contact.php on line 178
I don't know what to say... as I am not a php devoted coder... what if we would move or make a copy of this thread to the php Forum?
jkmyoung
01-24-2008, 01:40 PM
Why not just use a reverse for loop instead of a for-each?
for ($i = $num - 1; $i >= 0; i++)
...
$message = $messages[$i];
Shouldn't be?
for ($i = $num - 1; $i >= 0; i--)
jkmyoung
01-24-2008, 03:59 PM
Ah true say! I'd forgotten about that.
deathfist
01-24-2008, 09:49 PM
i have tried that also. anyway,thanks for the replay guys. maybe you are right, i'll post this on the php thread. thanks!
scragar
01-24-2008, 10:08 PM
$messages = $doc->getElementsByTagName( "message" );
$num = $messages->length;
echo $num;
for ($i = $num-1; $i >= 0; $i--){
$message = $messages->item($i);
$names = $message->getElementsByTagName( "name" );
$name = $names->item(0)->nodeValue;
$texts = $message->getElementsByTagName( "text" );
$text = $texts->item(0)->nodeValue;
$dates = $message->getElementsByTagName( "gdate" );
$date = $dates->item(0)->nodeValue;
echo "<tr> <td>$name<br />$date<br />$text<br /></td></tr>";
}
untested.
deathfist
01-27-2008, 06:25 AM
gee thanks it worked!! this line made the differece:
$message = $messages->item($i);
:D
lionMUC
01-24-2012, 05:25 AM
I know this thread is dead old but I stumbled across it, so others may too in future.
Nice solution nowadays:
$messages = array_reverse($doc->xpath('message'));
foreach($messages as $message){
...
}