Loop table row dynamically (Class Shedule Maker)

I am trying to make a student class routine from mysql data using php. Where each table row (day name wise like Monday, Tuesday) I have to put day’s name manually,

I want to loop table row dynamic

<tr>  
    <td>Monday</td> 
<?php       
for ($x = 1; $x <= 8; $x++) {
    $ret=mysqli_query($con,"select * 
                            from routine 
                            WHERE period_name = $x 
                            and day = 'Monday' 
                            and class='ukg';");
    $cnt=1;
    $row=mysqli_num_rows($ret);
    if($row>0){
        while ($row=mysqli_fetch_array($ret)) {
?>
    <td><?php  echo $row['subject'];?> <br><?php  echo $row['teacher'];?><br><?php  echo $row['time_start'];?> - <?php  echo $row['time_end'];?></td> 
<?php 
            $cnt=$cnt+1;
        } 
    } else {
?>
<?php 
    }
} 
?> 
</tr>
<tr>                                               
    <td>Tuesday</td> 
<?php       
    
for ($x = 1; $x <= 8; $x++) {
    $ret=mysqli_query($con,"select *    
                            from routine 
                            WHERE period_name = $x 
                            and day = 'Tuesday' 
                            and class='ukg';");
    $cnt=1;
    $row=mysqli_num_rows($ret);
    if($row>0){
        while ($row=mysqli_fetch_array($ret)) {
?>
    <td><?php  echo $row['subject'];?> <br><?php  echo $row['teacher'];?><br><?php  echo $row['time_start'];?> - <?php  echo $row['time_end'];?></td> 
<?php 
        $cnt=$cnt+1;
        } 
    } else {
?>
<?php 
    }
} 
?> 
</tr>