如何在更改php中的列值后重复表头/结构

时间:2016-11-28 16:59:28

标签: php html html-table

我有一个名为students_marks的MYSQL表,其中包含带有标记的学生姓名。样本结构如下。

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

我试图在我的php网页中更改每个学生ID之后重复表头或表结构本身,如下所示。

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

使用下面的代码,我能够得到一个简单的常用html表,其中包含上述示例结构中给出的数据。是否可以按照我的要求填充表格?我的PHP代码如下所示。

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

1 个答案:

答案 0 :(得分:0)

如果我理解,我认为你需要类似的东西

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