我有以下 MySQL 查询:
$query_waitingrooms = "SELECT * FROM waitingroom, waitingroom_case_lookup, vcase, patient WHERE vcase.patient_fk = patient.patient_pk AND waitingroom.waitingroom_pk = waitingroom_case_lookup.waitingroom_fk AND vcase.case_pk = waitingroom_case_lookup.case_fk AND vcase.active = 'y' AND vcase.case_pk NOT IN (SELECT case_fk FROM user_case_lookup WHERE user_id = '$user_id')";
$result_waitingrooms = mysql_query($query_waitingrooms, $connection) or die(mysql_error());
打印结果:
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>";
}
正如预期的那样,这会导致表“waitingroom_case_lookup”中的每条记录都有一个手风琴,每个手风琴包含一个“案例”。我所追求的是为每个“候诊室”配备一个手风琴,每个“候诊室”都包含来自表 vcase 和患者的相关病例数据。
目前,“waitingroom_case_lookup”表为:
CREATE TABLE waitingroom_case_lookup (
waitingroom_case_lookup_pk int(9) NOT NULL AUTO_INCREMENT,
waitingroom_fk int(3) NOT NULL,
case_fk int(3) NOT NULL,
PRIMARY KEY (waitingroom_case_lookup_pk)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
--
-- Dumping data for table 'waitingroom_case_lookup'
--
INSERT INTO waitingroom_case_lookup (waitingroom_case_lookup_pk, waitingroom_fk, case_fk) VALUES
(1, 1, 1),
(2, 1, 2),
(3, 2, 3);
看来我需要在每个手风琴候诊室中循环播放这些箱子,<span class='text'>
我</p></div>
有什么建议可以做到这一点吗?
编辑
感谢NoBBY,我有以下工作代码(仍然需要清理它重新格式化......):
<?php
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()
);
}
$waitingRooms[$waitingRoomPK]['cases'][] = array(
'case_pk' => $row_waitingrooms['case_pk'],
'patient_icon' => $row_waitingrooms['patient_icon'],
'first_name' => $row_waitingrooms['first_name'],
'last_name' => $row_waitingrooms['last_name'],
'age' => $row_waitingrooms['age']
);
echo "<h6>" . $row_waitingrooms['waitingroom'] . "</h6><div><p><span class='text'>";
foreach ($waitingRooms[$waitingRoomPK]['cases'] as $wcase){
echo "<table width='100%'><tr><td><input name='case' id='case_" . $wcase['case_pk'] . "' type='radio' value='" . $wcase['case_pk'] . "' /></td><td width='65' align='left'><div class='case_list'><img src='images/case_icons/" . $wcase['patient_icon'] . "' width='44' height='45' /></div></td><td width='350' align='left'>" . $wcase['first_name'] . ' ' . $wcase['last_name'] . '  Gender: ' . $wcase['gender'] . ' Age: ' . $wcase['age'] . '<br />Presenting Complaint: ' . $wcase['presenting_complaint'] . "</td></tr></table>";
}
echo "</span></p></div>";
}
?>