-
pagination and xml
I found a script that looks fairly easy to implement...I just have two problems with it.
Code:
<?php
$xml = simplexml_load_file('yourxml.xml');
$limit = 5;
$page = $_GET['page'];
foreach ( $xml->item as $item ) {
if ( empty($page) ) {
if ( $item->attributes()->page > 0 && $item->attributes()->page <= $page+$limit )
echo $item->attributes()->title,'<br/>';
} else {
if ( $item->attributes()->page > ($page-1)*$limit && $item->attributes()->page <= (($page-1)*$limit)+$limit )
echo $item->attributes()->title,'<br/>';
}
}
?>
the xml that goes with it is
Code:
<root>
<item page="1" title="title 1" />
</root>
My first problem is that my xml does not have a page# or ID (but I can add that)
Code:
<sayings>
<message>
<id></id> //can add this
<vdate></vdate>
<msgpost></msgpost>
</message>
</sayings>
my big problem is that the pagination script does a foreach loop going from 1 to the end, while my script posts the data from last to first. How can I put them together?
Code:
$i = count($xml->messages)-1;
for($x=$i;$x>=0;$x--)
{ //reformat date
$string = $xml->messages[$x]->vdate;
$stringArray = explode("/", $string);
$mydate = mktime(0,0,0,$stringArray[0],$stringArray[1],$stringArray[2]);
$converteddate = date("F j, Y", $mydate);
//write to page
echo
$converteddate.'<br /><p>'
.$xml->messages[$x]->msgpost.'<hr></p>';
}
?>
Thanks
-
I'm getting closer...
What I have gives me the 5 most recent posts.
Code:
//read from bev.xml
$file = 'xml/bev.xml';
$xml = simplexml_load_file($file);
$limit = 5;
$page = $_GET['page'];
$c = 1;
$i = count($xml->messages)-1;
for($x=$i;$x>=0;$x--)
if (empty($page) )
{
if ($c > 0 && $c <= $limit)
{
//write to page
echo
$xml->messages[$x]->$vdate.'<br /><p>'
.$xml->messages[$x]->msgpost.'<hr></p>';
$c++;
}
}
I now need to know how to create the next page.
Am I even going in the right direction?