0

我有以下 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'] . '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbspGender: ' . $row_waitingrooms['gender'] . '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;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'] . '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbspGender: ' . $wcase['gender'] . '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Age: ' . $wcase['age'] . '<br />Presenting Complaint: ' . $wcase['presenting_complaint'] . "</td></tr></table>";
  }
echo "</span></p></div>";
}
?>
4

1 回答 1

1

与其直接从数据库中输出结果,不如先尝试对其进行格式化。例如,您可以执行以下操作:

$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
    );
}

现在 $waitingRooms 是包含每个候诊室数据的数组,以及一个包含房间中每个患者数据的“病例”子数组 从这里 2 个简单的嵌套 foreach 语句将输出手风琴所需的 html

不过,这里有一些改进代码的方法:

  1. 不要使用 mysql_* 函数,它们已被弃用。文档中那些巨大的红色警告不仅仅是为了好看。用户 mysqli 或 PDO(都在文档中链接)
  2. 使用实际连接,查询中的替代连接语法会损害可读性
  3. 格式化你的 sql 和 html 代码。我敢打赌,当他们在代码段中看到这些水平滚动条时,很多人都跳过了你的问题

我希望你会同意:

<?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']; ?>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                        Gender: <?= $row_waitingrooms['gender']; ?>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                        Age: <?= $row_waitingrooms['age']; ?>
                        <br />
                        Presenting Complaint: <?= $row_waitingrooms['presenting_complaint']; ?>
                    </td>
                </tr>
            </table>
    </p>
</div>
<?php endwhile; ?>

比这要好得多:

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'] . '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbspGender: ' . $row_waitingrooms['gender'] . '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Age: ' . $row_waitingrooms['age'] . '<br />Presenting Complaint: ' . $row_waitingrooms['presenting_complaint'] . "</td></tr></table></p></div>";
}

你会得到奖励看到未闭合的跨度标签:)

于 2013-10-23T12:00:39.510 回答