Answer the question
In order to leave comments, you need to log in
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>
$categories = mysqli_query( $connection, "SELECT * FROM `products_categories` WHERE `id` = " . (int) $_GET['id'] );
if( mysqli_num_rows($categories) <= 0 )
{
?>
Категория не найдена!
<?php
} else
<?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))
{
Answer the question
In order to leave comments, you need to log in
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 questionAsk a Question
731 491 924 answers to any question