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

时间:2014-11-11 15:50:47

标签: php html css

我想在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;
}

我应该怎么做?!

4 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:2)

尝试使用类似的东西(显示每行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>

答案 2 :(得分:1)

你应该这样做:

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

希望这有帮助!如果结果来自查询,我会建议使用查询DB进行分页。通过这种方式,您将消耗更少的资源。

答案 3 :(得分:0)

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