Display a list of categories with links
How can I redo a piece of code so that it displays a list of categories with links that would display products from a specific category? And the active category would be highlighted by formatting.
Now displays a list of categories with a list of products from the first category, where
$id_kategori=$list_kategori[0]['id_category'];
If the digit is changed, then it outputs the goods from another one, i.e. what is needed, only the replacement of the digit needs to be executed correctly.
I understand that there should be a cycle in which instead of a digit in $list_kategori[0] there must be a variable. I tried it like this
<?php
for ($i = 1; $i <= $cek; $i++) {
$id_kategori=$list_kategori[$i]['id_category'];
}```
products disappear, instead of the first category error ```Notice
: Undefined offset:
?>```
<?php
mysqli_query($koneksi,"SET NAMES utf8mb4");
$data=mysqli_query($koneksi,"select * from bot_shop_category order by name_category ASC");
$cek= mysqli_num_rows($data);
if($cek>0){
$list_kategori = array();
while ($row = mysqli_fetch_assoc($data)) {
$list_kategori[] = $row;
}
$id_kategori=$list_kategori[0]['id_category'];
foreach ($list_kategori as $row) {
?>
<div class="col-md-12">
<p><a href="<?= $list_kategori?>"><?=$row['name_category']?></p>
<hr>
</div>
<?php
}
}
else{
echo 'There are no categories yet';
}
?>
</div>
</div>
<div class="col-md-10">
<div class="row">
<?php
if(isset($_GET['id_category']) && $_GET['id_category']!=''){
$id_kategori=$_GET['id_category'];
}
$data=mysqli_query($koneksi,"select * from bot_shop_product join bot_shop_category on bot_shop_product.id_category=bot_shop_category.id_category where bot_shop_product.id_category='$id_kategori' order by bot_shop_product.id_category ASC ");
$cek= mysqli_num_rows($data);
if($cek>0){
$list_produk = array();
while ($row = mysqli_fetch_assoc($data)) {
$list_produk[] = $row;
}
foreach ($list_produk as $row) {
?>
<div class="col-md-4">
<div class="card" >
<a href="produk-detail.php?id=<?=$row['id_product']?>"><img src="/o220123/assets/img/produk/<?=$row['image']?>" class="card-img-top" alt="..."></a>
<div class="card-body">
<p class="card-text text-center">
<span style="color: #333;font-size: 90%"><?=$row['name_category']?></span><br><?=$row['name_product']?><br><b>Rb <?=number_format($row['price'])?></b>
</p>
</div>
</div>
</div>
<?php
}
}
else{
echo 'There are no products available in this category';
}
?>
</div>
</div>
</div>
</div>
In mysql standard Category tables bot_shop_category with columns:id_category, name_category and Goods bot_shop_product: id_product, id_category, name_product