我试图修改Function中接受的答案以满足我的需求。我正在研究基于XML的图像库(个人偏好使用XML而不是MySQL,因为这是我的个人网站)。我的画廊工作正常,但我试图将记录限制为每页最多9个。我接受了上述相关问题的接受答案并使其部分工作。由于某种原因,它只显示了一半的结果。目前共有23个图像记录。为了测试目的,我已将数字设置为11
。执行此操作后,会生成2
个页面链接。由于某种原因,每页只有一个图像。还注意到,因为每页当前应该有两个,所以总共应该有12个页面链接。有人可以告诉我为什么当我将数字设置为<?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>
时,每页只显示一个图像?如果我将其设置为每页显示9个,则只显示8个。
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>
-->
:
{{1}}
答案 0 :(得分:1)
我做了一些改动,第一个是限制图像上的循环,只适用于你实际显示的那些..
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>");
}