我的mysql数据库有以下内容:
productid | productname | category
1 XYZ News
2 ZYX News
3 vcb Info
我想按类别
按类别在表格组中显示它们Category : News
xyz
zyx
Category : Info
vcb
Etc....
目前我正在使用
$sql="SELECT * FROM database Group By category";
$result = mysql_query($sql);
if(mysql_num_rows($result) > 0) {
echo "<table cellspacing='0' cellpadding='0'>
<thead>
<tr>
<th>id</th>
<th>name</th>
<th>catergory</th>
</tr>
</thead>";
$i = 0;
while($row = mysql_fetch_array($result))
{
if($i%2 == 0)
{
$class = 'even';
}
else
{
$class = 'odd';
}
{
echo "<tbody>";
echo "<tr class='$class'>";
echo "<td>" . $row['productid'] . "</td>";
echo "<td style='font-weight: bold; text-align: center;'>" . $row['productname '] . "</td>";
echo "<td style='font-weight: bold; text-align: center;'>" . $row['category '] . "</td>";
echo "</tr>";
}
$i++;
}
echo "</tbody>";
echo "</table>";
} else {
echo "";
}
答案 0 :(得分:0)
您可以使用MySQL的GROUP_CONCAT()
,它返回逗号连接字符串 -
SELECT GROUP_CONCAT(productname) as productname FROM database Group By category
然后您的$row['productname']
将显示为XYZ,ZYX
。
然后您可以使用php格式化数据 - 即。
echo str_replace(',','<br />',$row['productname']);