1

I am creating a cooking app where there are categories and recipe within per category. So when the user tap the category BREAKFAST, all recipe will display that is assigned to BREAKFAST. To display that, it will depend in my php file but for the meantime it displays all same recipe(the whole recipe in the database) in every category and does not show the recipe depending on their assigned category.

I need your help how to display recipes depending on which category they are assigned(Example: Breakfast: Pancakes, soup. Lunch: soup,pizza. Dinner: pizza, burger). Reminder: the recipe can be also assigned to different categories... Thanks !

PHP FILE

 <?php
 require_once 'include/DB_Connect.php';
 $db = new Db_Connect();
 $conn = $db->connect();
 if(isset($_GET['recipeId'])){
 $id = $_GET['recipeId'];
    $result = $conn->query("SELECT * FROM recipe where id=$id");
 }else{
    $result = $conn->query("SELECT * FROM recipe");
 }

 if ($result->num_rows > 0) {
    $list = array();
 while($row = $result->fetch_assoc()) {
    array_push($list, $row);
 }
  echo json_encode(array('result' => $list));
 } else {
    echo json_encode("no result");
 }
 ?>

Many-to-Many

enter image description here

PHPMYADMIN

enter image description here

Preview of categories

enter image description here

Recipe(displays the same whole recipe from database to each categories)

enter image description here

M. M. Human
  • 118
  • 2
  • 3
  • 14

1 Answers1

0

It looks like 'id' is unique (so far) for each of you DB entries. So a query of id=?

$result = $conn->query("SELECT * FROM recipe where id=$id");

is only going to return one entry.

I'm not sure which column is your category column, if it's 'recipe_id' try:

$result = $conn->query("SELECT * FROM recipe where recipe_id=$id");

If it's course_type_id try:

$result = $conn->query("SELECT * FROM recipe where course_type_id=$id");
gmfm
  • 659
  • 8
  • 21