Instead of directly ouputting the result from the database try formatting i t first. For example you can do something like that:
$waitingRooms = array();
while ($row_waitingrooms = mysql_fetch_assoc($result_waitingrooms)){
$waitingRoomPK = $row_waitingrooms['waitingroom_pk'];
if (!isset($waitingRooms[$waitingRoomPK])) {
$waitingRooms[$waitingRoomPK] = array(
'waitingroom_pk' => $waitingRoomPK,
'waitingroom' => $row_waitingrooms['waitingroom'],
'cases' => array(),
//.....all the data for a waitingroom
);
}
$waitingRooms[$waitingRoomPK]['cases'][] = array(
'case_pk' => $row_waitingrooms['case_pk'],
'patient_icon' => $row_waitingrooms['patient_icon'],
'first_name' => $row_waitingrooms['first_name'],
//.......all the data for a patient
);
}
Now $waitingRooms is array with data for each waiting room, and a 'cases' subarray with data for each patient in the room From here 2 simple nested foreach statements will output the desired html for the accordions
Here is some ways to improve your code though:
- Do not use mysql_* functions, they are deprecated. Those huge red warnings in the docs are not just for looks. User mysqli or PDO (both linked in the docs)
- Use actual joins, that alternative join syntax in the query hurts readability
- Format your sql and html code. I bet lots of people skipped your question when they saw these horisontal scrollbars in the code segments
I hope you will agree that that:
<?php while($row_waitingrooms = mysql_fetch_assoc($result_waitingrooms)): ?>
<h6><?= $row_waitingrooms['waitingroom']; ?></h6>
<div>
<p>
<span class='text'>
<table width='100%'>
<tr>
<td>
<input name='case' id='case_<?= $row_waitingrooms['case_pk']; ?>' type='radio' value='<?= $row_waitingrooms['case_pk']; ?>' />
</td>
<td width='65' align='left'>
<div class='case_list'>
<img src='images/case_icons/<?= $row_waitingrooms['patient_icon']; ?>' width='44' height='45' />
</div>
</td>
<td width='350' align='left'>
<?= $row_waitingrooms['first_name'] . ' ' . $row_waitingrooms['last_name']; ?>
Gender: <?= $row_waitingrooms['gender']; ?>
Age: <?= $row_waitingrooms['age']; ?>
<br />
Presenting Complaint: <?= $row_waitingrooms['presenting_complaint']; ?>
</td>
</tr>
</table>
</p>
</div>
<?php endwhile; ?>
is way better than that:
while ($row_waitingrooms = mysql_fetch_assoc($result_waitingrooms)){
echo "<h6>" . $row_waitingrooms['waitingroom'] . "</h6><div><p><span class='text'><table width='100%'><tr><td><input name='case' id='case_" . $row_waitingrooms['case_pk'] . "' type='radio' value='" . $row_waitingrooms['case_pk'] . "' /></td><td width='65' align='left'><div class='case_list'><img src='images/case_icons/" . $row_waitingrooms['patient_icon'] . "' width='44' height='45' /></div></td><td width='350' align='left'>" . $row_waitingrooms['first_name'] . ' ' . $row_waitingrooms['last_name'] . '  Gender: ' . $row_waitingrooms['gender'] . ' Age: ' . $row_waitingrooms['age'] . '<br />Presenting Complaint: ' . $row_waitingrooms['presenting_complaint'] . "</td></tr></table></p></div>";
}
And you get the bonus to see the unclosed span tag :)