+ 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
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
+ 6
Toni isotalo thanks, I try and customise it. Thank you
+ 1
b