How to display data row wise in HTML table

I have 3 tables in MySQL database

  1. rep (id, userid, date)
  2. time (time_id, id, Time)
  3. stud_details (stid, time_id, sid, Student, Subject, Topic, Confidence)

One report has many time details and each time has many student details. I want to display the data row wise in HTML table as shown below.

As show in the screenshot, each time is displayed in column and for each time their respective student details are shown. Every column has different number of students.

I have below code which displays column wise data in HTML table. But the issue is wherever there is empty student data, rows are not displayed. So I need to display the rows for all the cells as shown in the above screenshot.


<?php
 
 $sql2 = 'SELECT rd.Time,rd.id from time as rd,  rep as rep WHERE rep.id=rd.id and 
          DateofReport=:date and rep.userid=:userid';      // Query to get all the Time of specific report

 $params = [':date'=>$model->DateofReport,':userid' => $model->userid];    // Bind date parameter for the query

 $timedetails = \Yii::$app->db->createCommand($sql2, $params)->queryAll(); // Get all times
 
 ?>

 <?php
 echo "<table class='table table-bordered'>";

 foreach($timedetails as  $tdetails):
 
  ?>

  <td>
   <table class='table table-bordered'  style="border: 1px solid black">
      <tr  bgcolor='#B8B8B8' style="border: 1px solid black">
         <th style='border: 1px solid black;'>
            <?php echo $tdetails['Time']; // Display Time in columns?>  
        </th>
     </tr>

<?php
// Query to fetch student details for each time
$sql3 = 'SELECT s.StudentName, sd.Subject,sd.Topic,sd.Confidence 
         from time as rd, student as s, stud_details as sd, rep as rep
         where rd.time_id=sd.time_id
         and rep.id=rd.id
         and s.StudentId=sd.StudentId 
         and rd.id=:Id
         and rep.userid=:userid';

$params1 = [':userid' => $model->userid];    // Bind user id parameter
$params1[':Id']=$tdetails['time_id'];        // Bind time parameter

$StudentDetails=\Yii::$app->db->createCommand($sql3, $params1)->queryAll();// Get student data
 
foreach($StudentDetails as $StudentDetails){
?>
  <tr>
       <td style='width:100px; word-wrap: break-word; border: 1px solid black;'>
           <b><?php echo $StudentDetails['StudentName'];?></b>
       </td>
  </tr>
  <tr>
       <td style='width:100px;word-wrap: break-word; border: 1px solid black;'>
               <?php echo  $StudentDetails['Subject'];?>
       </td>
 </tr>
 <tr>
       <td style='width:200px;word-wrap: break-word;border: 1px solid black;'>
              <?php echo  $StudentDetails['Topic'];?>
       </td>
 </tr>
 <tr>
       <td style='width:100px;word-wrap: break-word;border: 1px solid black;'> 
             <?php echo  $StudentDetails['Confidence'];?>
       </td>
 </tr>
 <?php
   }
 ?>
</table>
</td>
<?php
endforeach;
echo '</table>';
?>

Hint - You need to find maximum row and loop to fill extra rows. Don’t use query inside loop.