How do I code pagination feature that fetches products from database with PHP and MySQL or PHP Data Objects aka PDO? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 10

How do I code pagination feature that fetches products from database with PHP and MySQL or PHP Data Objects aka PDO?

For e-commerce site

6th Jul 2019, 4:02 PM
Audu Peter Iko-Ojo
Audu Peter Iko-Ojo - avatar
3 Answers
+ 7
In sololearn discussion you see "page" (?page=2) parameter in the url. And there's 20 questions in one page. You would get the page number with $_GET["page"]; and then use it in your mysql query like this. $count = 20; // Rows per page $page = $_GET["page"]; $to= 0; // Starting index if($page >= 2) { $to = $count * ($page - 1); } $query = $db->prepare("SELECT FROM table LIMIT $count OFFSET $to"); $query->execute(); $rows = $query->fetch(); So on ?page=2, $to variable will be 20. LIMIT 20 OFFSET 20 Selects 20 rows starting from 21
6th Jul 2019, 4:45 PM
Toni Isotalo
Toni Isotalo - avatar
+ 6
Toni isotalo thanks, I try and customise it. Thank you
6th Jul 2019, 6:10 PM
Audu Peter Iko-Ojo
Audu Peter Iko-Ojo - avatar
+ 1
b
12th Jul 2019, 10:47 PM
Taref Ali
Taref Ali - avatar