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. I got a column called 'type' in database that holds names of categories each product belongs to, such as "phone, tv, camera...".
Now, I need to sort the products into categories, so after clicking eg. "Computers" on index.php, I'll only see computers on products.php. Could anyone please help me?
The cards look like this:
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):
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 = 'tv'");
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);
?>