DarthJawns 7 Posted April 15, 2020 Report Share Posted April 15, 2020 (edited) Does anyone have a good tutorial or reference for pagination in PHP? Looking to incorporate pagination for a few pages on my site Edited April 15, 2020 by DarthJawns Quote Link to post Share on other sites
judda 44 Posted April 15, 2020 Report Share Posted April 15, 2020 If you are using Laravel, then it comes out of the box. Otherwise, you essentially need to pass through a page number in the URL and then use a COUNT to see/calculate how many actual pages there are. Quote Link to post Share on other sites
Boltgreywing 28 Posted April 16, 2020 Report Share Posted April 16, 2020 1 hour ago, DarthJawns said: Does anyone have a good tutorial or reference for pagination in PHP? Looking to incorporate pagination for a few pages on my site Another option is Kaminari which has pretty good performance in comparison to pagination. Its a tool I use for my ruby on rails site. I am not sure if it supports php though. Quote Link to post Share on other sites
Hare 129 Posted May 19, 2020 Report Share Posted May 19, 2020 How many things on the page: $perpage = 100; Then put this after to get the page number from the URL if (isset($_GET["page"])) { $page = $_GET["page"]; } else { $page=1; }; $start_from = ($page-1) * $perpage; This add this to the end of the select query of things youre paginating. LIMIT $start_from, $perpage Then you just need to gather the links to the pages (in another query but without the limit). Quote Link to post Share on other sites
judda 44 Posted May 20, 2020 Report Share Posted May 20, 2020 15 hours ago, Hare said: How many things on the page: $perpage = 100; Then put this after to get the page number from the URL if (isset($_GET["page"])) { $page = $_GET["page"]; } else { $page=1; }; $start_from = ($page-1) * $perpage; This add this to the end of the select query of things youre paginating. LIMIT $start_from, $perpage Then you just need to gather the links to the pages (in another query but without the limit). You have to be careful with a query like that. You should be validating that it is more than just set otherwise you could potentially get yourself into SQL injection territories. Adding in an "intval" could help with that just to 100% make the string an integer before being placed in your code. I've adjusted your code block to also remove the "else" section, because you can easily define it above, and won't run into scoping issues. $page = 1; if (isset($_GET["page"]) && ($value = intval($_GET["page"]))) { $page = $value; } $start_from = ($page-1) * $perpage; The caveat with this code, is it isn't going to do a "0-based", so your pages must start at 1 which is what you are doing anyways with the page - 1 part. 1 Quote Link to post Share on other sites
Hare 129 Posted May 20, 2020 Report Share Posted May 20, 2020 1 hour ago, judda said: You have to be careful with a query like that. You should be validating that it is more than just set otherwise you could potentially get yourself into SQL injection territories. Adding in an "intval" could help with that just to 100% make the string an integer before being placed in your code. I've adjusted your code block to also remove the "else" section, because you can easily define it above, and won't run into scoping issues. $page = 1; if (isset($_GET["page"]) && ($value = intval($_GET["page"]))) { $page = $value; } $start_from = ($page-1) * $perpage; The caveat with this code, is it isn't going to do a "0-based", so your pages must start at 1 which is what you are doing anyways with the page - 1 part. Thank you, Judda! This looks great =D Quote Link to post Share on other sites
judda 44 Posted May 20, 2020 Report Share Posted May 20, 2020 Please Note: I didn't actually test it to verify that it works, but at first glance it looks like it should. Quote Link to post Share on other sites