Question

I have the following MySQL query:

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

The result is printed:

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>";
}

As expected, this results in an accordion for each of the records in table 'waitingroom_case_lookup' containing one 'case' per accordion. What I'm after is to have an accordion for each 'waitingroom' and each containing the relevant case data from the tables vcase and patient.

At the moment the table 'waitingroom_case_lookup' is:

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

It would appear that I need to loop out the cases in each of the accordion waitingrooms between <span class='text'> and </p></div> Any suggestions how I can do this?

EDIT

Thanks to NoBBY I have the following working code (still need to clean it up re formatting...):

<?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>";
}
?>
Was it helpful?

Solution

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:

  1. 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)
  2. Use actual joins, that alternative join syntax in the query hurts readability
  3. 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']; ?>&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; ?>

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'] . '&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>";
}

And you get the bonus to see the unclosed span tag :)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top