[英]PHP & MYSQL: using group by for categories
我的數據庫具有以下設置
productid | productname | category id
我想像這樣輸出它們:
category #1
item 1
item 2
item 3
category #2
item 1
item 2
item 3
我把它們組合在一起使用,並且工作正常,但我想循環遍歷每個組並顯示該組的內容。 我該怎么做?
我建議只是一個簡單的查詢來獲取所有行,按類別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";
}
?>
這應該工作:
$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',
...
),
....
);
你想要的是按類別訂購它們,而不是將它們分組。
SELECT * FROM MyTable
ORDER BY category_id, product_id
當您遍歷列表時,只要category_id更改,只需輸出一個新標頭。
此方法不需要按category_id對數據進行排序。
您可以使用php在嵌套數組中將每個類別組合在一起。 在此之后,數據可以輕松地顯示在表格中,傳遞到grap /圖表庫等...
<?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";
}
}
?>
最簡單的方法可能是拉出類別列表,迭代並拉動其附加產品。 (即幾個查詢。)
例如(偽代碼):
$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之前,可能有一種更復雜,更優雅的方式來編寫SQL以執行所有操作,但我並不那么聰明。 ;)
注意:示例使用偽代碼。 我使用的mysql抽象類的工作方式如下:
$db->query("SELECT stuff"); $db->multi_result(); // returns 2d-associative array
然后我可以foreach($db->multi_result() as $row)
,這是超級懶惰和真棒。
如果你願意,我可以發布抽象層的代碼。
$stmt = $dbConnection->prepare("SELECT exam_id, COUNT(report_id) FROM bug_report GROUP BY exam_id; ");
//$stmt->bind_param('s',$exam_id);
$stmt->execute();
$extract = $stmt->get_result();
$count = $extract->num_rows;
if($count){
while($rows = $extract->fetch_assoc()){
$exam_id = $rows["exam_id"];
switch($exam_id){
case "jee_questions":
$jeeBugCount = $rows["COUNT(report_id)"];
break;
case "gate_questions":
$gateBugCount = $rows["COUNT(report_id)"];
break;
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.