2

Below is code I'm using to parse XML file, however file has many records and I want to paginate it, and display 20 records per page.

I also want the pagination links at bottom of page so users can go to other pages as well. It should be something like, if no value is give then it will start from 0 to 20 else if value is 2 start from 40 and stop at 60, test.php?page=2.

$xml = new SimpleXMLElement('xmlfile.xml', 0, true);

foreach ($xml->product as $key => $value) {
    echo "<a href=\"http://www.example.org/test/test1.php?sku={$value->sku}\">$value->name</a>";
    echo "<br>";
}
hakre
  • 193,403
  • 52
  • 435
  • 836

3 Answers3

3

Something like this should work:

<?php
    $startPage = $_GET['page'];
    $perPage = 10;
    $currentRecord = 0;
    $xml = new SimpleXMLElement('xmlfile.xml', 0, true);

      foreach($xml->product as $key => $value)
        {
         $currentRecord += 1;
         if($currentRecord > ($startPage * $perPage) && $currentRecord < ($startPage * $perPage + $perPage)){

        echo "<a href=\"http://www.example.org/test/test1.php?sku={$value->sku}\">$value->name</a>";    

        //echo $value->name;

        echo "<br>";

        }
        }
//and the pagination:
        for ($i = 1; $i <= ($currentRecord / $perPage); $i++) {
           echo("<a href='thispage.php?page=".$i."'>".$i."</a>");
        } ?>
Phil
  • 668
  • 7
  • 13
  • Exactly the same thing worked, Thank you so much. @Phil – Khushwant Singh Mar 29 '13 at 18:09
  • @Phil very sad that you did the homework for him and didn't let him learn applying the pagination concept by himself. – stefreak Mar 29 '13 at 18:14
  • @stefreak thanks for your intention, I've to learn it one way or another, Everybody's way of learning is different, so is mine, I learn quick by seeing working examples, or you can say some people are educated others are well trained, I don't feel bad saying that I'm the one leans toward well trained. Thanks – Khushwant Singh Mar 29 '13 at 18:22
  • PHP has a [`LimitIterator`](http://php.net/LimitIterator) for a reason. The logic in the answer with `$currentRecord` is broken, you might be looking for [`SimpleXMLElement::count()`](http://php.net/simplexmlelement.count) instead to get the total number of records. – hakre Apr 01 '13 at 09:10
1

You could use php's array_slice function (Documentation: http://www.php.net/manual/en/function.array-slice.php)

Start would be $page * $itemsPerPage, end would be $page * $itemsPerPage + $itemsPerPage and the number of pages would be ceil(count($xml->product) / $itemsPerPage).

Example:

$allItems = array(0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20);
$itemsPerPage = 5;
$page = isset($_GET['page']) ? intval($_GET['page']) : 0;

foreach (array_slice($allItems, $page * $itemsPerPage, $page * $itemsPerPage + $itemsPerPage) as $item) {
    echo "item $item";
}

It even works :) see: http://codepad.org/JiOiWcD1

stefreak
  • 1,460
  • 12
  • 30
1

As SimpleXMLElement is a Traversable, you can do the pagination with a LimitItertor which ships with PHP.

To get the total number of product elements you can use the SimpleXMLElement::count() function.

Pagination works like outlined in the hundreds of other questions, I preferable use the LimitPagination type for it.

It takes the current page, the total amount of elements and elements per page as arguments (see as well: PHP 5.2 and Pagination). It also has a helper function to provide the LimitIterator.

Example:

$products = $xml->product;

// pagination
$pagination = new LimitPagination($_GET['page'], $products->count(), 20);

foreach ($pagination->getLimitIterator($products) as $product) {
    ...
}

If you want to output a pager that allows to navigate between the pages, the LimitPagination has more to offer to make that a bit easier, e.g. for just all pages highlighting the current page (here exemplary with brackets):

foreach ($pagination->getPageRange() as $page)
{
    if ($page === $pagination->getPage()) {
        // current page
        printf("[p%d] ", $page); 
    } else {
        printf("p%d ", $page);
    }
}

foreach ($pagination->getPageRange() as $page)
{
    if ($page === $pagination->getPage()) {
        // current page
        printf("[p%d] ", $page); 
    } else {
        printf("p%d ", $page);
    }
}

Interactive online demo: http://codepad.viper-7.com/OjvNcO
Less interactive online demo: http://eval.in/14176

Community
  • 1
  • 1
hakre
  • 193,403
  • 52
  • 435
  • 836