troubleshooting Question

Need help with pagination

Avatar of ray-solomon
ray-solomonFlag for United States of America asked on
PHP
6 Comments1 Solution301 ViewsLast Modified:
I am creating a simple gallery script with pagination. This will find all images in a directory and show the navigation, image and some image information. This works great, but there is only one problem. The pagination starts at 0 and I would like it to start a 1 instead. I think there needs to be a loop somewhere to add +1 to the current pagination numbers. But I am not sure where to put this.

Here is the directory structure:
Start at document root: /
/gallery
/gallery/index.php
/gallery/.htaccess
/gallery/images/  <- several images in here

On a side note:  I use mod_rewrite to make it look like static html pages like this: http://www.mysite.com/gallery/1.html
#start htaccess file
Options +FollowSymlinks
RewriteEngine on
RewriteBase /gallery/

RewriteRule ^([^/]+)\.html$ index.php?page=$1 [L]
#end htaccess file


<?php

// Get the requested page
      $page = (!isset($_GET['page']))? 1 : $_GET['page'];

// Set the image directories
      $image_dir = "./images/";

// Reading the thumbnail directory an putting the filenames into an array
      $readdir = opendir("$image_dir");
      while (false !== ($file = readdir($readdir)))
      {
               if ($file != "." && $file != "..")
               {
                     $gallery[] = $file;
               }
      }
            closedir($readdir);

// Calculating the number of images in the array
      $total_images = count($gallery);

// Set current, prev and next page
      $prev = ($page - 1);
      $next = ($page + 1);

// Max results per page
      $max_results = 1;

// Calculate total pages
      $total_pages = ceil($total_images / $max_results);

// Predefine this var
      $pagination = '';

// Create a PREV link if there is one
      if ($page >= 1)
      {
            $pagination .= '<a href="'.$prev.'.html"> Previous </a>&nbsp;';
      }

// Loop through the total pages
      for ($i = 0; $i <= $total_pages; $i++)
      {
            if (($page) == $i)
            {
                  $pagination .= $i;
            }
      else
            {
                  $pagination .= '<a href="'.$i.'.html"> '.$i.' </a>&nbsp;';
            }
      }

// Print NEXT link if there is one
      if ($page <= $total_pages)
      {
            $pagination .= '<a href="'.$next.'.html"> Next </a>&nbsp;';
      }

// Show the navigation
      echo $pagination.'<br><br>You are viewing result '.$page.' of '.$total_images.'<br>';


            // Show the gallery
            if ($gallery[$page])
            {    
                  
                  echo '<a href="'.$image_dir.$gallery[$page].'"
                  title="Show image" target="_blank"><img src="'.$image_dir.$gallery[$page].'"
                  style="margin: 5px;" border="0"></a><br />';
                  
                  // Removing the extension from the filename and other misc functions
                  $name = substr_replace($gallery[$page],'',-4);
                  $name = ucwords($name);
                  $dimensions = getimagesize($image_dir.$gallery[$page]);
                  $size = filesize($image_dir.$gallery[$page])/1024;
                  if ($size <= 1024)
                  {
                        $filesize = number_format($size,1,'.','.').' Kilobytes';
                  }
                  else
                  {
                        $filesize = number_format($size,1,'.',',').' Megabytes';
                  }
                  echo 'Name: '.$name.'<br />';
                  echo 'Dimensions: ' .$dimensions[0].' x '.$dimensions[1].' Pixels<br />';
                  echo 'Filesize: '.$filesize.' <br />';
                  
                  
            }

      echo '<br /><br />';

?>
ASKER CERTIFIED SOLUTION
xtcharles

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Log in to continue reading
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform for $9.99/mo
View membership options
Unlock 1 Answer and 6 Comments.
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
The Value of Experts Exchange in My Daily IT Life

Experts Exchange (EE) has become my company's go-to resource to get answers. I've used EE to make decisions, solve problems and even save customers. OutagesIO has been a challenging project and... Keep reading >>

Mike

Owner of Outages.IO
Phoenix, Arizona, United States
Member Since 2016
Join a full scale community that combines the best parts of other tools into one platform.
Unlock 1 Answer and 6 Comments.
View membership options
“All of life is about relationships, and EE has made a virtual community a real community. It lifts everyone's boat.”
William Peck

Member since 2004