用PHP实现XML分页
下面是我用来解析XML文件的代码,但该文件有很多记录,我想对其进行分页,每页显示20条记录 我还希望页面底部的分页链接,这样用户也可以转到其他页面。它应该是这样的,如果没有给出任何值,那么它将从0开始到20,如果值为2,则从40开始到60停止,用PHP实现XML分页,php,xml,parsing,pagination,simplexml,Php,Xml,Parsing,Pagination,Simplexml,下面是我用来解析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
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>";
}
$xml=newsimplexmlement('xmlfile.xml',0,true);
foreach($xml->productas$key=>$value){
回声“;
回声“
”;
}
类似的方法应该可以:
<?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>");
} ?>
产品为$key=>$value)
{
$currentRecord+=1;
如果($currentRecord>($startPage*$perPage)和&$currentRecord<($startPage*$perPage+$perPage)){
回声“;
//echo$value->name;
回声“
”;
}
}
//以及分页:
对于($i=1;$i)
类似的方法应该可以:
<?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>");
} ?>
产品为$key=>$value)
{
$currentRecord+=1;
如果($currentRecord>($startPage*$perPage)和&$currentRecord<($startPage*$perPage+$perPage)){
回声“;
//echo$value->name;
回声“
”;
}
}
//以及分页:
对于($i=1;$i)
您可以使用php的array\u slice
函数(文档:)
开始为$page*$itemsPerPage
,结束为$page*$itemsPerPage+$itemsPerPage
,页数为ceil(计数($xml->product)/$itemsPerPage)
例如:
$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";
}
$products = $xml->product;
// pagination
$pagination = new LimitPagination($_GET['page'], $products->count(), 20);
foreach ($pagination->getLimitIterator($products) as $product) {
...
}
它甚至可以工作:)请参阅:您可以使用php的
数组片
函数(文档:)
开始为$page*$itemsPerPage
,结束为$page*$itemsPerPage+$itemsPerPage
,页数为ceil(计数($xml->product)/$itemsPerPage)
例如:
$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";
}
$products = $xml->product;
// pagination
$pagination = new LimitPagination($_GET['page'], $products->count(), 20);
foreach ($pagination->getLimitIterator($products) as $product) {
...
}
它甚至可以工作:)请参见:因为
simplexmlement
是一个可遍历的文件,您可以使用PHP附带的
要获取产品元素的总数,可以使用simplexmlement::count()
函数
分页的工作原理与其他数百个问题中概述的一样,我更倾向于使用
它将当前页面、元素总数和每页元素数作为参数(另请参见:)。它还有一个帮助函数,用于提供LimitIterator
例如:
$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";
}
$products = $xml->product;
// pagination
$pagination = new LimitPagination($_GET['page'], $products->count(), 20);
foreach ($pagination->getLimitIterator($products) as $product) {
...
}
如果您想输出允许在页面之间导航的寻呼机,LimitPagination
提供了更多功能,使之更容易实现,例如,仅针对突出显示当前页面的所有页面(此处以括号为例):
交互式在线演示:
交互式较少的在线演示:由于simplexmlement
是一种可遍历的,您可以使用PHP附带的
要获取产品元素的总数,可以使用simplexmlement::count()
函数
分页的工作原理与其他数百个问题中概述的一样,我更倾向于使用
它将当前页面、元素总数和每页元素数作为参数(另请参见:)。它还有一个帮助函数,用于提供LimitIterator
例如:
$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";
}
$products = $xml->product;
// pagination
$pagination = new LimitPagination($_GET['page'], $products->count(), 20);
foreach ($pagination->getLimitIterator($products) as $product) {
...
}
如果您想输出允许在页面之间导航的寻呼机,LimitPagination
提供了更多功能,使之更容易实现,例如,仅针对突出显示当前页面的所有页面(此处以括号为例):
交互式在线演示:
互动性较差的在线演示:对不起,我不认为这是你的意图,但你比以往任何时候都让我困惑@没问题,我添加了一个例子,希望这能帮助你理解我的意思。如果你仍然困惑,请解释到底是什么让你困惑。+1至少提供一些解释。对不起,我不认为这是你的意图,但你比以往任何时候都让我困惑@没问题,我添加了一个例子,希望这能帮助你理解我的意思。如果你仍然困惑不解,请解释到底是什么让你困惑。+1至少提供了一些解释。完全一样的事情奏效了,非常感谢@Phil@Phil非常遗憾,你为他做了家庭作业,没有让他自己学习应用分页概念。@stefreak感谢你的意图,我必须以这样或那样的方式学习,每个人的学习方式都不同,我的也不同,我通过看工作示例学习得很快,或者你可以说有些人受过良好的教育,有些人受过良好的训练,说我是那个倾向于受过良好训练的人,我并不感到难过。Thankspp有一个理由。使用$currentRecord
的答案中的逻辑已被破坏,您可能需要查找以获取记录的总数。完全相同的操作已成功,非常感谢@Phil@Phil非常遗憾,你为他做了家庭作业,没有让他自己学习应用分页概念。@stefreak感谢你的意图,我必须以这样或那样的方式学习,每个人的学习方式都不同,我的也不同,我通过看工作示例学习得很快,或者你可以说有些人受过良好的教育,有些人受过良好的训练,说我是那个倾向于受过良好训练的人,我并不感到难过。Thankspp有一个理由。使用$currentRecord
的答案中的逻辑已中断,您可能需要查找以获取记录总数。