Jump to content

Recommended Posts

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!!!

This is my database table:

https://ibb.co/QNh5vrB

The cards on index.php look like this:
https://i.stack.imgur.com/uMtKg.jpg

Index.php code:

            <?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


$sql = "SELECT * FROM product_details WHERE type = ". $_GET["category"];


$result = mysqli_query($conn, $sql);


if ($result === false) {
    echo mysqli_error($conn);
} else {
    $products = mysqli_fetch_assoc($result);
}

mysqli_close($conn);

?>

With this one, I'm getting "Unknown column 'TVs' in 'where clause' " error.

 

I have already tried so many ways and I'm getting really desperate. I tried this way:

$sql = "SELECT * FROM product_details WHERE type = 'TVs';
$result = mysqli_query($conn, $sql);

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']." Kč"."</div>
					<button type='submit' class='buy'>Do košíku</button>
				</form>
		   	  </div>";
        }

it worked but of course showed TVs in each category.

And if I go with

$sql = "SELECT * FROM `product_details` ORDER BY type";

$result = mysqli_query($conn, $sql);

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']." Kč"."</div>
                    <button type='submit' class='buy'>Do košíku</button>
                </form>
                 </div>";
        }

it works fine too but shows all of the products from the database.

Help me please, I really don't know how to deal with this.

Thank you all a lot for any help.

Link to comment
https://forums.phpfreaks.com/topic/317091-please-help-me-with-categorizing/
Share on other sites

35 minutes ago, Sisa said:

I have already tried so many ways and I'm getting really desperate. I tried this way:

$sql = "SELECT * FROM product_details WHERE type = 'TVs';
$result = mysqli_query($conn, $sql);

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']." Kč"."</div>
					<button type='submit' class='buy'>Do košíku</button>
				</form>
		   	  </div>";
        }

it worked but of course showed TVs in each category.

Of course it showed TVs in each category - that's what you selected. Why is that wrong?

- - -

I would advise you add a category table and store the catgory id in the product records and not repeat the name in every product of that type.

   +-------------+        +-----------------+
   | product     |        | category        |
   +-------------+        +-----------------+
   | id          |   +----| id              |
   | name        |   |    | cat_name        |
   | cat_id      |>--+    | cat_image       |
   | code        |        | cat_price       |
   | price       |        +-----------------+
   | image       |        
   +-------------+        

 

  • Like 1
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.