简体   繁体   中英

How to repeat a table header/structure after changing a column value in php

I have a MYSQL table called students_marks containing student names with their marks. Sample structure given below.

Student ID  Student Name    Subject FA1 Mark    FA2 Mark    SA1 Mark
----------  ------------    ------- --------    --------    --------
70001           John        English     9           9           8
70001           John        Language    9           8           8
70001           John        Maths       8           9           9
80001           Anna        Computer    8           8           9
80001           Anna        Scocial     9           7           8
90001           Mariya      Maths       9           9           8
90001           Mariya      English     8           8           9
--------------------------------------------------------------------

I am trying to repeat the table header or the table structure itself after changing each student id in my php web page, like given below.

--------------------------------------------------------------------
Student ID  Student Name    Subject FA1 Mark    FA2 Mark    SA1 Mark
----------  ------------    ------- --------    --------    --------
70001           John        English     9           9           8
70001           John        Language    9           8           8
70001           John        Maths       8           9           9
--------------------------------------------------------------------

--------------------------------------------------------------------
Student ID  Student Name    Subject FA1 Mark    FA2 Mark    SA1 Mark
----------  ------------    ------- --------    --------    --------
80001           Anna        Computer    8           8           9
80001           Anna        Scocial     9           7           8
--------------------------------------------------------------------

--------------------------------------------------------------------
Student ID  Student Name    Subject FA1 Mark    FA2 Mark    SA1 Mark
----------  ------------    ------- --------    --------    --------
90001           Mariya      Maths       9           9           8
90001           Mariya      English     8           8           9
-------------------------------------------------------------------- 

with the below code, I able to get a simple usual html table with data as given in the above sample structure. Is it possible to populate the table as per my requirement? My php code is given below.

<table>
    <thead>
        <tr>
            <th>
                Srl No.
            </th>
            <th>
                Student ID
            </th>
            <th>
                Name
            </th>
            <th>
                Subject
            </th>
            <th>
                FA1 Mark
            </th>
            <th>
                FA2 Mark
            </th>
            <th>
                SA1 Mark
            </th>
        </tr>
    </thead>
    <tbody>
<?php 

$no = 1; 
while($row = mysqli_fetch_row($clslst))  { 
$id     = $row[0]; 
$sub    = $row[1]; 
$name   = $row[2];
$fa1    = $row[3]; 
$fa2    = $row[4]; 
$sa1    = $row[5]; 
?>                                    
        <tr>
            <td>
                <?php echo $no; ?>
            </td>
            <td>
                <?php echo $id; ?>
            </td>
            <td>
                <?php echo $name; ?>
            </td>
             <td>
                <?php echo $sub; ?>
            </td>
            <td>
                <?php echo $fa1; ?>    
            </td>
            <td>
                <?php echo $fa2; ?>
            </td>
            <td>
                <?php echo $fa3; ?>
            </td>
            <?php $no++; ?>
        </tr>
<?php } ?>                                       
    </tbody> 
</table>

If i understand, i think you need something like that

<?php
// Initialize an id
$prev_id = 0;
$is_first = true;
$no = 1;

while($row = mysqli_fetch_row($clslst))  {

      $id     = $row[0];
      $sub    = $row[1]; 
      $name   = $row[2];
      $fa1    = $row[3]; 
      $fa2    = $row[4]; 
      $sa1    = $row[5];

      // Checks if the id is changed then starts a new table
      if($prev_id != $id):
          // Set the new previous id
          $prev_id = $id;

          // Adds a closing table if not the first cycle
          if(!$is_first){
             echo " </tbody></table>";
          } else {
           // Next iteration it will add the closing table
           $is_first = false;
          }
      ?>
      <table>
    <thead>
        <tr>
            <th>
                Srl No.
            </th>
            <th>
                Student ID
            </th>
            <th>
                Name
            </th>
            <th>
                Subject
            </th>
            <th>
                FA1 Mark
            </th>
            <th>
                FA2 Mark
            </th>
            <th>
                SA1 Mark
            </th>
        </tr>
    </thead>
    <tbody>
    <?php
    else:
    // Adds the row
    ?>
     <tr>
            <td>
                <?php echo $no; ?>
            </td>
            <td>
                <?php echo $id; ?>
            </td>
            <td>
                <?php echo $name; ?>
            </td>
             <td>
                <?php echo $sub; ?>
            </td>
            <td>
                <?php echo $fa1; ?>    
            </td>
            <td>
                <?php echo $fa2; ?>
            </td>
            <td>
                <?php echo $fa3; ?>
            </td>
            <?php $no++; ?>
        </tr>
     <?php
     endif;
}
// Close last iteration
echo " </tbody></table>";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM