下面是我用来解析 XML 文件的代码,但是文件有很多记录,我想对其进行分页,并每页显示 20 条记录。
我还希望页面底部有分页链接,以便用户也可以转到其他页面。它应该是这样的,如果没有给出值,那么它将从 0 开始到 20,否则如果值为 2,则从 40 开始并在 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 - 使用 PHP 进行 XML 分页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15708638/