Pagination Problem

  • CStrauss
  • Graduate
  • Graduate
  • User avatar
  • Posts: 122
  • Loc: St. Louis MO. USA

Post 3+ Months Ago

I'm working on a new image gallery script and everything is working fine except one thing with my pagination system and hope someone has an idea that will help me I been racking my brain over this for the past few days and really can use some help here.

Here is a link to my image gallery I'm working on. link-----> digitalgallery.cbstrauss(dot)com/gallery.php

To give a brief explanation. all the data is being pulled from a database that holds the image path links, img_id etc and stores it in a an array called index. This is where my problem is occurring.

If you go to the above link and test for your self and look at the variables being passed in the url as well as status bar when you hover over a page link/thumbnail you see whats happening more clearly.

Now when the page loads it loads the thumbs 6 per page and the first image has a variable index value of 0, on the second page the first variable index value for that page is 6 and 3rd page 12 since we are displaying 6 thumbs per page each page it goes up by 6.

Now everything works fine no images get out of order when you click the thumb to view larger version as long as you start at page 1 then go to page 2, then 3 and so on or use the <(back) >(next)links everything stays in order. The problem Occurs when you have a user skipping around pages the index value is getting out of wack. The thumbs display correctly but if you click the larger image it shows the wrong image.

Also its getting out wack if you if do start at page one and got to page 2 then 3 in order and decide to go back say from page 3 to page 2 via the link its adding 6 to the index rather then subtracting 6. Since in this case the first image on the page has an index of 18 if I go back to page 2 it needs to subtract and pass and index value of 12, but instead it adds 6 and passes and value of 24. Again The thumbs are in the right order but if you click the image you see its not the right image.

I somehow need to find away for it to calculate the page differences and adjust the index value so the right value is being passed and the index value is consistent with what the thumbnail index value should be so when the thumb is click the correct image is being display.

I'm thinking I need to adjust my code in the for loop that display the page number links to check to see if the current page its on is greater then or less then the page you want to skip to and somehow calculate the index value to be correct. I'm really having trouble coming up with the correct syntax to use to get this to work properly. So hopefully someone here can look at it with fresh eyes and help me come up with a solution. Here is the code for this script:

PHP Code: [ Select ]
<?php
   // Connect to a database
 
// Get the current index from of the URL.
$curIndex = isset($_GET['index']) ? (int) $_GET['index'] : 0;
 
 
//Pagenation Code
   // Get Number of Rows in Table
$result = mysql_query("SELECT COUNT(img_id) AS numRows FROM gallery") or die(mysql_error());
$numRowsTotal = mysql_result($result, 0, 'numRows');
 
   // Number of items to display per page
$rowsperpage = 6;
 
   // Find out total number of pages
$totalpages = ceil($numRowsTotal / $rowsperpage);
 
   // Get the current page or set defult page.
if (isset($_GET['currentpage']) && is_numeric($_GET['currentpage'])) {
   // type cast var as int
   $currentpage = (int) $_GET['currentpage'];
}else{
   // Default Page Number
   $currentpage = 1;
   
}
 
   // if current page is greater then total number of pages set the current page to last page
if($currentpage > $totalpages){
   $currentpage = $totalpages;  
}
 
   // if current page is less then first page set the current page to first page
if($currentpage < 1){
   $currentpage = 1;
}
 
   // offset the list based on the current page
$offset = ($currentpage - 1) * $rowsperpage;
 
// Create Pagenation Links
 
   // Range of number of links to show
$range = 3;
$nextIndex = $curIndex + $rowsperpage;
$prevIndex = $curIndex - $rowsperpage;
 
 
   // if not on page 1 dont show back links
if ($currentpage > 1) {
   
   // get previous page num
   $prevpage = $currentpage - 1;
   // show < link to go back to 1 page
   echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$prevpage&index=$prevIndex'><</a> ";
}
 
// loop to show links to range of pages around current page
for ($x = ($currentpage - $range); $x < (($currentpage + $range) + 1); $x++) {
   // if it's a valid page number...
   if (($x > 0) && ($x <= $totalpages)) {
      // if we're on current page...
      if ($x == $currentpage) {
         // 'highlight' it but don't make a link
         echo " [<b>$x</b>] ";
      // if not current page...
      } else {
         // make it a link
         echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$x&index=$nextIndex'>$x</a> ";
      } // end else
   } // end if
} // end for
 
// if not on last page, show forward and last page links        
if ($currentpage != $totalpages) {
   // get next page
   $nextpage = $currentpage + 1;
    // echo forward link for next page
   echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$nextpage&index=$nextIndex'>></a> ";
 
} // end if
 
 
// End Pagenation Code
?>
<br />
<?php
// display contents from database
   // Get Data from DB
   $result = mysql_query("SELECT * FROM gallery ORDER BY img_id DESC LIMIT $offset, $rowsperpage");
   if(!$result){
      echo "MYSQL ERROR: ".mysql_error();  
   }
   
    // Display results while they are avaiable
    while($row = mysql_fetch_assoc($result)){
       stripslashes(extract($row));
?>
   <!-- Display Thumbs -->
   <a href="gallery.php?index=<?=$curIndex;?>"<img src="gallery/art_tn/<?=$img_tnName;?>" alt="<?=$curIndex;?>" /></a><br />
<?php
       $curIndex++;
    }
 
?>
 
 
 
 
  1. <?php
  2.    // Connect to a database
  3.  
  4. // Get the current index from of the URL.
  5. $curIndex = isset($_GET['index']) ? (int) $_GET['index'] : 0;
  6.  
  7.  
  8. //Pagenation Code
  9.    // Get Number of Rows in Table
  10. $result = mysql_query("SELECT COUNT(img_id) AS numRows FROM gallery") or die(mysql_error());
  11. $numRowsTotal = mysql_result($result, 0, 'numRows');
  12.  
  13.    // Number of items to display per page
  14. $rowsperpage = 6;
  15.  
  16.    // Find out total number of pages
  17. $totalpages = ceil($numRowsTotal / $rowsperpage);
  18.  
  19.    // Get the current page or set defult page.
  20. if (isset($_GET['currentpage']) && is_numeric($_GET['currentpage'])) {
  21.    // type cast var as int
  22.    $currentpage = (int) $_GET['currentpage'];
  23. }else{
  24.    // Default Page Number
  25.    $currentpage = 1;
  26.    
  27. }
  28.  
  29.    // if current page is greater then total number of pages set the current page to last page
  30. if($currentpage > $totalpages){
  31.    $currentpage = $totalpages;  
  32. }
  33.  
  34.    // if current page is less then first page set the current page to first page
  35. if($currentpage < 1){
  36.    $currentpage = 1;
  37. }
  38.  
  39.    // offset the list based on the current page
  40. $offset = ($currentpage - 1) * $rowsperpage;
  41.  
  42. // Create Pagenation Links
  43.  
  44.    // Range of number of links to show
  45. $range = 3;
  46. $nextIndex = $curIndex + $rowsperpage;
  47. $prevIndex = $curIndex - $rowsperpage;
  48.  
  49.  
  50.    // if not on page 1 dont show back links
  51. if ($currentpage > 1) {
  52.    
  53.    // get previous page num
  54.    $prevpage = $currentpage - 1;
  55.    // show < link to go back to 1 page
  56.    echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$prevpage&index=$prevIndex'><</a> ";
  57. }
  58.  
  59. // loop to show links to range of pages around current page
  60. for ($x = ($currentpage - $range); $x < (($currentpage + $range) + 1); $x++) {
  61.    // if it's a valid page number...
  62.    if (($x > 0) && ($x <= $totalpages)) {
  63.       // if we're on current page...
  64.       if ($x == $currentpage) {
  65.          // 'highlight' it but don't make a link
  66.          echo " [<b>$x</b>] ";
  67.       // if not current page...
  68.       } else {
  69.          // make it a link
  70.          echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$x&index=$nextIndex'>$x</a> ";
  71.       } // end else
  72.    } // end if
  73. } // end for
  74.  
  75. // if not on last page, show forward and last page links        
  76. if ($currentpage != $totalpages) {
  77.    // get next page
  78.    $nextpage = $currentpage + 1;
  79.     // echo forward link for next page
  80.    echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$nextpage&index=$nextIndex'>></a> ";
  81.  
  82. } // end if
  83.  
  84.  
  85. // End Pagenation Code
  86. ?>
  87. <br />
  88. <?php
  89. // display contents from database
  90.    // Get Data from DB
  91.    $result = mysql_query("SELECT * FROM gallery ORDER BY img_id DESC LIMIT $offset, $rowsperpage");
  92.    if(!$result){
  93.       echo "MYSQL ERROR: ".mysql_error();  
  94.    }
  95.    
  96.     // Display results while they are avaiable
  97.     while($row = mysql_fetch_assoc($result)){
  98.        stripslashes(extract($row));
  99. ?>
  100.    <!-- Display Thumbs -->
  101.    <a href="gallery.php?index=<?=$curIndex;?>"<img src="gallery/art_tn/<?=$img_tnName;?>" alt="<?=$curIndex;?>" /></a><br />
  102. <?php
  103.        $curIndex++;
  104.     }
  105.  
  106. ?>
  107.  
  108.  
  109.  
  110.  


Again any help will be greatly appreciate everything seems to be work like its suppose except this one last thing I hope so hopefully someone sees an answer I have missed. Thansk
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • joebert
  • Fart Bubbles
  • Genius
  • User avatar
  • Posts: 13502
  • Loc: Florida

Post 3+ Months Ago

I'm not sure why you're passing both a current_page along with a current_index, and I think using both is what has lead to this problem.

You only need to pass one of them, and use whichever one you pass to calculate the other using your total_pages/items_per_page/etc.

I think if you pull out all of the code that depends on one, and replace it with the value calculated using the other, this problem will either fix itself, or stick out like a sore thumb and be something simple.

I personally prefer passing page numbers for pagination rather than the starting index. For me it's simpler to work with page numbers than indexes.

--
Somewhat off topic, but, you've got the right idea with type juggling here, but there's a handy little shortcut you can use since an invalid integer will return zero with type juggling and you can suppress undefined index errors with @. :D
PHP Code: [ Select ]
   // Get the current page or set defult page.
if (isset($_GET['currentpage']) && is_numeric($_GET['currentpage'])) {
   // type cast var as int
   $currentpage = (int) $_GET['currentpage'];
}else{
   // Default Page Number
   $currentpage = 1;
   
}
  1.    // Get the current page or set defult page.
  2. if (isset($_GET['currentpage']) && is_numeric($_GET['currentpage'])) {
  3.    // type cast var as int
  4.    $currentpage = (int) $_GET['currentpage'];
  5. }else{
  6.    // Default Page Number
  7.    $currentpage = 1;
  8.    
  9. }

PHP Code: [ Select ]
$currentpage = max(1, (int)@$_GET['currentpage']);
  • CStrauss
  • Graduate
  • Graduate
  • User avatar
  • Posts: 122
  • Loc: St. Louis MO. USA

Post 3+ Months Ago

Well I'm passing both values for two reason the variable $current_page when you hover over one of the page number links represents that page, so if you have your cursor over page 2 in the status bar it will say currentpage=2 not meaning the current_page you are on. I know its a bit confusing to think about to be honest that pagination code something I used following a tutorial. So to keep it simple I just used the same variables as I was following along. once I get it working right I will go back and rename somethings.

The other variable index has to be passed for how the thumbs are being loaded. I have it set to load 6 thumbs per page. The index variable is holding the information from the database as an array. So think of it like index[0] has all the information for the first image, and index[1] has all the information for the second. So on on the first page the first thumb has an index value of 0 and then on 2nd page the first image on that page has a index value of 6 and the third page the first image on the page has an index value of 12 and so on.

Its important these values be passed correctly so when you click on the larger thumb its passing that same index value so the right image loads in full view.

So trying what you suggested removing the index value being passed from the link when it goes to the second page the index value is 0 again rather then 6. and If I dont pass the currentpage value then obviously it wont have a page number. it wouldnt let me post a link to the actually page for you to play around see for yourself whats going on if you copy and past digitalgallery.cbstrauss(dot)com/gallery.php into your url and change (dot) to a (.) then you will see whats going on.

go through the pages in order click to see full view and you see this way it works fine. Then trying skipping pages and see how the index are getting out of wack. Thats what I need to fix cause we all know that the end users do crazy stuff and like to try to break our scripts so this is where I am stuck at.

So again I think I somehow need to figure out a way to calculate the differences in pages for example the difference between going from page 1 to 4 is 3 and multiply that by the curIndex which would have the value of 0 for the first image on the first page and would give me the correct index. Just having problem implementing the syntax for this theory.

But what you put here I think is somewhat correct to my problem and the solution is in there somewhere.

joebert wrote:
I'm not sure why you're passing both a current_page along with a current_index, and I think using both is what has lead to this problem.

You only need to pass one of them, and use whichever one you pass to calculate the other using your total_pages/items_per_page/etc.


So maybe if you can give a bit more detailed expliantion on what you mean with that total_pages/items_per_page and how it can help I might be able to understand better


Normally I do image gallery scripts pretty much the way you discribed just wanted to try something different. As far as your other script on the tyep juggling I will have to plug that in after I get this problem solved. Thats what I like to do after I get something working go back and try to change it around and do something different so thanks for that tip.

Post Information

  • Total Posts in this topic: 3 posts
  • Users browsing this forum: No registered users and 99 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.