PHP/MySQL – Nested loop to populate HTML table cell-ThrowExceptions

Exception or error:

I have a table created using a while loop. I want to use another loop to populate a cell in the second column in the table with other results from the same query. The table needs to look like:

enter image description here

The code so far:

$query = "SELECT * FROM assessment, assessment_type, assessment_assessment_type_lookup, unit, unit_outcome, unit_unit_outcome_lookup, unit_outcome_assessment_lookup
WHERE
assessment_type.assessment_type_pk = assessment_assessment_type_lookup.assessment_type_fk
AND
assessment.assessment_pk = assessment_assessment_type_lookup.assessment_fk
AND
unit_outcome.unit_outcome_pk = unit_unit_outcome_lookup.unit_outcome_fk
AND
unit.unit_pk = unit_unit_outcome_lookup.unit_fk
AND
unit_outcome.unit_outcome_pk = unit_outcome_assessment_lookup.unit_outcome_fk
AND
assessment.assessment_pk = unit_outcome_assessment_lookup.assessment_fk
AND unit.unit_pk = '$unit_pk' GROUP BY assessment.assessment_pk";
$result = $connection->query( $query );
echo "<table width='100%'><thead><tr><th>Assessements</th><th>Outcomes</th></tr></thead><tbody>";
while ($row = mysqli_fetch_array($result)) {
echo "<tr><td>" . $row['assessment'] . "</td><td>"; 
    foreach($row['unit_outcome'] as $o){
        echo $o. "</td></tr>";
}
}
echo "<tbody></table>";

However $row['unit_outcome'] is not appearing in the table cell.

enter image description here

var_dump($row['unit_outcome']);

string(125) “

Display professional behavior in the educational and clinical settings and outline some challenges to professionalism
” string(142) “

Outline the principles of reflective practice, recognize personal abilities and difficulties and access support services when necessary
” string(117) “

Comply with medicolegal responsibilities and outline some bioethical issues in the doctor-patient relationship
” string(442) “

Explain generic principles of clinically relevant normal and abnormal human structure, function, behavior, development, responses and compensatory mechanisms to illness and injury, and outline the classification, epidemiology, etiology, anatomy, pathophysiology, common clinical and pathological manifestations, natural history, diagnostic principles and therapeutic principles for some specified organ systems and medical conditions
” string(269) “

Explain generic principles of patient management including pharmacological and non-pharmacological therapies, and explain the use of therapies for specified organ system medical conditions, and demonstrate adherence to infection control and safe patient handling

The table shows five rows corresponding to the five unit_outcomes when not grouping by assessments in the query.

enter image description here

OK, with:

$result = $connection->query( $query );
echo "<table width='100%' border='1'><thead><tr><th>Assessements</th><th>Outcomes</th></tr></thead><tbody>";
while ($row = mysqli_fetch_array($result)) {
echo "<tr><td>" . $row['assessment'] . "</td>". "<td>"; 
    var_dump($row['unit_outcome']);
    foreach($row['unit_outcome'] as $o){
        echo $o; //<-------------------
    }
   echo "</td></tr>";   
}
echo "<tbody></table>";

I get:

enter image description here

How to solve:

You have error in your table row declaration, change to this:

$result = $connection->query( $query );
echo "<table width='100%'><thead><tr><th>Assessements</th><th>Outcomes</th></tr></thead><tbody>";
while ($row = mysqli_fetch_array($result)) {
echo "<tr><td>" . $row['assessment'] . "</td>"
 . "<td>"; 
    foreach($row['unit_outcome'] as $o){
        echo $o; //<-------------------
    }
   echo "</td></tr>";
}
echo "<tbody></table>";

Also you can do it this way too:

<?php
//Here you have a initial query
$result = $connection->query( $query );
?>
<table width='100%'>
    <thead>
        <tr>
            <th>Assessements</th>
            <th>Outcomes</th>
        </tr>
    </thead>
    <tbody>
        <?php while ($row = mysqli_fetch_array($result)): ?>
        <?php
        /*
          Here you have to create a new query string and get all 
          records WHERE id equal to $row['id'] of your assignment and 
          store it in $assignment_result
        */
        ?>
        <tr>
            <td><?php echo $row['assessment']; ?></td>
            <td>
                <ul>
                <?php while ($assignment = mysqli_fetch_array($assignment_result)):: ?>
                    <li>
                         <?php echo $assignment['YOUR TEXT FIELD']; ?>
                    </li>
                <?php endwhile;?>
                </ul>
            </td>
         </tr>
         <?php endwhile; ?>
    <tbody>
</table>

Leave a Reply

Your email address will not be published. Required fields are marked *