Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/281.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP,在一个数组中获取特定类型的所有项_Php_Mysql - Fatal编程技术网

PHP,在一个数组中获取特定类型的所有项

PHP,在一个数组中获取特定类型的所有项,php,mysql,Php,Mysql,好的,我有两张桌子和两道菜。每个类别都有一个id,每个菜都属于一个特定的类别。现在我想要的是,所有属于某一类别的菜肴都要组合在一起。所以所有的汤都会在汤数组中[{colla,Price,Description,…},{Tomato,Price,Description,…}]。鸡阵中的所有鸡等等。我目前正在做的是: $query = "Select id from Category"; $Id = $mysqli->query($query); while($row =

好的,我有两张桌子和两道菜。每个类别都有一个id,每个菜都属于一个特定的类别。现在我想要的是,所有属于某一类别的菜肴都要组合在一起。所以所有的汤都会在汤数组中[{colla,Price,Description,…},{Tomato,Price,Description,…}]。鸡阵中的所有鸡等等。我目前正在做的是:

    $query = "Select id from Category";
    $Id =  $mysqli->query($query);

while($row = $Id->fetch_assoc()) {
    $idlist[] = $row["id"];
}





foreach ($idlist as $id){

    $query = "SELECT * from Dishes where Dishes.cat_id = '$id'";
    $Listings = $mysqli->query($query);

    while($row = $Listings->fetch_assoc()) {
        $AllDishes[] = array(
           'id' => $row['id'],
           'Name' => $row['Name'],
           'Description' => $row['Description'],
           'cat_id' => $row['cat_id'],
           'Price' => $row['Price'],
           'ImagePath' => $row ['ImagePath']
          );
    }

}

但这会导致所有的菜都集中在一起。我如何根据菜品的类别来区分菜品?这也是构造JSON数据的正确方法吗

我不确定,但我认为这将是您的while循环,以获得预期的结果

 $AllDishes = array();
 while($row = $Listings->fetch_assoc()) {
    $AllDishes[$row['Name']][] = array(
       'id' => $row['id'],
       'Name' => $row['Name'],
       'Description' => $row['Description'],
       'cat_id' => $row['cat_id'],
       'Price' => $row['Price'],
       'ImagePath' => $row ['ImagePath']
      );
}
1.使用
JOIN
在单个查询中执行此操作(最佳解决方案):-

如果您想以您的编码方式执行,那么:-

2.根据类别的
id
区分:-

3.根据类别的名称区分:-


注意:-请在第三个解决方案中更改列名(因为我不知道必须存储类别名称的列名)。

您可以使用SQL联接,而不是两个单独的SQL查询

I hope the following may help you.



    $query = "SELECT * FROM Dishes INNER JOIN Category
                                   ON Dishes.id = Category.id ORDER BY Dishes.id";
    $res = $mysqli->query($query);      
    while($row = $res->fetch_assoc())
   {
       $AllDishes[$$row['category_name']][] = array(
                                             'id' => $row['id'],
                                             'Name' => $row['Name'],
                                             'Description' => 
                                                  $row['Description'],
                                             'cat_id' => 
                                                  $row['cat_id'],
                                             'Price' => $row['Price'],
                                             'ImagePath' => 
                                                  $row['ImagePath']
                                           );//Array Close   
   }

用于保存类别名称的字段是什么?请告诉我您的问题出在
$alldisks[]=
中,因为您一直在为每个类别添加
$alldisks
变量。每次迭代都必须“更改”存储变量。我敢要求第三个使用1条SQL语句的选项吗:)对不起-使用1条带有联接的SQL语句。@NigelRen添加了该选项。请检查一次
$query = "Select id from Category";//change column name
$Id =  $mysqli->query($query);

while($row = $Id->fetch_assoc()) {
    $idlist[] = $row['id'];
}

foreach ($idlist as $id){
    $query = "SELECT * from Dishes where Dishes.cat_id = '$id'";
    $Listings = $mysqli->query($query);

    while($row = $Listings->fetch_assoc()) {
        $AllDishes[$id][] = array(
           'id' => $row['id'],
           'Name' => $row['Name'],
           'Description' => $row['Description'],
           'cat_id' => $row['cat_id'],
           'Price' => $row['Price'],
           'ImagePath' => $row ['ImagePath']
          );
    }

}
$query = "Select id,category_name from Category";//change column name
$Id =  $mysqli->query($query);

while($row = $Id->fetch_assoc()) {
    $idlist[] = $row;
}

foreach ($idlist as $id){
    $id = $id['id'];
    $category_name = $id['category_name']; //change column-name
    $query = "SELECT * from Dishes where Dishes.cat_id = '$id'";
    $Listings = $mysqli->query($query);

    while($row = $Listings->fetch_assoc()) {
        $AllDishes[$category_name][] = array(
           'id' => $row['id'],
           'Name' => $row['Name'],
           'Description' => $row['Description'],
           'cat_id' => $row['cat_id'],
           'Price' => $row['Price'],
           'ImagePath' => $row ['ImagePath']
          );
    }

}
I hope the following may help you.



    $query = "SELECT * FROM Dishes INNER JOIN Category
                                   ON Dishes.id = Category.id ORDER BY Dishes.id";
    $res = $mysqli->query($query);      
    while($row = $res->fetch_assoc())
   {
       $AllDishes[$$row['category_name']][] = array(
                                             'id' => $row['id'],
                                             'Name' => $row['Name'],
                                             'Description' => 
                                                  $row['Description'],
                                             'cat_id' => 
                                                  $row['cat_id'],
                                             'Price' => $row['Price'],
                                             'ImagePath' => 
                                                  $row['ImagePath']
                                           );//Array Close   
   }