troubleshooting Question

Need help with pagination

Avatar of ray-solomon
ray-solomonFlag for United States of America asked on
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/images/  <- several images in here

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

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


// 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;

// 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;
                  $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';
                        $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 />';

Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 6 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 6 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros