<?php
// simulated data
$data = array( array('part1' => 'x', 'part2' => '1', 'part3' => 'I'),
array('part1' => 'x', 'part2' => '1', 'part3' => 'II'),
array('part1' => 'x', 'part2' => '2', 'part3' => 'III'),
array('part1' => 'x', 'part2' => '2', 'part3' => 'IV'),
array('part1' => 'y', 'part2' => '3', 'part3' => 'V'),
array('part1' => 'y', 'part2' => '3', 'part3' => 'VI'),
array('part1' => 'y', 'part2' => '4', 'part3' => 'VII'),
array('part1' => 'y', 'part2' => '4', 'part3' => 'VIII'));
$lastpart1 = '';
$lastpart2 = '';
$lastpart3 = '';
foreach($data as $rec) {
// get the parts
$part1 = $rec['part1'];
$part2 = $rec['part2'];
$part3 = $rec['part3'];
// do it
if($part1 != $lastpart1) {
if($lastpart1 != '') {
echo "</table>\r\n";
}
echo "$part1:\r\n";
echo "<table border='1'>\r\n";
$lastpart1 = $part1;
}
if($part2 != $lastpart2) {
if($lastpart2 != '') {
echo "</tr>\r\n";
}
echo "<tr>\r\n";
echo "<td>$part1</td>\r\n";
$lastpart2 = $part2;
}
echo "<td>$part3</td>\r\n";
}
echo "</tr>\r\n";
echo "</table>\r\n";
?>
giver:
x:
<table>
<tr>
<td>x</td>
<td>I</td>
<td>II</td>
</tr>
<tr>
<td>x</td>
<td>III</td>
<td>IV</td>
</table>
y:
<table>
</tr>
<tr>
<td>y</td>
<td>V</td>
<td>VI</td>
</tr>
<tr>
<td>y</td>
<td>VII</td>
<td>VIII</td>
</tr>
</table>