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;
}
40 Replies
generally you want to limit the traffic to and from your database, so you want to limit your rows there
would give you the first 10 items
gives you the second 10
So you'd write your query something like this
you'd get the number of pages from this
and can use that in a separate query to determine the number of pages
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
sounds good! Rest is important, tired devs write bad code 🙂
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 performanceI 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?
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)
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
That kind of stuff is usually a rewritten url, or a route. /cars/bmw would end up at cars.php?brand=bmw for example
So when i click on an catalog item i open a new page and generate content from the data of that element?
Using GET
it depends on what you want
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
also, what you can and can't implement varies wildly based on your environment and the tools you're using
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
you can even have the data as json files in a directroy, that you read and show in the page
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
by the way, you can also do the filtering in the client side
there are some tools for that
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
do you want to load a 2nd page?
And it has the name of the product for each product i click
Yes
I mean idk how is supposed to work
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?
or how to know what information to show?
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
are you using any framework or platform?
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
you should use isotope for filtering, and the rest is very very very varied
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
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)there's ways to make it a lot more manageable
with pretty urls for seo
This is exactly how i thinked it should be done. What put my in the fog was that weird url.
but this relies on knowing exactly how the PHP files are served
yeah, just forget about the weird URL for now
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
you have a single PHP file for all products, and load them based on a URL parameter
Aaaah, so that url is for SEO this is why they made it in that way
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
So i must learn laravel now i guess
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
Ok, ill try to do this litle project
Thank you guys! idk what i would've done without you
do you use composer?
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
wow, throwing yourself immediatelly into the deep end
how's the file structure you have currently?