XML pagination with PHP(使用 PHP 进行 XML 分页)
本文介绍了使用 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>";
}
推荐答案
这样的事情应该可行:
<?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 分页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
织梦狗教程
本文标题为:使用 PHP 进行 XML 分页
基础教程推荐
猜你喜欢
- 有什么方法可以用编码 UTF-8 而不是 Unicode 返回 PHP`json_encode`? 2021-01-01
- PHP PDO MySQL 查询 LIKE ->多个关键词 2021-01-01
- 在PHP中根据W3C规范Unicode 2022-01-01
- 如何在 Laravel 中使用 React Router? 2022-01-01
- Cron Jobs 调用带有变量的 PHP 脚本 2022-01-01
- 如何替换eregi() 2022-01-01
- 学说 dbal querybuilder 作为准备好的语句 2022-01-01
- 如何在 Laravel 5.3 注册中添加动态下拉列表列? 2021-01-01
- PHP 类:全局变量作为类中的属性 2021-01-01
- YouTube API v3 点赞视频,但计数器不增加 2022-01-01
