dudejma Posted January 15, 2012 Share Posted January 15, 2012 Is there a way of doing pagination other than going through the whole big file, like: $limit = "LIMIT 0, 30"; if ($_GET['page'] == 2) { $limit = "LIMIT 30, 30"; } $sql = "SELECT * FROM table WHERE blah = 'blah' $limit"; $result = mysql_query($sql); But is there a way to do this without having to write an if statement for each page without getting to extensive? Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/255040-pagination/ Share on other sites More sharing options...
AyKay47 Posted January 15, 2012 Share Posted January 15, 2012 you can google this and find many results, but basically you will set the mysql offset and limit to variables, and when a page is clicked, set the offset to (page_number * number of things per page) and the limit to (page_offset - number of things per page). Quote Link to comment https://forums.phpfreaks.com/topic/255040-pagination/#findComment-1307743 Share on other sites More sharing options...
dudejma Posted January 15, 2012 Author Share Posted January 15, 2012 Oh, that makes sense. Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/255040-pagination/#findComment-1307746 Share on other sites More sharing options...
AyKay47 Posted January 15, 2012 Share Posted January 15, 2012 Oh, that makes sense. Thanks! really, you can keep the limit at a constant (number of things per page), the offset is the $variable that will need to be dynamic. Quote Link to comment https://forums.phpfreaks.com/topic/255040-pagination/#findComment-1307748 Share on other sites More sharing options...
dudejma Posted January 15, 2012 Author Share Posted January 15, 2012 I'm not very advanced, but isn't offset the same thing as the second number in limit? Ex.: "LIMIT 0, 30" - Would 30 be the offset, or do you have to do; "LIMIT 0 OFFSET 30"? Quote Link to comment https://forums.phpfreaks.com/topic/255040-pagination/#findComment-1307749 Share on other sites More sharing options...
AyKay47 Posted January 15, 2012 Share Posted January 15, 2012 for limit 0,30 0 is the offset, 30 is the number of rows to display Quote Link to comment https://forums.phpfreaks.com/topic/255040-pagination/#findComment-1307750 Share on other sites More sharing options...
.josh Posted January 15, 2012 Share Posted January 15, 2012 http://www.phpfreaks.com/tutorial/basic-pagination Quote Link to comment https://forums.phpfreaks.com/topic/255040-pagination/#findComment-1307753 Share on other sites More sharing options...
QuickOldCar Posted January 15, 2012 Share Posted January 15, 2012 As AyKay47 pointed out already. Here's a pagination script and demo I did, might be able to see the process better. http://get.blogdns.com/paginate/ As can see there is a startrow(mysql starting row begins at zero) and a posts_per_page(how many results are displayed each page) in the query. It uses math to determine what the mysql starting row will be depending on the page number and how many posts per page are displaying. $result = mysql_query("SELECT * FROM table ORDER BY id DESC LIMIT $startrow,$posts_per_page"); It would be better to have the $startrow dynamic in the query, not checking with multiple if's and setting the values for each page like you are. There's a complete pagination tutorial at phpfreaks. http://www.phpfreaks.com/tutorial/basic-pagination Quote Link to comment https://forums.phpfreaks.com/topic/255040-pagination/#findComment-1307754 Share on other sites More sharing options...
dudejma Posted January 15, 2012 Author Share Posted January 15, 2012 It works, thanks guys! Quote Link to comment https://forums.phpfreaks.com/topic/255040-pagination/#findComment-1307756 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.