Add another page if there is 5 or more rows
I've created a table that is being populated by data from an XML file using SimpleXML function of PHP. What I would like to do is to add paging capability wherein if the rows becomes 5 or more then page 2 will be and so开发者_如何学C on. How to do that?
Given XML:
<root>
<item page="1" title="title 1" />
<item page="2" title="title 2" />
<item page="3" title="title 3" />
<item page="4" title="title 4" />
<item page="5" title="title 5" />
<item page="6" title="title 6" />
<item page="7" title="title 7" />
<item page="8" title="title 8" />
<item page="9" title="title 9" />
<item page="10" title="title 10" />
<item page="11" title="title 11" />
</root>
PHP:
<?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/>';
}
}
?>
Then you can access using yourpage.php?page=2 etc etc.
Have a GET parameter indicating the current page. If it's not set, default to 0. Then, only show elements from currentPage * 5
to (currentPage * 5) + 5
. To link to your pages, link to the same document but with the GET parameter set, ie: results.php?page=2
.
精彩评论