分类MySQL数据到单独的HTML表格? [英] Categorizing mysql data into seperate html tables?
本文介绍了分类MySQL数据到单独的HTML表格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个数据库查询,返回项ID,ITEMNAME,类别ID和类别名称。我被他们的类别名称试图组我的结果到单独的HTML表格所以最终的结果看起来是这样的:
I have a database query that returns ItemID, ItemName, CategoryID and CategoryName. I am trying to group my results into separate html tables by their CategoryName so the end result looks something like this:
________________________________
|____________CategoryName________|
| ItemName | |
|__________| |
| ItemName | |
|__________| |
| ItemName | |
|__________|_____________________|
________________________________
|____________CategoryName________|
| ItemName | |
|__________| |
| ItemName | |
|__________| |
| ItemName | |
|__________|_____________________|
目前我能输出我的数据到一个表,但我不确定如何去休息。
看来我有更好的ASCII艺术技能比PHP技能:/
Currently I can output my data into one table, but I am uncertain how to go about the rest. Seems i have better ascii art skills than php skills :/
推荐答案
的HTML是关闭的,但这应该让你开始:
The HTML is off, but this should get you started:
<?php
$query = 'SELECT CategoryID, CategoryName, ItemID, ItemName
FROM tableName
ORDER BY CategoryID';
$result = mysql_query($query);
if (!$result || mysql_num_rows($result) == 0) {
echo "No rows found";
exit;
}
$lastCatID = 0; //or some other invalid category ID
while ($row = mysql_fetch_assoc($result)) {
if($lastCatID != $row['CategoryID']) {
//starting a new category
if($lastCatID != 0) {
//close up previous table
echo '</table>';
}
//start a new table
echo '<table><th><td colspan="2">Category '
. $row['CategoryName'] .'</td></th>';
$lastCatID = $row['CategoryID'];
}
echo '<tr><td>' . $row['ItemName'] . '</td><td></td></tr>';
}
if($lastCatID != 0) {
//close up the final table
echo '</table>';
}
mysql_free_result($result);
?>
这篇关于分类MySQL数据到单独的HTML表格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文