我的数据库具有以下设置PHP和MySQL:使用组由类别
productid | productname | category id
,我想输出他们像这样:
category #1
item 1
item 2
item 3
category #2
item 1
item 2
item 3
我用组由它们组合在一起,工作正常,但我想遍历每个组并显示该组的内容。我将如何做到这一点?
我的数据库具有以下设置PHP和MySQL:使用组由类别
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";
}
?>
你想要的是按类别排序,而不是将它们分组。
SELECT * FROM MyTable
ORDER BY category_id, product_id
当您迭代整个列表时,只要category_id发生变化就输出一个新的标题。
最简单的方法可能是拉出类别列表,遍历并拉取附加的产品。 (即几个查询。)
例如(伪):
$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)
,这是超级懒惰和真棒。
我可以发布抽象层的代码,如果你喜欢。
这应该工作:
$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',
...
),
....
);
这种方法不需要将数据发送到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";
}
}
?>
发布您正在使用的查询,以便有人可以提供PHP将数据拉出。 – 2010-06-04 19:38:22
我没有查询,我的问题是使用什么查询。这篇文章包括我的数据库设置(包含的数据)以及我希望如何显示,还应该包含哪些内容? – sam 2010-06-04 19:42:36