'Pagination in PHP with N number of elements per page
I need a little bit of help.
I have an XML file as a database. From it, I need to create a pagination with 25 elements per page with Next and Previous buttons, but with my skills I'm stuck at it.
At the moment, I've written this code, but every page it reset the ID from the database to 0, so it doesn't work.
<?php
$xml = simplexml_load_file("SITE_URL/database_gamecard.xml");
$ids = array();
$count = 0;
foreach($xml->gamecard as $id) {
$ids[] = $id;
}
//Order IDs by url names
//usort ($ids, function($a, $b) {
//$url = $id->url;
//return strcmp($a[$url], $b[$url]);
//});
function PopulatePage($id) {
$url = $id->url;
$img = $id->img;
echo '<a class="darken" href="' . $url . '"><img width="320" height="240" src="' . $img . '"></a>';
}
function StopCount() {
$page = intval($_GET['page']);
$pageurl = "SITE_URL/list.php?page=";
$count = 0;
$page = $page + 1;
$newurl = $pageurl . $page;
echo "<a href='" . $newurl . "'>Next</a>";
}
//Test generating pages
foreach ($ids as $id) {
$count++;
if($page == 1) {
PopulatePage($id);
if($count == 25) {
StopCount();
break;
}
}
if($page == 2) {
PopulatePage($id);
if($count == 25) {
StopCount();
break;
}
}
}
?>
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
