2010-06-04 20 views
6

我的數據庫具有以下設置PHP和MySQL:使用組由類別

productid | productname | category id 

,我想輸出他們像這樣:

category #1 
item 1 
item 2 
item 3 

category #2 
item 1 
item 2 
item 3 

我用組由它們組合在一起,工作正常,但我想遍歷每個組並顯示該組的內容。我將如何做到這一點?

+0

發佈您正在使用的查詢,以便有人可以提供PHP將數據拉出。 – 2010-06-04 19:38:22

+0

我沒有查詢,我的問題是使用什麼查詢。這篇文章包括我的數據庫設置(包含的數據)以及我希望如何顯示,還應該包含哪些內容? – sam 2010-06-04 19:42:36

回答

15

我建議只是一個簡單的查詢來獲取所有的行,按類別ID排序。僅當其類別的值從前一行更改時才輸出該類別。

<?php 

$stmt = $pdo-> query("SELECT * FROM `myTable` ORDER BY categoryID"); 

$current_cat = null; 
while ($row = $stmt->fetch()) { 
    if ($row["categoryID"] != $current_cat) { 
    $current_cat = $row["categoryID"]; 
    echo "Category #{$current_cat}\n"; 
    } 
    echo $row["productName"] . "\n"; 
} 

?> 
2

你想要的是按類別排序,而不是將它們分組。

SELECT * FROM MyTable 
ORDER BY category_id, product_id 

當您迭代整個列表時,只要category_id發生變化就輸出一個新的標題。

0

最簡單的方法可能是拉出類別列表,遍歷並拉取附加的產品。 (即幾個查詢。)

例如(僞):

$result = fetch_assoc("SELECT category_id FROM categories"); 
foreach($result as $row) 
{ 
    echo $row['category_id']; 
    $result2 = fetch_assoc("SELECT product_id FROM products"); 
    foreach($result2 as $row2) 
    { 
    echo $row2['product_id']; 
    } 
} 

如果你想做到這一切在一個查詢中,你可以這樣做:

$result = fetch_assoc("SELECT product_id, category_id FROM products p JOIN categories c ON p.category_id = c.category_id ORDER BY c.category_id ASC"); 
$last = null; 
foreach($result as $row) 
{ 
    # Output the category whenever it changes 
    if($row['category_id'] != last) 
    { 
    echo $row['category_id']; 
    $last = $row['category_id']; 
    } 
    echo $row['item_id']; 
}

然後你可以遍歷通過結果集並隨時更改類別名稱。

在從數據庫獲取數據之前,可能會有更復雜,更優雅的方式來編寫SQL來完成所有工作,但我並不聰明。 ;)

注意:示例使用僞代碼。我使用MySQL抽象類,它的工作原理是:

$db->query("SELECT stuff"); 
$db->multi_result(); // returns 2d-associative array

那麼我可以foreach($db->multi_result() as $row),這是超級懶惰和真棒。

我可以發佈抽象層的代碼,如果你喜歡。

7

這應該工作:

$categories = array(); 
$result= mysql_query("SELECT category_id, product_name FROM `table` GROUP BY `catagory_id` ORDER BY `catagory_id`"); 
while($row = mysql_fetch_assoc($result)){ 
    $categories[$row['category_id']][] = $row['product_name']; 
} 

// any type of outout you like 
foreach($categories as $key => $category){ 
    echo $key.'<br/>'; 
    foreach($category as $item){ 
     echo $item.'<br/>'; 
    } 
} 

輸出,你可以自己風格。您只需將所有內容添加到多維數組中,並將類別ID作爲第一級密鑰。

編輯:結果數組可能是這樣的:

$categories = array(
    'cateogy_id_1' => array(
     1 => 'item_1', 
     2 => 'item_2', 
     ... 
    ), 
    'cateogy_id_2' => array(
     1 => 'item_1', 
     2 => 'item_2', 
     ... 
    ), 
    .... 
); 
2

這種方法不需要將數據發送到CATEGORY_ID進行排序。

您可以使用php將每個類別在一個嵌套數組中組合在一起。 在此之後,數據可以很容易地顯示在表格中,傳遞給圖表/圖表庫等​​等。

<?php 
$stmt = $pdo-> query("SELECT * FROM myTable ORDER BY categoryID"); 


$categories = []; //the array to hold the restructured data 

//here we group the rows from different categories together 
while ($row = $stmt->fetch()) 
{ 
    //i'm sure most of you will see that these two lines can be performed in one step 
    $cat = $row["categoryID"]; //category id of current row 
    $categories[$cat][] = $row; //push row into correct array 
} 

//and here we output the result 
foreach($categories as $current_cat => $catgory_rows) 
{ 
    echo "Category #{$current_cat}\n"; 

    foreach($catgory_rows as $row) 
    { 
     echo $row["productName"] . "\n"; 
    } 
} 
?>