在php中的n行之后重复html表头


repeat html table header after n row in php

我想在n行之后重复表头。我想把结果打印在一张纸上,所以如果不止一张纸,我也希望这个表头在第二页上。

    <table> 
        <thead> 
        <tr>
            <th style="width:50px">Num</th> 
            <th style="width:100px">Name</th> 
        </tr> 
        </thead> 
        <tbody> 
        <?php if(isset($people) && is_array($people) && count($people)>0) : ?>
        <?php $rows = 0; ?>
        <?php foreach($people as $person) : ?>
        <tr >
            <td class="td-center"><?php echo $rows + 1; ?></td> 
            <td class="td-center"><?php echo $person->Name; ?></td> 
        </tr>
        <?php $rows++; ?> 
        <?php endforeach; ?>
        <?php endif; ?>
        </tbody>
    </table>

我使用了下面的CSS,但遗憾的是没有得到任何结果。

 thead{
    display: table-header-group;
}

我该怎么做?!

在100行之后重复。。。

<table> 
    <thead> 
    <tr>
        <th style="width:50px">Num</th> 
        <th style="width:100px">Name</th> 
    </tr> 
    </thead> 
    <tbody> 
    <?php if(isset($people) && is_array($people) && count($people)>0) : ?>
    <?php $rows = 0; ?>
    <?php foreach($people as $person) : ?>
    <?php if($rows % 100 == 0) { ?>
    <tr>
        <th style="width:50px">Num</th> 
        <th style="width:100px">Name</th> 
    </tr>       
    <?php } ?> 
    <tr >
        <td class="td-center"><?php echo $rows + 1; ?></td> 
        <td class="td-center"><?php echo $person->person_Position; ?></td> 
    </tr>
    <?php $rows++; ?> 
    <?php endforeach; ?>
    <?php endif; ?>
    </tbody>
</table>

试试这样的东西(每5行显示一行你的头线):

<table> 
    <thead> 
    <tr>
        <th style="width:50px">Num</th> 
        <th style="width:100px">Name</th> 
    </tr> 
    </thead> 
    <tbody> 
    <?php if(isset($people) && is_array($people) && count($people)>0) : ?>
    <?php $rows = 0; ?>
    <?php foreach($people as $person) : ?>
    <?php if ($rows%5==0) { ?>
      <tr>
        <th style="width:50px">Num</th> 
        <th style="width:100px">Name</th> 
      </tr> 
    <?php } ?>
    <tr >
        <td class="td-center"><?php echo $rows + 1; ?></td> 
        <td class="td-center"><?php echo $person->Name; ?></td> 
    </tr>
    <?php $rows++; ?> 
    <?php endforeach; ?>
    <?php endif; ?>
    </tbody>
</table>

您应该这样做:

<table> 
    <thead> 
    <tr>
        <th style="width:50px">Num</th> 
        <th style="width:100px">Name</th> 
    </tr> 
    </thead> 
    <tbody> 
    <?php if(isset($people) && is_array($people) && count($people)>0) : ?>
    <?php $rows = 0; ?>
    <?php $n = 10; //this can be a result form DB?>
    <?php foreach($people as $person) : ?>
    <?php if ($rows % $n == 0) { //repeat after nth row?>
      <tr>
        <th style="width:50px">Num</th> 
        <th style="width:100px">Name</th> 
      </tr> 
    <?php } ?>
    <tr >
        <td class="td-center"><?php echo $rows + 1; ?></td> 
        <td class="td-center"><?php echo $person->Name; ?></td> 
    </tr>
    <?php $rows++; ?> 
    <?php endforeach; ?>
    <?php endif; ?>
    </tbody>
</table>

希望这能有所帮助!尽管如果结果来自查询,我建议使用查询数据库进行分页。这样,您将消耗更少的资源。

使用array_chunk()获得更可读的代码。http://php.net/manual/en/function.array-chunk.php

<?php foreach(array_chunk($people, 10) as $chunkOfPeople) {?>
<!-- your headers here -->
  <?php foreach($chunkOfPeople as $person) {?>
    <?php //loop the properties of $person and create rows ?>
  <?php } ?> //end inner loop
<?php } ?> //end outer loop