XML pagination with PHP

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
QbertsBrother
Forum Commoner
Posts: 58
Joined: Thu Oct 11, 2007 10:12 am

XML pagination with PHP

Post by QbertsBrother »

hello everyone

has anyone done any pagination with PHP using an XML file for the data?

what i have is a PHP page that reads an XML file that contains product inventory and what i would like to do with that is break it up so it only displays 10 items per page as there are hundreds and the page gets really long.

i have done pagination with items pulled from a database but i have no idea where to start with the XML file.

any help would be great.

thanks
hitman6003
Forum Newbie
Posts: 4
Joined: Mon Jun 23, 2008 7:36 pm

Re: XML pagination with PHP

Post by hitman6003 »

Use a for loop that starts at the page number * the number per page:

Code: Select all

$start = $entries_per_page * $page_number;
for ($i = $start; $i < ( $start + $entries_per_page); $i++) {
  print_r($array_of_xml[$i]);
}
 
QbertsBrother
Forum Commoner
Posts: 58
Joined: Thu Oct 11, 2007 10:12 am

Re: XML pagination with PHP

Post by QbertsBrother »

thanks

i tried that and i am still getting all of the results that are in the XML file.

i am probably not doing it correctly. this outputs the all the records that are in the xml file.

Code: Select all

 
$entries_per_page = '1';
if (isset($_GET['pagenum'])) {
$page_number = $_GET['pagenum'];
}
else {
$page_number = 1;
}
 
if (isset($_GET['inventorytype'])) {
$inventorytype = $_GET['inventorytype'];
}
else {
$inventorytype = 'New';
}
 
if ($inventorytype == 'New'){
$inventoryFile = "inventory/newinventory.xml";
}
$start = $entries_per_page * $page_number;
$xml = @simplexml_load_file($inventoryFile) or die ("no file loaded");
$vehicleslist = $xml->listName;
 
foreach ($xml->vehicle as $vehicle) {
//echo "<h1>List: " . $vehicleslist . "</h1>";
for ($i = $start; $i < ( $start + $entries_per_page); $i++) {
 
//foreach ($vehicle->vin as $vehiclevin) {
 
print_r($vehicle[$i]);
//echo "<div class=\"vehiclerow\">
//<div class=\"vehicleimage\"><img src=\"images/vehicles/" . $vehicle->image01 ."\" /></div>
//<div class=\"vehicledesc\">Year: " . $vehicle->year . "<br />Make: " . $vehicle->make . "<br />Model: " . $vehicle->model . "</div>
//</div>";
}
//}
}
 
AlexGomez
Forum Newbie
Posts: 1
Joined: Fri Jun 27, 2008 3:51 am

Re: XML pagination with PHP

Post by AlexGomez »

Just wondering if you managed to solve this problem? I'm facing a similiar situation whereby I wish to split the XML data into multiple pages. Cheers.

Alex
QbertsBrother
Forum Commoner
Posts: 58
Joined: Thu Oct 11, 2007 10:12 am

Re: XML pagination with PHP

Post by QbertsBrother »

i was able to get it to filter the results but when i would go to the next page it would show the same filtered results. i could not get it to show the next result set such as displaying the records 11 - 20. it would always show 1 - 10.

so i had to change how i am doing it and now i am loading the data into a database. i did not have the time to mass around with it now. maybe i will come back to it later.

i hope it works out for you.
Post Reply