Link to home
Start Free TrialLog in
Avatar of ray-solomon
ray-solomonFlag for United States of America

asked on

Need help with pagination

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

?>
Avatar of xtcharles
xtcharles

Heya buddy:

Take a look at this block from your code:

// Loop through the total pages
      for ($i = 0; $i <= $total_pages; $i++)
      {
            if (($page) == $i)
            {
                  $pagination .= $i;

Now you are setting the variable $i to 0. I believe that is where your problem is. If not, then double check the fact that you are going to less than/equal to the $total_pages.

But I'm fairly certain that you meant for that 0 to be a 1.
If everything is working right as you said, it could be tricky, because you may want to only display $page + 1, but internally your pagination system in zero based. So you could just change what you are getting from your URL

It could be as simple as changing the line of code to this:
// Get the requested page
      $page = (!isset($_GET['page']))? 0 : $_GET['page'] - 1;

Or there may be more to it. It's hard to say without the whole picture.
Avatar of ray-solomon

ASKER

I tried both of your suggestions, but it still will not work. I modified the code so it can be used without mod_rewrite. If anyone would like to test this, just put this script into a new directory, then create a subdirectory called "images", then put some images in that.

// rename to index.php
<?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 >= 2)
      {
            $pagination .= '<a href="index.php?page='.$prev.'"> Previous </a>&nbsp;';
      }

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

// Print NEXT link if there is one
      if ($page <= $total_pages)
      {
            $pagination .= '<a href="index.php?page='.$next.'"> 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
Avatar of xtcharles
xtcharles

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
One more thing bud,

I just want to also mention I *just* noticed your code gives you the option on the last page to go one page more. Just thought I'd let you know that you should include a check to not allow next to go past the total pages.
Thank you for your help . It is all working now.
I fixed the problem that you described above.

// Print NEXT link if there is one (Took out the equal sign)
      if ($page < $total_pages)

My next modification is to limit the amount of page numbers seen between the NEXT & PREV links.

=]