+ 2
How make two pages of products in PHP?
Hi, I want to ask how do I set up in PHP to show up in my store on one page of 10 products and on the next page of another 10. I have products in the database.
3 ответов
+ 6
I made similar answers already but I can't find it so here it is again.
I'm going to use sololearn as example
https://www.sololearn.com/Discuss?page=2
It's that "page" parameter that is important.
You want 10 products per page
$products = 10;
$page = $_GET["page"]; //Page number from url
Our stating index would be products per page multiplied with page number.
$index = 0;
if($page > 1) {
$index = $products * ($page - 1); //?page=2 index would be 10 and so on
}
"SELECT * FROM products LIMIT $products OFFSET $index"
On ?page=2 that query will select 10 products from your database starting on record 11
?page=1
1 - 10
?page=2
11 - 20
?page=3
21 - 30
and so on
Fetch your rows and display them
+ 2
Use a framework, most will do it for you. If it's for educational purposes then what Toni Isotalo said.
+ 2
Thanks Toni Isotalo.