justlukeyou Posted May 20, 2012 Share Posted May 20, 2012 Hi, I have done some pagination code which echos 15 results onto a page and echos the number of pages complete with a link. Hovever, when I click on a link nothing is happening, it just reloads the page starting from 1. Can anyone advise where I am going wrong please? <?php $per_page = 15; $pages_query = mysql_query("SELECT COUNT(`description`) FROM `productdbase`"); echo $pages = mysql_result($pages_query, 0); echo $pages = ceil(mysql_result($pages_query, 0) / $per_page); $page = (isset($_GET['page'])) ? (int)$_GET['page'] : 1; $start = ($page - 1) * $per_page; $query = mysql_query("SELECT `description` FROM `productdbase` LIMIT $start, $per_page"); while ($query_row = mysql_fetch_assoc($query)) { echo '<p>', $query_row['description'] ,'</p>'; } if ($pages >= 1 && $page <= $pages) { for ($x=1; $x<=$pages; $x++) { echo ($x == $page) ? '<strong><a href="?products='.$x.'">'.$x.'</a></strong> ' : '<a href="?products='.$x.'">'.$x.'</a> '; } } ?> Quote Link to comment Share on other sites More sharing options...
Jessica Posted May 20, 2012 Share Posted May 20, 2012 Echo your query and see what it says. Quote Link to comment Share on other sites More sharing options...
justlukeyou Posted May 20, 2012 Author Share Posted May 20, 2012 Thanks, didn't know I could that. It prints this: "Resource id #5" Quote Link to comment Share on other sites More sharing options...
Jessica Posted May 20, 2012 Share Posted May 20, 2012 No, the query, not the result of the query. Put the query in a string, and echo it. Quote Link to comment Share on other sites More sharing options...
Jamdog Posted May 20, 2012 Share Posted May 20, 2012 First place I'd look for a problem is here: $page = (isset($_GET['page'])) ? (int)$_GET['page'] : 1; How are you passing 'page' to the PHP script? Is it possible that something is going wrong and page isn't being passed, so therefore you are always on page 1? Are your 'prev' and 'next' buttons correctly pointing to: myscript.php?page=x Personally, I always prefer to use $_REQUEST['page'], but that is just laziness, as it allows me to use GET or POST variables without code changes. Not suggesting you use $_REQUEST, just an option... Quote Link to comment Share on other sites More sharing options...
justlukeyou Posted May 21, 2012 Author Share Posted May 21, 2012 Hi, I have echoed the query as suggested This is the result: SELECT `description` FROM `productdbase` LIMIT 0, 15 So it is Starting a 0 and showing the next 15 but it wont transfer onto 16 for the next 15 to appear. Quote Link to comment Share on other sites More sharing options...
Jamdog Posted May 21, 2012 Share Posted May 21, 2012 As I stated above, your problem is here: if ($pages >= 1 && $page <= $pages) { for ($x=1; $x<=$pages; $x++) { echo ($x == $page) ? '<strong><a href="?products='.$x.'">'.$x.'</a></strong> ' : '<a href="?products='.$x.'">'.$x.'</a> '; } } You are setting your page links as ?products=$x But, your PHP script, on this line: $page = (isset($_GET['page'])) ? (int)$_GET['page'] : 1; is asking for a GET variable called page. This doesn't exist, so page is always equal to zero. You can either: Set your href urls to ?page=$x or $_GET['products'] Quote Link to comment Share on other sites More sharing options...
justlukeyou Posted May 21, 2012 Author Share Posted May 21, 2012 Brilliant, thanks mate. Now I have 482 pages of 15 rows. Anyone know how I can limit to it shows say 10? I take it I need some of limit on the echo of the page numbers? Quote Link to comment Share on other sites More sharing options...
Kays Posted May 21, 2012 Share Posted May 21, 2012 Change the LIMIT in your SQL from 15 to 10. Quote Link to comment Share on other sites More sharing options...
justlukeyou Posted May 21, 2012 Author Share Posted May 21, 2012 Hi, I did that but as I thought it just showed 10 results on the page, I want to display 15. Quote Link to comment Share on other sites More sharing options...
Kays Posted May 21, 2012 Share Posted May 21, 2012 I'm confused. What do you want? Quote Link to comment Share on other sites More sharing options...
justlukeyou Posted May 21, 2012 Author Share Posted May 21, 2012 I currently have 482 links (numbers) along the bottom split with 15 rows showing on the page. So I am looking to limit the 482 links to just 10. Quote Link to comment Share on other sites More sharing options...
Kays Posted May 21, 2012 Share Posted May 21, 2012 You want to limit it to +/- 5 of the current user's page? In other words, if the user is on page 28, then it shows 23 - 33? Quote Link to comment Share on other sites More sharing options...
Jamdog Posted May 21, 2012 Share Posted May 21, 2012 How about something like: if ($pages >= 1 && $page <= $pages) { for ($x=1; $x<=$pages; $x++) { $link = '<a href="?page='.$x.'">'.$x.'</a>'; if ($x == $page) echo '<strong>'.$link.'</strong> '; // Current Page else if (($x == 1) && ($page>5)) echo $link.' ... '; // First page else if (($x == $pages) && ($page<$pages-5)) echo ' ... '.$link; // Last Page else if ($x >= ($page-5)) && ($x <= ($page+5)) echo $link.' '; // Within 5 pages } } So, if you are on page 12 of 482, it would display: 1 ... 7 8 9 10 11 12 13 14 15 16 17 ... 482 and page 3 should display like: 1 2 3 4 5 6 7 8 ... 482 Quote Link to comment Share on other sites More sharing options...
Kays Posted May 21, 2012 Share Posted May 21, 2012 Okay, well ... <?php // if you are on page less than 10 $page = filter_input(INPUT_GET, 'page'); $start = $page - 5; $end = $page + 5; if ($page < 10) { $start = 1; $end = 8; } $max_pages = <some php code here to get 482>; if ($page < 10) { // for loop to print $start -> $end, then 3 dots and $max_pages. } else { // for loop to print 1, then 3 dots, then $start -> $end, then 3 dots, and finally 482 } Similar check for if the user is within 8 pages of 482, so: 1 ... 475, 476, 477, 478, 479, 480, 481, 482. Quote Link to comment Share on other sites More sharing options...
justlukeyou Posted May 21, 2012 Author Share Posted May 21, 2012 Brilliant, thanks Jamdog. The last line returns a unexpected T_BOOLEAN_AND error but I cant see what it is, it looks fine from what I can see. else if (($x == 1) && ($page>5)) echo $link.' ... '; // First page else if (($x == $pages) && ($page<$pages-5)) echo ' ... '.$link; // Last Page else if ($x >= ($page-5)) && ($x <= ($page+5)) echo $link.''; // Within 5 pages Quote Link to comment Share on other sites More sharing options...
TOA Posted May 22, 2012 Share Posted May 22, 2012 I think you're aiming for this for your last else if: (just wrapped it in another set) else if (($x >= ($page-5)) && ($x <= ($page+5))) echo $link.''; // Within 5 pages Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.