I’m quite in a pickle with a code I need to generate a page. I’ll try to be as simple as possible to make everyone understands what I need to do.
I have a database of N numbers of elements as an XML file. From that file I need to generate a multipage. I need to have a maximum of 25 elements per page with Next and Previous buttons, but at the moment I’m stuck with generating functions to make it work.
For the moment I’ve done some coding to count how many items are in the XML array and generating the increments of 25, but I can’t go any further with my skills. I’ll leave here the code I’ve written.
'''
$xml = simplexml_load_file("SITE_URL/database.xml");
$page = intval($_GET['page']);
$id = 0;
$count = 0;
$ids = array();
$idI = 0;
$idIArray = array();
foreach($xml->gamecard as $id) {
$ids[] = $id;
}
//Count IDs and Pages
$count = count($ids) - 1;
$pages = $count / 25;
$pages = ceil($pages);
function StopCount() {
$pageurl = "SITE_URL/list.php?page=";
if ($page <= $pages) {
$page++;
$newurl = $pageurl . $page;
echo "<a href='" . $newurl . "'>Next</a>";
} else {
//null
}
}
function PopulatePage($id) {
$url = $id->url;
$img = $id->img;
echo '<a class="darken" href="' . $url . '"><img width="320" height="240" src="' . $img . '"></a>';
}
function CountIdForPage($idCount, $count, $idIArray) {
$idI = (($idI + 25) * $idCount) - 1;
if ($idI > $count) {
$idI = $count;
}
array_push($idIArray, $idI);
return $idIArray;
}
function ExplodeIdI($idIArray) {
//TO DO
}
//Elaborate value for generating subsequential IDs number
foreach (range(1, $pages) as $idCount) {
$idIArray = CountIdForPage($idCount, $count, $idIArray);
}
//Testing
print_r($idIArray);
'''
If someone could help, I’ll be really gratefull!