I have nested foreach
loops to show MySQL records in nested array.
while ($row = mysql_fetch_array($result)) {
$departments[$row['department_name']][$row['group_name']][] = $row['lab_test'] . ',' . $row['result'] . ',' . $row['um'] . ',' . $row['reference_interval'];
}
foreach($departments as $department_name => $groups){
echo "<tr class='department'><td>" . $department_name . "</td></tr>";
foreach($groups as $group_name => $tests){
echo "<tr class='test_group'><td>" . $group_name . "</td></tr>";
foreach($tests as $test){
list($test_name, $test_result, $test_um, $test_interval) = explode(',', $test);
echo "<tr class='test_item'><td>" . $test_name . "</td><td>" . $test_result . "</td><td>" . $test_um . "</td><td>" . $test_interval . "</td></tr>";
}
}
echo "<tr><td> </td><td> </td><td> </td><td> </td></tr>";
}
This works OK, but I want to change it so that $group_name
only shows if it is not equal to $test_name
. The problem being $test_name
is not initiated until the next foreach
loop.