Jump to content
Killersites Forums

New PHP pagination problem


Recommended Posts

I am using the script here: http://www.lateralcode.com/simple-php-gallery-pagination/


and trying to modify it so it works with my current image gallery (done differently to theirs) but after loading the first 12 images (first page), clicking on Next goes to page_no 1 but thats as far as it goes, and on page 1 (0 is the first) no pictures load. It's driving me nuts as i dont know which bit of code is messed up.


can someone take a look?


PHP (gallery_images.php):



   $country = $_GET['trip'];
   $directory = 'Images/'.$country; //folder of gallery photos.
   $thumbs_dir = 'thumbs';
   $_thumbs = "_thumb."; //to add to end of each thumbnail's name.
   $allowed_types = array('jpg','jpeg','gif','png');

   $file_parts = array(); //consists of file name and extension.
   $title = ''; //First part of file parts.
   $ext = ''; //Second part of file parts.
   $i = 0; //Iterator to check for last (fourth) image in each row.

   $per_page = 12;
   $has_previous = false;
   $has_next = false;

   $page_no = $_GET['page_no'];

   //try to open directory.
   $dir_handle = opendir($directory) or die("Cannot open directory.");
   $count = 0;
   $skip = $page_no * $per_page;

   if($skip != 0){
       $has_previous = true;

   while($count < $skip && $file = readdir($dir_handle))
       if(!is_dir($file) && in_array($ext,$allowed_types))

   echo 'page number = '.$page_no;
   $count = 0;
   //while($count < $per_page && $file = readdir($dir_handle))
   //    if(!is_dir($file) && in_array($ext,$allowed_types)){
   //        $count++;
   //    }
   while($count <= $per_page && $file = readdir($dir_handle)) //Look through folder.
       if($file == '.' || $file == '..') continue; //Ignore these files.
       $file_parts = explode('.',$file); //Split file into its parts and put into array.
       $ext = strtolower(array_pop($file_parts)); //Puts last item of array into ext.
           $title = implode('.',$file_parts); //ext has been popped off, so all that remains is title of file.
           $title = htmlspecialchars($title); //Make title html safe if it isn't already.

           //Create image to create thumbnail from.
           $img = imagecreatefromjpeg($directory.'/'.$file);
           $width = imagesx($img);
           $height = imagesy($img);

           //Create thumbnails.
           $new_width = 150;
           $new_height = floor($height * ($new_width / $width)); //Creates proportionally correct new height according to new width. floor rounds down to integer.

           //Create new image with new dimensions to hold thumb
           $tmp_img = imagecreatetruecolor($new_width,$new_height);

           //Copy and resize original image into new image.

           //Store image in a directory
           //echo '';//';

           $nomargin = ''; //Variable for last image of each row.
           if(in_array($ext,$allowed_types)) //If file is an allowed file type.
               //echo '
               if(($i+1)%4==0) $nomargin = 'nomargin'; //i begins at 0 so add one to correspond to first image. If it is divisable by 4, it
               //the last of the row, so give special class of nomargin.

                   echo '

                   //echo '';

                   $i++; //Next iteration we are looking at next image.

   while($file = readdir($dir_handle))
       if(!is_dir($file) && in_array($ext,$allowed_types))
           $has_next = true;
   closedir($dir_handle); //close the directory.


PHP (gallery_content.php):

       include 'gallery_images.php';
           echo 'Previous';
           echo 'Next';

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Create New...