R
R
Redoker2020-06-23 16:53:24
PHP
Redoker, 2020-06-23 16:53:24

How to display products by category?

Given: An
online store with product categories. Navigation buttons with links to a specific category of products.

Problem:
There is a page with categories where products from the database are loaded. I did the loading of goods, but all goods are loaded, regardless of the category.

<?php
            $categories = mysqli_query($connection, "SELECT * FROM `products_categories`");
        ?>

        <nav class="nav">
            <?php
                while( $cat = mysqli_fetch_assoc($categories) )
                {
                    ?>
                        <a class="nav__link" href= "categorie.php?id=<?php echo $cat['id']; ?>"><strong><?php echo $cat['title']; ?></strong></a>
                    <?php
                }
            ?>
        </nav>

The navigation code and a link to navigate through the categories is presented above.

$categories = mysqli_query( $connection, "SELECT * FROM `products_categories` WHERE `id` = " . (int) $_GET['id'] );
        
            if( mysqli_num_rows($categories) <= 0 )
            {
                ?>
                    Категория не найдена!
                <?php
            } else

The category output code is presented above.

<?php
                                            $sql = "SELECT * FROM `products`";
                
                                            $results_per_page = 6;
                                            $result = mysqli_query($connection, $sql);
                                            $number_of_results = mysqli_num_rows($result);

                                            $number_of_pages = ceil($number_of_results/$results_per_page);

                                            if (!isset($_GET['page']))
                                            {
                                                $page = 1;
                                            } else
                                            {
                                                $page = $_GET['page'];
                                            }

                                            $offset = ($page - 1) * $results_per_page;

                                            $sql = "SELECT * FROM `products` LIMIT $offset, $results_per_page";
                                            $result = mysqli_query($connection, $sql);

                                            while ($prod = mysqli_fetch_array($result))
                                            {

Product output code and pagination.

Product category table
5ef208834b666164861442.jpeg


Part of the product table structure
5ef208f969586425737445.jpeg


Solution:
It is necessary to make a transition by category, it is possible to add database conditions, but I do not quite understand what exactly needs to be corrected.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Michael Aniskin, 2020-06-30
@MichaelAniskin

Relationship between categories and products by type(categories) value and not by id
1 href= "category.php?type=<?php echo $cat['type']
2 "SELECT * FROM `products_category` WHERE `category` = " .(int) $_GET['type']

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question