CBStrauss Posted November 10, 2009 Share Posted November 10, 2009 I been working on a new gallery script and have everything working perfectly except 1 little problem with the pagination and clicking on a page number to go back. I need the link to pass a variable called index to make sure if Im displaying 2 images per page on the first page the first image is index 0 next is 1. Then when I click the page 2 link it takes the current index which in this case is 0 (which 0 is the first image in the index and first displayed on the page) and adds 2 to the variable (because I'm displaying 2 images per page) so on page 2 the first image has the correct index of 2. This all works fine as long as I go forward. My problem is say I went from page 1 to page 2 my current index of the first image on page 2 is 2, but if I want to go back to page 1 by clicking the page 1 link its adding 2 again to my index variable being passed messing up my index order and setting my index to 4 instead of 0. I know I need to somehow make it so it subtracts in this situation just having a hard time figuring out how I need to change my code to do this. Here is my code: <?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 = 2; // 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++; } ?> My question is what do I need to change in my code to get this pagnation navigation to work properly when going back and changing my index variable being passed in the link to subtract rather then add when I click a page number to go back 1 page or I click back more then 1 page back from my current page. Link to comment https://forums.phpfreaks.com/topic/181039-pagination-problem-passing-a-variable-through-the-link/ Share on other sites More sharing options...
CBStrauss Posted November 11, 2009 Author Share Posted November 11, 2009 Tried to sleep on this problem and come back with a fresh head this morning and still drawing a blank to getting my navigation to work properly. Here is the link to what I'm working on. http://digitalgallery.cbstrauss.com/gallery.php if you click the page links you see what I'm talking about you start on page one and if you click the next page it works fine. If you click the next page after that is fine, you can see in the url the images index variable is increasing by 6 in this case correctly. Now if you click a page number lower then what your on you see it still adding 6 instead of subtracting and that's what I'm trying to fix I'm using the same code as I posted above and this the last thing I need to fix (I think LOL) to complete this project. Anyone have an Idea of how I can get it to subtract correctly when going backwards? Link to comment https://forums.phpfreaks.com/topic/181039-pagination-problem-passing-a-variable-through-the-link/#findComment-955734 Share on other sites More sharing options...
siric Posted November 11, 2009 Share Posted November 11, 2009 What you need to do print the variables all the way along the script and follow along manually. This should help you find where the problem variable is. Link to comment https://forums.phpfreaks.com/topic/181039-pagination-problem-passing-a-variable-through-the-link/#findComment-955774 Share on other sites More sharing options...
canabatz Posted November 11, 2009 Share Posted November 11, 2009 i think one of your problem is in this code: <?php $curIndex++; } ?> Link to comment https://forums.phpfreaks.com/topic/181039-pagination-problem-passing-a-variable-through-the-link/#findComment-955787 Share on other sites More sharing options...
canabatz Posted November 11, 2009 Share Posted November 11, 2009 also try that : // 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> "; $curIndex - 6; } Link to comment https://forums.phpfreaks.com/topic/181039-pagination-problem-passing-a-variable-through-the-link/#findComment-955790 Share on other sites More sharing options...
CBStrauss Posted November 11, 2009 Author Share Posted November 11, 2009 Well to explain it the $curIndex++ in your first reply is necessary that relates to when the thumbs are loading on the page. To explain what value that is holding better is basically its holding the value of index the current index its on so when the page with the thumbs load the first image has an index of 0 so the first time it goes through the loop the $curIndex has a value of 0 the when it gets to $curIndex++; its adding 1 which means the next image thumb being loaded has a value of 1 then the 3rd thumb will have a index of 2 and so on. That has nothing to do with my problem. Thats just setting the index value that will be passed in the url to know which image to show. The second post I see what you did and thats not really helping me at all. My problem is not with how the images are being displayed on the thumbnails page really the problem is the page links at the top of the page. For example when the page loads its displaying 6 thumbs per page the first image has an index value of 0 and the 6th image has an index value of 5. Now if you click page 2 everything stays in order the index values being passed increase by 6 based on the the current index value of the first image on the page which on page 1 is 0 + the number of images being displayed per page which in this case is 6 so 0 + 6 = 6. So on page two the first image on that page has a value of 6 which is how it should be. Now if you go from page 2 to 3 everything works as explained above it takes the index value of the first image passes through the link which is 6 and adds another 6 to it representing the number of images to be displayed so the first image on the 3rd has an index variable value of 12. And as you continue on clicking the next page this way in order everything works fine. Also you will notice the < (goes back 1 page) and >(goes forward 1 page) this works fine as long as you HAVE been going through the pages in order. Now the issue I'm having if you on page 1 and skip page 2 and click on page 3 the Index value of the first image on the page is wrong its still adding 6 to the value when I need it to make adjustments for those that are skipping pages in this case It needs to add 12 instead of 6. This also causes problems if you say again skip page 2 and click on page 3 and then decided to click the page 2 link or click the > or < link the images on the thumbnail are displaying in the correct order but the value that the variable hold index is wrong. In most cases its still adding 6 rather then taking the current index value for the first image on that page and subtracting. Now I know somehow to make the adjustments I need to adjust for page skipping by taking the current page I'm on say I start on page 1 and want to go to page 3 I need to some how figure out the difference which would be 2 (3-1 = 2 page difference) then take the current index of the first image on page 1in this case which is 0 and multiply it by 6(the number of images per page) times the difference in pages I'm navigating which is 2 in this case which would put me at the correct index value of the first image on page 3 which would be 12. And need to figure out away to do something similar if I go from page 1 to page 3 then go back to page 2. The index value being passed has to be correct to display the proper image when you click to see the full image. cause index is holding the information stored in the database which includes the path to each image. Does this make sense what I'm trying to do? What I need to do is on the tip of my brain but I'm just having trouble getting the right syntax for it and getting placed in the proper spot in my code. Again to see whats going on here is the link. http://digitalgallery.cbstrauss.com/gallery.php To see more clearly what I'm talking about: 1) load the page and then navigate in order from page 1 to page 2 to page 3 and so on. you can even click the <(back link) >(forward link) and see its fine. Click on a thumb to see the correct full size image is being displayed. Also look in the status bar at the bottom of the page as you hover over an image or one of the page links and see what values are being passed. 2)Now load the page again and go from page 1 to another page other then 2, skip around page numbers and with the < > links. Then click on the large images check the values being passed in the url and status bar and see they are not matching up.Even though on the thumbnail page the images are in the right order their index value is not correct causing the wrong image to load when you click the full size image and when you go back its adding instead of subtracting. Again I feel I'm close to figuring this out but just cant figure out what I need to do to get it to work properly so any examples or ideas I really appreciate it. I spent two days working on this script and this seems to be the last issue to sort out before I'm done. Link to comment https://forums.phpfreaks.com/topic/181039-pagination-problem-passing-a-variable-through-the-link/#findComment-955861 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.