Tuesday, 15 February 2011

php - Displaying mysql data in tables with group headers -



php - Displaying mysql data in tables with group headers -

i have sql returns records in mysql table. each record has many fields, , 1 of field called "type" type of sample. samples broadly classified 6 types. displaying records in table. want grouping info 'type' , show separate tables type side heading. @ nowadays utilize 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>";

this displays records in 1 table. want break table when records same value in type field displayed , begin new table next type of samples. (like grouping header) there easy way other running separate sqls?

make sure sql query order type , in while loop 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>';

i've not tested whenever type changes insert closing , opening table tag (you can add together whatever else want in there).

php mysql sql

No comments:

Post a Comment