在带有组头的表中显示mysql数据 [英] Displaying mysql data in tables with group headers
问题描述
我有一个SQL,该SQL返回mysql表中的记录.每个记录都有许多字段,其中一个称为类型"的字段即样本的类型.所有样本大致分为六类.我正在显示表中的所有记录.但是我想按类型"对数据进行分组,并以类型作为标题显示单独的表.目前,我使用此脚本.
I have an SQL which returns records in a mysql table. Each record has many fields, and one of the field called "type" which is the type of sample. All samples are broadly classified into six types. I am displaying all the records in the table. But I want to group the data by 'type' and show separate tables with type as side heading. At present I use this script.
echo "<table class='wqtable'><tr><th>USIN</th><th>Type</th><th>Date of Coll.</th> <th>Location</th><th>Description</th><th>H3</th><th>Cs</th><th>Sr</th><th>Analyst</th></tr>";
while($data=mysql_fetch_array($result)){
echo "<tr>";
echo "<td>".$data['usin']."</td>";
echo "<td>".$data['type']."</td>";
echo "<td>".$data['doc1']."</td>";
echo "<td>".$data['location']."</td>";
echo "<td>".$data['description']."</td>";
echo "<td>".$data['h3']."</td>";
echo "<td>".$data['cs']."</td>";
echo "<td>".$data['sr']."</td>";
echo "<td>".$data['name']."</td>";
echo"</tr>";
}
echo "</table>";
这将所有记录显示在一个表中.当显示所有在类型字段中具有相同值的记录时,我想破坏表,并为下一种类型的样品开始一个新表. (例如组标题),除了运行单独的SQL之外,还有其他简便的方法吗?
This displays all the records in one table. I want to break the table when all the records with same value in type field is displayed and begin a new table for next type of samples. (like group header) Is there any easy way other than running separate SQLs?
推荐答案
确保您的SQL查询为ORDER BY type
,然后在您的while
循环中可以执行以下操作:
Make sure your SQL query is ORDER BY type
and then in your while
loop you could do this:
$lastType = '';
echo '<table>';
while($data=mysql_fetch_array($result)) {
if($lastType != '' && $lastType != $data['type']) {
echo '</table>';
echo '<table>';
}
echo "<tr>";
echo "<td>".$data['usin']."</td>";
echo "<td>".$data['type']."</td>";
echo "<td>".$data['doc1']."</td>";
echo "<td>".$data['location']."</td>";
echo "<td>".$data['description']."</td>";
echo "<td>".$data['h3']."</td>";
echo "<td>".$data['cs']."</td>";
echo "<td>".$data['sr']."</td>";
echo "<td>".$data['name']."</td>";
echo"</tr>";
$lastType = $data['type'];
}
echo '</table>';
我尚未对其进行测试,但是每当type
进行更改时,它都会插入一个闭合和打开的table
标记(您显然可以在其中添加任何其他内容).
I've not tested it but it whenever the type
changes it will insert a closing and opening table
tag (you can obviously add whatever else you want in there).
这篇关于在带有组头的表中显示mysql数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!