PHP Catalog Limit Numbers of Items per page

Hello, i managed to understand how to generate divs from the numbers of row into an database. I can't figure out how to limit the amounth of items per pages just like in a normal ecommerce shop. You can navigate to page 1,2,3 etc. In my code it just spams each row into the page. I want 10 elements per page, then change page 2, then 3 etc. I am newbie, i don't know even if i am doing this div generations corectly. And yes i know i have to use prepared statements for sql, but is easier to work with it like this. This is the code i wrote: $sql = "SELECT * from prodotti"; $result = $conn->query($sql); for($x=0; $x<$result->num_rows;$x++){ $row = $result->fetch_assoc(); $id = $row["idprodotti"]; $nome = $row["nome"]; $descrizione = $row["descrizione"]; echo '<div class="container-prodotti">'; echo '<h1>'.$nome.'</h1>'; echo '<p>'.$descrizione.'</p>'; echo '</div>'; } This is some code i made to know how many pages i need but idk how to go from here. $nr_entrate = $result->num_rows; //29 $verifica_resto = $nr_entrate % 10; $dividi_entrate = (int) ($nr_entrate / 10); if($verifica_resto === 0){ $pagine = $dividi_entrate; } else { $pagine = $dividi_entrate + 1; }
No description
40 Replies
Jochem
Jochem11mo ago
generally you want to limit the traffic to and from your database, so you want to limit your rows there
SELECT * FROM prodotti LIMIT 10 OFFSET 0;
SELECT * FROM prodotti LIMIT 10 OFFSET 0;
would give you the first 10 items
SELECT * FROM prodotti LIMIT 10 OFFSET 10;
SELECT * FROM prodotti LIMIT 10 OFFSET 10;
gives you the second 10 So you'd write your query something like this
$limit = 10;
$offset = (intval($_GET['page']) - 1) * $limit;

$sql = "SELECT * FROM prodotti LIMIT $limit OFFSET $offset";
$limit = 10;
$offset = (intval($_GET['page']) - 1) * $limit;

$sql = "SELECT * FROM prodotti LIMIT $limit OFFSET $offset";
you'd get the number of pages from this
SELECT count(*) FROM prodotti;
SELECT count(*) FROM prodotti;
and can use that in a separate query to determine the number of pages
DimitriUI
DimitriUI11mo ago
Thank you, chatgpt guided me in the same direction as you. I made a mess of a code, ill take a break because i am exausted and tomorrow ill try to make it clean again. I understanded now the logic, ill come back with my full code when working
Jochem
Jochem11mo ago
sounds good! Rest is important, tired devs write bad code 🙂
ἔρως
ἔρως11mo ago
one EXTREMELLY important consideration: get into the habit of using prepared statements and validate everything you get from an user. the query there seems perfectly secure, and it shouldn't have any security holes. however, if you get into the habit of using prepared statements, then when you need to do, say, a search, you are already prepared for handling the values safely also, contrary to what you may think, select count(*) from table_with_primary_key is extremelly fast and doesn't count every single row in the table when you use it dont be tempted to change it to count(id) - the mysql compiler and optimizer may not catch on what you are trying to actually do, and you could hurt performance
DimitriUI
DimitriUI11mo ago
I managed to do it. But now new questions are rising. Lets say i want to add filters, by category, price lower to higher, colors etc... How i do this? Make a function for each case?
Jochem
Jochem11mo ago
usually you'd use a single function, you just build out the query depending on the parameters you receive if there's a filter, you add a where clause. if there's a sort order, you add an ORDER clause just keep in mind that you still need to prevent SQL injections with everything you receive from the user, so use prepared statements, filter column names to an approved list (and still use prepared statements to populate them into your query)
DimitriUI
DimitriUI11mo ago
I just learned also how to load the catalog without reloading the entire page with AJAX. Now the only question that remains is: Once i can generate and filter the catalog, if i click on an item. What i am supposed to do? Should i reload a new page with the article or update the same one with AJAX? I see on some sites that for example i am on catalog www.sitename.ro/cars i click on an item on catalog and brings me to www.sitename.ro/bmw for example not /cars/bmw And is the same name as the product on catalogue, which makes me think that it has a page for each product? Doesn't have sense
Jochem
Jochem11mo ago
That kind of stuff is usually a rewritten url, or a route. /cars/bmw would end up at cars.php?brand=bmw for example
DimitriUI
DimitriUI11mo ago
So when i click on an catalog item i open a new page and generate content from the data of that element? Using GET
ἔρως
ἔρως11mo ago
it depends on what you want
Jochem
Jochem11mo ago
yeah, there's about a million different ways to do that in general, with a vanilla PHP application, you'll generally have one .php file per type of page. So a car-detail page will have a php file, a catalog page will have a php file, the homepage will too, then a set for the checkout process each PHP file will then receive parameters through the URL to tell it which car to display, or which filters to apply that gets more complicated when you start fetching partial pages using AJAX, where you might have a file for showing the base HTML and then either a central API with files per endpoint to get data from, or a separate PHP file to load data from with AJAX, or even (though I'm not sure I'd recommend it) the same file that shows the HTML just with a different request method or different parameter there's no one right way to do it though, so "Do I do X?" usually isn't really answerable
ἔρως
ἔρως11mo ago
also, what you can and can't implement varies wildly based on your environment and the tools you're using
DimitriUI
DimitriUI11mo ago
I think of doing this. I have the catalogue with items on it, in each item i place an link to another php file and through that link i give it parameters and load new page. Example i click on an item of engines i will go to www.example.ro/engine.php?par1=value$par2=value.... etc and load the page
ἔρως
ἔρως11mo ago
you can even have the data as json files in a directroy, that you read and show in the page
Jochem
Jochem11mo ago
I wouldn't have separate files for filters if I were you. What if I want all cars with a certain engine, but also only the blue ones? also, you'll have to write so much of the code for fetching the cars and rendering them out to HTML multiple times, once per filter type
ἔρως
ἔρως11mo ago
by the way, you can also do the filtering in the client side there are some tools for that
DimitriUI
DimitriUI11mo ago
I think i explained bad my self. You see on these screens the first 1 there is the address. Then i click on an product and it loads another page. This confuse me a lot. How do i do that? This is what i dont understand
No description
No description
ἔρως
ἔρως11mo ago
do you want to load a 2nd page?
DimitriUI
DimitriUI11mo ago
And it has the name of the product for each product i click Yes I mean idk how is supposed to work
Jochem
Jochem11mo ago
are you confused how they managed to not have .php at the end? or about how they managed to have mostly the same header but a different page underneath?
ἔρως
ἔρως11mo ago
or how to know what information to show?
DimitriUI
DimitriUI11mo ago
When i click on an item on the catalog. What must happen so i arrive to the second page with the products detail? Sorry, my questions are bad written. Need a break
ἔρως
ἔρως11mo ago
are you using any framework or platform?
DimitriUI
DimitriUI11mo ago
HTML CSS PHP JAVASCRIPT JQUERY Standard On visual studio code I have the database on an virtual machine on bridge with a fix ip for test
ἔρως
ἔρως11mo ago
you should use isotope for filtering, and the rest is very very very varied
Jochem
Jochem11mo ago
The way I would build that with vanilla PHP, is with a catalog.php page that displays the left screenshot, then a product.php file that displays the product detail on the right. in catalog.php, you would have a link that points to product.php with some kind of unique identifier, usually a numeric id or a so-called "slug", which is usually a unique string of text with nothing but alphanumeric characters or a dash. The very basic version would just be <a href="product.php?id=1">My Cool Product</a> in catalog.php, and then product.php would do something like SELECT * FROM products WHERE productid = $productid (properly sanitized, or preferably using prepared statements of course) The way they change the URL, is probably using a part of Apache called mod_rewrite. It lets you define rules for URLs that let you modify the URL before it's properly processed. I don't remember the details of the syntax, but in pseudocode you'd say something like
if URL is plante-exotice or URL is plante-domestice
then load catalog.php
else
load product.php?slug=<original URL>
if URL is plante-exotice or URL is plante-domestice
then load catalog.php
else
load product.php?slug=<original URL>
then in product.php, instead of filtering on productid, you'd filter on slug: SELECT * FROM products WHERE slug = $slug (again, with prepared statements or in a pinch proper sanitization. NEVER EVER put user entered data directly into a query)
ἔρως
ἔρως11mo ago
there's ways to make it a lot more manageable with pretty urls for seo
DimitriUI
DimitriUI11mo ago
This is exactly how i thinked it should be done. What put my in the fog was that weird url.
ἔρως
ἔρως11mo ago
but this relies on knowing exactly how the PHP files are served
Jochem
Jochem11mo ago
yeah, just forget about the weird URL for now
DimitriUI
DimitriUI11mo ago
So i have another php file for an single product. When i click on an item on the catalogue i link to that page with an id inside for example and generate the rest of the page
Jochem
Jochem11mo ago
you have a single PHP file for all products, and load them based on a URL parameter
DimitriUI
DimitriUI11mo ago
Aaaah, so that url is for SEO this is why they made it in that way
Jochem
Jochem11mo ago
it's not done with PHP most of the time, in practice you'll probably want to use something like Laravel in the future which will do this for you, and even if you want to stick to plain PHP, learning mod_rewrite takes an afternoon if you've got a decent base level of PHP knowledge and a little bit of Apache knowledge or alternatively whatever nginx uses, no clue personally
DimitriUI
DimitriUI11mo ago
So i must learn laravel now i guess
Jochem
Jochem11mo ago
that's not necessarily what I said, but learning a framework is probably a next step once you have a decent base knowledge I'd not recommend you dive into Laravel now though, you should be more comfortable writing your own applications first
DimitriUI
DimitriUI11mo ago
Ok, ill try to do this litle project Thank you guys! idk what i would've done without you
ἔρως
ἔρως11mo ago
do you use composer?
DimitriUI
DimitriUI11mo ago
No, i am a newbie trying to learn how to make website's In the last month i try harded to learn all together HTML, CSS, JAVASCRIPT, MYSQL and PHP. I got the basics and i want to try make an ecommerce site
ἔρως
ἔρως11mo ago
wow, throwing yourself immediatelly into the deep end how's the file structure you have currently?
Want results from more Discord servers?
Add your server