使用PHP进行XML分页 [英] XML pagination with PHP

查看:74
本文介绍了使用PHP进行XML分页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是我用来解析XML文件的代码,但是该文件有很多记录,我想对它进行分页,每页显示20条记录.

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.

我还希望页面底部的分页链接,以便用户也可以转到其他页面.应该是这样的,如果没有给出值,则它将从0到20开始,否则,如果值是2,则从40开始并在60处停止,test.php?page=2.

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

推荐答案

类似的方法应该起作用:

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

这篇关于使用PHP进行XML分页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆