我正在尝试修改来自XML pagination with PHP的可接受的答案,以适应我的需要。我正在开发一个基于XML的图像库(个人偏好使用XML而不是MySQL,因为这是我的个人站点)。我的画廊运作良好,但我试图将记录限制在每页最多9张。我接受了上述问题的公认答案,并使其部分发挥作用。出于某种原因,它只显示了一半的结果。目前共有23个图像记录。为了测试目的,我已经设置了向2显示的数字。完成此操作后,将生成11页面链接。由于某种原因,每页只有一张图片。还注意到,由于目前每页应该有两个,那么总共应该有12个页面链接。有人能告诉我为什么当我将数字设置为2时,每页只显示一幅图像吗?如果我把它设置为每页显示九个,那么只有八个显示。
<?php
include $_SERVER['DOCUMENT_ROOT'] . "/settings/config.php";
?>
<body>
<?php
include $_SERVER['DOCUMENT_ROOT'] . "/php/header.php";
if(isset($_GET['page'])) { $startPage = $_GET['page']; } else { $startPage = 1; }
$perPage = 2;
$currentRecord = 0;
$imagexml = new SimpleXMLElement($_SERVER['DOCUMENT_ROOT'].'/info/gallery.xml', 0, true);
?>
<div class="container">
<div class="row">
<div class="message text-center w-100">
<h1 class="txt-shadow"><span class="tattoo-icon">l</span> Photo Gallery <span class="tattoo-icon">;</span></h1>
</div>
<div class="card-deck">
<?php
foreach($imagexml->image as $image) {
$currentRecord += 1;
if($currentRecord > ($startPage * $perPage) && $currentRecord < ($startPage * $perPage + $perPage)){
echo '<div class="col-sm-12 col-md-4 mb-3 text-center">
<div class="card">
<a href="http://example.com/img/gallery/'.$image->src.'" data-toggle="lightbox" data-gallery="body_piercings">
<img src="http://example.com/img/gallery/'.$image->src.'" class="card-img-top img-fluid img-thumbnail bx-shadow" alt="Card image cap">
</a>
<div class="card-body">
<h5 class="card-title txt-shadow">'. $image->title .'</h5>
<p class="card-text txt-shadow">'.$image->desc.'</p>
<a href="'.$image->instagram.'" target="_blank" class="btn btn-instagram bx-shadow"><i class="fa fa-instagram"> </i> View On Instagram</a>
</div>
<div class="card-footer">
<small class="text-muted">Posted: '.$image->date.'</small>
</div>
</div>
</div>';
}
}
?>
</div>
</div>
<nav>
<ul class="pagination justify-content-center">
<?php
//and the pagination:
for ($i = 1; $i <= ($currentRecord / $perPage); $i++) {
echo("<li class='page-item'><a class='page-link' href='?page=".$i."'>".$i."</a></li>");
} ?>
</ul>
</nav>
</div>
<div class="container-fluid">
<?php
include $_SERVER['DOCUMENT_ROOT'] . "/php/footer.php";
?>
</div>
</body>
</html>这是gallery.xml当前的结构
<?xml version="1.0" encoding="UTF-8"?>
<images>
<image>
<src>belly_button_1.jpg</src>
<title>Belly Button</title>
<desc>Belly button piercing using 14G curved barbell.</desc>
<instagram>https://www.example.com/p/BbHba1BgmrC/</instagram>
<date>November 5, 2017</date>
</image>
<image>
<src>christina_1.jpg</src>
<title>Christina(Pubic)</title>
<desc>Christina(pubic) piercing using 14G curved barbell.</desc>
<instagram>https://www.example.com/p/BclajtRgkPk/</instagram>
<date>December 11, 2017</date>
</image>
<image>
<src>christina_2.jpg</src>
<title>Christina(Pubic)</title>
<desc>Christina(pubic) piercing using 14G curved barbell.</desc>
<instagram>https://www.example.com/p/BdSlWkogshq/</instagram>
<date>December 29, 2017</date>
</image>
<image>
<src>eyebrow_1.jpg</src>
<title>Eyebrow</title>
<desc>Vertical eyebrow piercing using 16G curved barbell.</desc>
<instagram>https://www.example.com/p/BbIBtHDAX2F/</instagram>
<date>November 5, 2017</date>
</image>
<image>
<src>eyebrow_2.jpg</src>
<title>Eyebrow</title>
<desc>Vertical eyebrow piercing using 16G curved barbell.</desc>
<instagram>https://www.example.com/p/Bfi9x_RAczq/</instagram>
<date>February 23, 2018</date>
</image>
<image>
<src>lobe_1.jpg</src>
<title>Ear Lobe</title>
<desc>Ear lobe piercing using 16G straight barbell.</desc>
<instagram>https://www.example.com/p/BbHxZLtAnVA/</instagram>
<date>November 5, 2017</date>
</image>
<image>
<src>lobes_1.jpg</src>
<title>Ear Lobe(Pair)</title>
<desc>Ear lobe piercings using 14G captive bead rings.</desc>
<instagram>https://www.example.com/p/Bb4zQO1gsAO/</instagram>
<date>November 24, 2017</date>
</image>
<image>
<src>lobes_2.jpg</src>
<title>Ear Lobe(Pair)</title>
<desc>Ear lobe piercings using 14G captive bead rings.</desc>
<instagram>https://www.example.com/p/BdSlqmXggSt/</instagram>
<date>December 29, 2017</date>
</image>
<image>
<src>monroe_1.jpg</src>
<title>Monroe</title>
<desc>Monroe piercing using 16G lip stud.</desc>
<instagram>https://www.example.com/p/BbHxJwYA3lt/</instagram>
<date>November 5, 2017</date>
</image>
<image>
<src>monroe_2.jpg</src>
<title>Monroe</title>
<desc>Monroe piercing using 16G internally threaded lip stud.</desc>
<instagram>https://www.example.com/p/BifXeMYFjfO/</instagram>
<date>May 7, 2018</date>
</image>
<image>
<src>nipples_1.jpg</src>
<title>Nipples(Female Pair)</title>
<desc>Nipple piercings using 14G straight barbell.</desc>
<instagram>https://www.example.com/p/Bcl7eSoAuaa/</instagram>
<date>December 12, 2017</date>
</image>
<image>
<src>nipples_2.jpg</src>
<title>Nipples(Female Pair)</title>
<desc>Nipple piercings using 14G straight barbell.</desc>
<instagram>https://www.example.com/p/BdSlP5iATaj/</instagram>
<date>December 29, 2017</date>
</image>
<image>
<src>nostril_1.jpg</src>
<title>Nostril</title>
<desc>Nostril piercing using 20G nose stud.</desc>
<instagram>https://www.example.com/p/BbHbK6gg8Ub/</instagram>
<date>November 5, 2017</date>
</image>
<image>
<src>nostril_2.jpg</src>
<title>Nostril</title>
<desc>Nostril piercing using 18G nose stud.</desc>
<instagram>https://www.example.com/p/BifWlv7FsMK/</instagram>
<date>May 7, 2018</date>
</image>
<image>
<src>septum_1.jpg</src>
<title>Septum</title>
<desc>Septum piercing using 16G horeshoe barbell.</desc>
<instagram>https://www.example.com/p/Bb_QglhAYIA/</instagram>
<date>November 27, 2018</date>
</image>
<image>
<src>septum_2.jpg</src>
<title>Septum</title>
<desc>Septum piercing using 16G horeshoe barbell.</desc>
<instagram>https://www.example.com/p/BcDzH46AG6W/</instagram>
<date>November 28, 2018</date>
</image>
<image>
<src>septum_snakebites_1.jpg</src>
<title>Septum And Snakebites</title>
<desc>Septum piercing using 16G horeshoe barbell along with snakebite piercings using 16G internally threaded lip studs.</desc>
<instagram>https://www.example.com/p/BcI_PAuAYQ3/</instagram>
<date>November 30, 2017</date>
</image>
<image>
<src>snakebites_1.jpg</src>
<title>Snakebites</title>
<desc>Snakebite piercings using 14G horseshoe barbells.</desc>
<instagram>https://www.example.com/p/BbK2X5pgXpv/</instagram>
<date>November 6, 2017</date>
</image>
<image>
<src>snakeeyes_1.jpg</src>
<title>Snake Eyes</title>
<desc>Snake Eyes piercing through tip of tongue using 14G internally threaded curved barbell.</desc>
<instagram>https://www.example.com/p/BePKMu_AdHH/</instagram>
<date>January 21, 2018</date>
</image>
<image>
<src>throat_surface.jpg</src>
<title>Throat Surface</title>
<desc>Throst surface piercing using two 16G curved barbells.</desc>
<instagram>https://www.example.com/p/BbHwp83Adq3/</instagram>
<date>November 5, 2017</date>
</image>
<image>
<src>tongue_1.jpg</src>
<title>Tongue</title>
<desc>Tongue piercing using 14G straight barbell.</desc>
<instagram>https://www.example.com/p/BbH21jKAovA/</instagram>
<date>November 5, 2017</date>
</image>
<image>
<src>tongue_2.jpg</src>
<title>Tongue</title>
<desc>Tongue piercing using 14G straight barbell.</desc>
<instagram>https://www.example.com/p/Bb76eLBAVSf/</instagram>
<date>November 25, 2017</date>
</image>
<image>
<src>tongue_3.jpg</src>
<title>Tongue</title>
<desc>Tongue piercing using 14G straight barbell.</desc>
<instagram>https://www.example.com/p/BeUhHj2goVO/</instagram>
<date>January 23, 2018</date>
</image>
</images>
<!--
<image>
<src></src>
<title></title>
<desc></desc>
<instagram></instagram>
<date></date>
</image>
-->发布于 2018-05-12 05:32:56
我做了几个修改,第一个是限制对图像的循环,使其只适用于实际显示的图像。
for ($currentRecord = (($startPage-1)*$perPage); $currentRecord < (($startPage-1)*$perPage)+$perPage && $currentRecord < count($imagexml->image); $currentRecord++) {
$image = $imagexml->image[$currentRecord];
//foreach ($imagexml->image as $image) {
//$currentRecord += 1;
//if ($currentRecord >= ($startPage * $perPage) && $currentRecord < ($startPage * $perPage + $perPage)) {在使用XML文件时,可以将节点元素(在本例中为<image>)元素引用为数组。它所做的不是循环遍历所有文档,而是从页面的开始图像循环,并循环每个页面上的图像数量,或者列表的末尾(注释掉的代码是用来显示它替换的行)。
第二,链接。我认为问题是,当你有23张图片,每页2张,($currentRecord / $perPage)会给出11.5张,所以当你在1秒内上升时,这个值会在11点停止。你需要做的是使用ceil(),把答案打成12圈。通过上面的修改,你可以使用下面的代码.
// and the pagination:
$numberPages = ceil((count($imagexml->image) / $perPage));
for ($i = 1; $i <= $numberPages; $i ++) {
echo ("<li class='page-item'><a class='page-link' href='?page=" . $i . "'>" . $i . "</a></li>");
}https://stackoverflow.com/questions/50302659
复制相似问题