2013-03-23 79 views
1

循環嵌套數組我有2個MySQL表,在那裏我有以下SQL語句提取數據:創建與PHP和MySQL

分類:SELECT cat_id, cat_name FROM categories

產品:SELECT pro_id, pro_name, pro_category FROM products

當然,pro_categorycat_id

外鍵我想達到以下JSON數據:

"categories":[ 
    { 
     "id": 1, 
     "name": "Guitars", 
     "products":[ 
      { 
       "id": 1, 
       "name": "Fender Statocaster" 
      }, 
      { 
       "id": 2, 
       "name": "Gibson Les Paul" 
      } 
     ] 
    }, 
    { 
     "id": 2, 
     "name": "Basses", 
     "products":[ 
      { 
       "id": 3, 
       "name": "Fender Jazz Bass" 
      }, 
      { 
       "id": 4, 
       "name": "MusicMan StingRay" 
      } 
     ] 
    } 
] 

所以我必須做一個類別的數組,併爲每個元素,產品的子數組(然後我會去與json_encode())。

通過類別循環並創建第一個數組很容易,但我完全停留在如何創建產品子數組...這是我的代碼,問號是我停下來的地方,因爲我不知道該怎麼辦:

$categories = array(); 
while ($row_rsCategories = mysql_fetch_assoc($rsCategories)) { 
    $categories[] = array(
     'id' => $row_rsCategories['cat_id'], 
     'name' => $row_rsCategories['cat_name'], 
     'products' => array(
      ??????? 
     ), 
    ); 
} 

請問,你能幫忙嗎?
謝謝!

回答

4
$categories = array(); 
while ($row_rsCategories = mysql_fetch_assoc($rsCategories)) { 

    $product_array = array(); 
    $product_array_query = mysql_query("SELECT pro_id, pro_name, pro_category FROM products WHERE pro_category = '".$row_rsCategories['cat_id']."'"); 

    while($product_array_fetch = mysql_fetch_array($product_array_query)) { 

     $product_array[] = array("id"=>$product_array_fetch['pro_id'],"name"=>$product_array_fetch['pro_name']); 

    }     

    $categories[] = array(
     'id' => $row_rsCategories['cat_id'], 
     'name' => $row_rsCategories['cat_name'], 
     'products' => $product_array, 
    ); 
} 
+0

Thnaks,這個作品!產品查詢是否會影響性能? – Ivan 2013-03-23 13:15:41

+0

@Ivan:我想你將不得不使用inner for循環。請考慮使用'PHP PDO'或'mysqli extentions'來代替默認的'mysql_ *'函數,因爲php社區不再推薦它們,並且容易受到像'mysql injection'這樣的安全威脅的攻擊。 – mithunsatheesh 2013-03-23 13:28:04

0
$categories = array(); 
while ($row_rsCategories = mysql_fetch_assoc($rsCategories)) { 
    $categories[$row_rsCategories['cat_id']] = array(
     'id' => $row_rsCategories['cat_id'], 
     'name' => $row_rsCategories['cat_name'], 
     'products' => array(), 
    ); 
} 

while ($row_rsProducts = mysql_fetch_assoc($rsProducts)) { 
    $categories[$row_rsProducts['pro_category']]['products'][] = array(
     'id' => $row_rsProducts['pro_id'], 
     'name' => $row_rsProducts['pro_name'], 
    ); 
} 
-1

我使用JOIN查詢其類別ID訂購產品,使產品所屬的類別將走到一起。如果看到cat_id更改,則遍歷結果數組並創建新的類別。

$db = new PDO($connectionString); 

$sql = "SELECT * FROM products t1 LEFT JOIN categories t2 ON t1.pro_category = t2.cat_id ORDER BY t2.cat_id"; 
$stmt = $db->prepare($sql); 
if($stmt->execute()) 
{ 
    $rows = $stmt->fetchAll(); 
    $currentCategory = NULL; 
    $categories = array(); 
    foreach($rows as $r) 
    { 
     if ($currentCategory == NULL || $r['cat_id' != $currentCategory['id']) 
     { 
      $currentCategory = array(
       'id' => $r['cat_id'], 
       'name' => $r['cat_name'], 
       'products' => array(); 
      ); 
      $categories[] = $currentCategory; 
     } 
     $currentCategory['products'][] = array(
      'id' => 'pro_id', 
      'name' => 'pro_name', 
     ) 
    } 
} 
else 
{ 
    var_dump($stmt->errorInfo()); 
}