Novice Novice - 2 months ago 9
MySQL Question

I need help displaying data in php

I am just learning php and I have a problem with a project. I have a database in mysql (localhost) and in it I have 3 tables: categories, products and restaurants, categories and restaurants are linked to products. And when I try to display products of a specified category it doesn't display anything. The connection to the database is good and the queries are good so I have no idea what's wrong. Pleas help.

This is the index.php

<?php

include_once './database.php';
include_once './header.php';

<div class="content" align="center">


<div id="galery" class="content_block" style="height:400px;">
<div class="galery">
<img src="./img/gallery/6.jpg">
</div>
</div>
<div class="content_block" style="padding-bottom:10px;">


$query = "SELECT * FROM categories";
$result = mysqli_query($link, $query);
while ($row = mysqli_fetch_array($result)){


echo '<a href="categories.php?cat='.$row['category'].'"><div class="contentButton inline"><div class="catFood_img foodCategory_1"></div><div>'.$row['category'].'</div></div></a>';



</div>

<div class="content_block" style="height:190px" align="center">


$query = "SELECT * FROM restourants";
$result = mysqli_query($link, $query);
while ($row = mysqli_fetch_array($result)){
echo '<a href="categories.php?res='.$row['restourant'].'"><div class="restournats inline"><img src="./food/restourants/chens.jpg"></div></a>';

}

</div>
</div>



include_once './footer.php';

?>


And this is the code for categories.php



include_once './database.php';
include_once './header.php';




<div class="content" align="center">
<div class="content_block2">




$category= (int)(isset($_GET['id_category']) ? $_GET['id_category'] : '');

$query = "SELECT * FROM products WHERE products.id_category=$category";

$result = mysqli_query($link, $query);

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


echo '
<a href="dishes.php?id='.$row['id'].'"><div class="item inline">
<div class="food_img"><img src="'.$row['img'].'" /></div>
<div class="title">'.$row['name'].'</div>
<div class="price inline">Price: '.$row['price'].' €</div><div class="btn">Choose</div>
</div>
</a>';
}


</div>
</div>


include_once './footer.php';

Answer

In your index.php you are creating the link as

echo '<a href="categories.php?cat='.$row['category'].'"> 

(double check $row['category'] given above is the desired category id value you need)

But in your categories.php you are trying to get the category as

$category= (int)(isset($_GET['id_category']) ? $_GET['id_category'] : '');

Try $_GET['cat'] instead of $_GET['id_category']

Also try echo $category and echo $query to see if you are getting the desired values.

Comments