2017-09-02 47 views
1

好吧,所以我有兩個表類別和菜餚。每個類別都有一個ID。每道菜屬於一個特定的類別。現在我想要的是將屬於特定類別的所有菜餚分組在一起。因此,所有的湯都將在湯類[{捲心菜,價格,描述,...},{番茄,價格,描述,...}]中。雞羣中的所有雞等等。我現在所做的是這樣的:PHP,在一個數組中獲取特定類型的所有項目

$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數據嗎?

+0

什麼是您用來保存類別名稱的字段?請告訴 –

+0

您的問題出現在'$ AllDishes [] ='中,因爲您不斷爲每個類別添加「$ AllDishes」變量。您必須爲每次迭代「更改」存儲變量。 – insider

回答

1

1.使用JOIN做,在單個查詢(最佳解決方案): -

$query = "Select Dishes.*,Category.id as cat_id from Category LEFT JOIN Dishes on Dishes.cat_id = Category.id"; 
$Listings = $mysqli->query($query); 
$AllDishes = array(); 
while($row = $Listings->fetch_assoc()) { 
    $AllDishes[$row['cat_id']][] = array(
     'id' => $row['id'], 
     'Name' => $row['Name'], 
     'Description' => $row['Description'], 
     'cat_id' => $row['cat_id'], 
     'Price' => $row['Price'], 
     'ImagePath' => $row ['ImagePath'] 
    ); 
} 
print_r($AllDishes); 

如果你想在你的編碼方式做那麼: -

2.Distinguish他們基於類別的id: -

$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'] 
     ); 
    } 

} 

3.Distinguish他們基於類別的name: -

$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'] 
     ); 
    } 

} 

注: - 請更改列名在第三個解決方案中(因爲我不知道你需要存儲什麼列名稱類別名稱)。

+0

敢問我使用第一個SQL語句的第三個選項:) –

+0

對不起 - 使用1條SQL語句加入。 –

+0

@NigelRen添加了.check一次 –

0

我不知道,但我認爲這裏將是你的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'] 
    ); 
} 
0

您可以使用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 
    } 
相關問題