XML pagination with PHP [duplicate]

2019-01-15 20:58发布

This question already has an answer here:

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>";
}

3条回答
再贱就再见
2楼-- · 2019-01-15 21:43

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

查看更多
Bombasti
3楼-- · 2019-01-15 21:56

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

查看更多
SAY GOODBYE
4楼-- · 2019-01-15 21:58

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>");
        } ?>
查看更多
登录 后发表回答