Hello! I’m doing my project with an e-commerce website.
I got index.php that shows cards with product categories, such as “TVs, Computers, Smarwatches…”
I got products.php that shows product list, so far including all the products that I have in database but I need it to be displaying certain products that belong into the certain category (under certain cards).
In my database I got a column called ‘type’ that holds names of categories certain products belong to, such as “Phones, TVs, Cameras…”.
Now, I need to sort the products into categories - The user is on index.php, clicks on the “TVs” card, gets to products.php?category=TVs and I need only the TVs to be there and same for other categories.
Could anyone please help me with the sorting part?
Thanks a lot everyone!!!
The cards on index.php look like this:
https://i.stack.imgur.com/uMtKg.jpg
Index.php code:
> <p>
<?php $TV = ["id" => "1", "name" => "TVs", "img" => "<img src='img/TV.png'>", "price" => "$1000"]; $Computer = ["id" => "2", "name" => "Computers", "img" => "<img src='img/computer.png'>", "price" => "$2000"]; $Laptop = ["id" => "3", "name" => "Laptops", "img" => "<img src='img/laptop.png'>", "price" => "$750"]; $Camera = ["id" => "4", "name" => "Cameras", "img" => "<img src='img/camera.png'>", "price" => "$500"]; $Phone = ["id" => "5", "name" => "Phones", "img" => "<img src='img/phone.png'>", "price" => "$400"]; $Smartwatch = ["id" => "6", "name" => "Smartwatches", "img" => "<img src='img/smartwatch.png'>", "price" => "$300"]; // echo "<img src='img/computer.jpg'>"; $catalog = array ($TV, $Computer, $Laptop, $Camera, $Phone, $Smartwatch); // print_r($catalog); foreach ($catalog as $item) { echo "<div class='all-items'> <div class='catalog-item'> <div class='catalog-img'> ".$item ["img"]." </div> <h3> ".$item ["name"]." </h3> <div> ".$item ["price"]." </div>" ?> <a href='products.php?category=<?= $item["name"]; ?>' class='catalog-more-button'> See more </a>
products.php looks this way for now (the prices are just fictional):
https://i.stack.imgur.com/pjQ1w.jpg
and here products.php code:
<?php
> $result = mysqli_query($conn,"SELECT * FROM `product_details` ORDER BY type");
> // $result = mysqli_query($conn,"SELECT * FROM `product_details` WHERE type = 'TVs'");
>
> while($row = mysqli_fetch_assoc($result)) {
> echo "<div class='product_wrapper'>
> <form method='post' action=''>
> <input type='hidden' name='code' value=".$row['code']." />
> <div class='img'><img src='".$row['img']."' /></div>
> <div class='name'>".nl2br ($row['name'])."</div>
> <div class='price'>"."$".$row['price']."</div>
> <button type='submit' class='buy'>Add to basket</button>
> </form>
> </div>";
> }
>
> mysqli_close($conn);
> ?>
When I go with $result = mysqli_query($conn,“SELECT * FROM product_details
WHERE type = ‘TVs’”); it works but of course shows TVs in each category…