0

I'm trying to modify the accepted answer from XML pagination with PHP to suit my needs. I'm working on an XML based image gallery(personal preference to use XML instead of MySQL as this is my personal site). I have the gallery working fine, but I'm trying to limit the records to a maximum of 9 per page. I took the accepted answer of the above linked question and got it partially working. For some reason it is only showing half of the results. Currently there are a total of 23 image records. I have set the number to show to 2 for testing purpose. Upon doing this there are 11 page links generated. For some reason there is only one image per page. Also noting that since there should currently be two per page then there should be 12 total page links. Could someone tell me why this would only display one image per page when I set the number to 2? If I set it to show nine per page then only eight show.

<?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>

This is the current structure of 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>
-->
Jesse Elser
  • 974
  • 2
  • 11
  • 39

1 Answers1

1

I've made a couple of changes, the first is to limit the loop over the images to only work with the ones you'r actually displaying..

    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)) {

When using an XML file, you can reference the elements of a node (in this case <image>) elements as an array. What this does is rather than looping over all of the document, it loops from the start image of the page and loops for the number of images on each page, or the end of the list (the commented out code is there to show which lines it replaces).

Secondly the links. I think the problem is that when you have 23 images and 2 per page, ($currentRecord / $perPage) will give 11.5 and so when you go up in 1's, this will stop at 11. What you need to do is use ceil() which rounds the answer up to make that 12. With the above changes you can use this code instead...

// 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>");
}
Nigel Ren
  • 56,122
  • 11
  • 43
  • 55
  • Works great. Granted there are now new errors but it's progress. Just to clarify what these errors are it appears that the script is trying to create `9` records on the last page when there are only `5` leftover. Shouldn't be hard to fix. Thanks for the help! – Jesse Elser May 12 '18 at 07:13
  • Sorry - that is the one downside of using a `for()` loop, I've updated the line to say stop when it reaches `count($imagexml->image)` – Nigel Ren May 12 '18 at 07:15
  • Lol that got us working. Thank you very much. And I know that feeling all too well. It's coffee time. – Jesse Elser May 12 '18 at 07:25