Question

I'm trying to do a multi table query and contain the results to records in a specific table. I'm sure that's not very clear, so here are the tables with relevant fields:

waitingroom
    +----------------+-------------+
    | waitingroom_pk | waitingroom |
    +----------------+-------------+
    |         1      |   PATH2201  |
    +----------------+-------------+
    |         2      |   PATH2202  |
    +----------------+-------------+

waitingroom_case_lookup
    +----------------------------+----------------+---------+
    | waitingroom_case_lookup_pk | waitingroom_fk | case_fk |  
    +----------------------------+----------------+---------+
    |         1                  |   1            |  1      |  
    +----------------------------+----------------+---------+
    |         2                  |   1            |  2      |  
    +----------------------------+----------------+---------+
    |         3                  |   2            |  3      |  
    +----------------------------+----------------+---------+
vcase
    +---------+------------+------------+
    | case_pk | case_title | patient_fk |  
    +---------+------------+------------+
    |    1    |  Case One  |     1      |  
    +---------+------------+------------+
    |    2    |  Case Two  |     2      |  
    +---------+------------+------------+
    |    3    | Case Three |     3      |  
    +---------+------------+------------+

  patient
    +------------+------------+-----------+---------+
    | patient_pk | first_name | last_name | case_fk |
    +------------+------------+-----------+---------+
    | 1          | John       | Smith     |   1     |
    +------------+------------+-----------+---------+
    | 2          | Will       | Jones     |   2     |
    +------------+------------+-----------+---------+
    | 3          | Mary       | Ryan      |   3     |
    +------------+------------+-----------+---------+

The query that I'm using is:

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

This query returns results showing three waitingrooms instead of two, presumably because there are three records in table waitingroom_case_lookup. What I want is to group the vcase data according to the waitingrooms using waitingroom_case_lookup.

As for printing the results, the waitingrooms show as JQuery accordions and I have a nested foreach loop which shows the case data in each waitingroom accordion. The problem is, as I said before, that three waiting rooms show, not two. The second and third waitingroom accordions appear OK, with case_pk one and two showing in the second accordion and case_pk three showing in the third. The first accordion shows case_pk one as well, and this accordion shouldn't show.

The results are printed using the following code:

    <?php
$waitingRooms = array();

while ($row_waitingrooms = mysql_fetch_assoc($result_waitingrooms)){

        if($row_waitingrooms['gender'] == 'f'){
                $gender = 'Female';
            } else if ($row_waitingrooms['gender'] == 'm'){
                $gender = 'Male';
            }

        $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'],
            'gender' => $gender,
            'presenting_complaint' => $row_waitingrooms['presenting_complaint']
        );



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

I suspect that the query needs to be changed to group the case data accordion to the waitingrooms tables, any ideas how I should do this?

EDIT

Thanks to developerCK I now have the following working code:

<?php
while ($row_waitingrooms = mysql_fetch_assoc($result_waitingrooms)){

    if($row_waitingrooms['gender'] == 'f'){
            $gender = 'Female';
        } else if ($row_waitingrooms['gender'] == 'm'){
            $gender = 'Male';
        }

    $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'],
        'gender' => $gender,
        'presenting_complaint' => $row_waitingrooms['presenting_complaint']
    );

}

foreach($waitingRooms as $val){

echo "<h6>" . $val['waitingroom'] . "</h6><div><p><span class='text'>";

    foreach ($val['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

use it after while loop, and remove your foreach loop. your query is right. just need to change some php code

foreach(waitingRooms as $val){

echo "<h6>" . $val['waitingroom'] . "</h6><div><p><span class='text'>";

    foreach ($val['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>";
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top