如何在每个页面中重复 table 列 header

How to repeat the table column header in each page

我正在使用 FPDF 创建一个 PDF 文件,其中的数据是从 MySQL 数据库中提取的。 table 是用两个主列 headers "User Name" 和 "Total Time Spent".

创建的
<?php
/**
 * Created by PhpStorm.
 * User: SiNUX
 * Date: 8/3/2017
 * Time: 2:44 PM
 */

require('../Libraries/fpdf/fpdf.php');
include_once ('../iConnect/handShake.php');

class h4PDF extends FPDF{
    function Header(){
        $this->Image('../../images/logo.png', 10,6,30);
        $this->SetFont('Arial', 'B', 12);
        $this->Cell(80);
        $this->Cell(50, 10, 'Total Spent Time', 0, 0,'C');
        $this->Ln(10);
    }
}

$getTot = "SELECT userlogin.uName, SEC_TO_TIME(SUM(TIME_TO_SEC(timeSpent))) AS totTime FROM usertimetrack
           LEFT JOIN userlogin ON usertimetrack.usrId = userlogin.uId WHERE jDate = :jdate GROUP BY usrId";
$getTotQuery = $dbConnect -> prepare($getTot);
$getTotQuery -> bindParam('jdate', $_REQUEST["date"]);
$getTotQuery -> execute();

$pdf = new h4PDF();
$pdf->AddPage();

$pdf->SetFont('Arial','', 14);
$pdf->SetFillColor(255,0,0);
$pdf->SetTextColor(255);
$pdf->SetDrawColor(128,0,0);
$pdf->SetLineWidth(.3);
$pdf->SetFont('','B');

$pdf->SetX(70);
$pdf->Cell(40,6,'User Name',1,0,'C',1);
$pdf->SetX(105);
$pdf->SetX(110);
$pdf->Cell(42,6,'Total Spent Time',1,0,'C',1);
$pdf->Ln();

$pdf->SetFillColor(224,235,255);
$pdf->SetTextColor(0);
$pdf->SetFont('');

$fill = false;
while ($getTotRow = $getTotQuery -> fetch(PDO::FETCH_ASSOC)){
    $uName = $getTotRow["uName"];
    $totTime = $getTotRow["totTime"];

    $pdf->SetX(70);
    $pdf->Cell(40,6,$uName,'LR',0,'C',$fill);
    $pdf->SetX(105);
    $pdf->SetX(110);
    $pdf->Cell(42,6,$totTime ,'LR',0,'C',$fill);
    $pdf->Ln();
    $fill = !$fill;
}
$pdf->SetX(70);
$pdf->Cell(82,0,'','T');

$pdf->Output();

使用当前代码我可以创建 table 但此代码仅打印第一页上 table 列的 header 实际上仅在 [= 的开头35=] 其余部分将填充数据,一直持续到数据结束。我希望在每页 table 的开头在每页重复这 2 header。

我尝试做类似 this 脚本的事情,但最终创建了一个无限循环。

我想我必须为页面做一个手动数据量,然后关闭自动分页符,然后检查最大数据,然后打印 header 如果 table已达到其最大数据量。但我不知道如何将其插入此代码。

有人可以告诉我如何做到这一点,我在多次尝试失败或最终破坏整个脚本后发布了这个。

**更新:**感谢 kastriotcunaku 我设法解决了我的问题,现在我的新代码如下,

<?php
/**
 * Created by PhpStorm.
 * User: SiNUX
 * Date: 8/3/2017
 * Time: 2:44 PM
 */

require('../Libraries/fpdf/fpdf.php');
include_once ('../iConnect/handShake.php');

class h4PDF extends FPDF{
    function Header(){
        $this->Image('../../images/logo.png', 10,6,30);
        $this->SetFont('Arial', 'B', 12);
        $this->Cell(80);
        $this->Cell(50, 10, 'Total Spent Time', 0, 0,'C');
        $this->Ln(10);

        $this->SetFont('Arial','', 14);
        $this->SetFillColor(255,0,0);
        $this->SetTextColor(255);
        $this->SetDrawColor(128,0,0);
        $this->SetX(74);
        $this->Cell(40,6,'User Name',1,0,'C',1);
        $this->SetX(109);
        $this->SetX(114);
        $this->Cell(42,6,'Total Spent Time',1,0,'C',1);
        $this->Ln();
    }
}

$getTot = "SELECT userlogin.uName, SEC_TO_TIME(SUM(TIME_TO_SEC(timeSpent))) AS totTime FROM usertimetrack
           LEFT JOIN userlogin ON usertimetrack.usrId = userlogin.uId WHERE jDate = :jdate GROUP BY usrId";
$getTotQuery = $dbConnect -> prepare($getTot);
$getTotQuery -> bindParam('jdate', $_REQUEST["date"]);
$getTotQuery -> execute();

$pdf = new h4PDF();
$pdf->AddPage();

$pdf->SetFont('Arial','', 14);
$pdf->SetFillColor(255,0,0);
$pdf->SetTextColor(255);
$pdf->SetDrawColor(128,0,0);
$pdf->SetLineWidth(.1);
$pdf->SetFont('','B');

$pdf->SetFillColor(224,235,255);
$pdf->SetTextColor(0);
$pdf->SetFont('');

$fill = false;
while ($getTotRow = $getTotQuery -> fetch(PDO::FETCH_ASSOC)){
    $uName = $getTotRow["jDate"];
    $totTime = $getTotRow["timeSpent"];

    $pdf->SetX(74);
    $pdf->Cell(40,6,$uName,'1',0,'C',$fill);
    $pdf->SetX(109);
    $pdf->SetX(114);
    $pdf->Cell(42,6,$totTime ,'1',0,'C',$fill);
    $pdf->Ln();
    $fill = !$fill;
}

$pdf->Output();

我删除了 while 循环停止后要绘制的最后一行,实际上只是离开 table 的末尾没有最后一行所以我删除了它并启用了单元格的边框并将线宽设置为 0.1,这让我的 table 看起来好多了。

您需要在函数 Header() 中指定 table header,在 $pdf->AddPage();

之前

尝试这样的事情:

<?php

class h4PDF extends FPDF{
    function Header(){
        $this->Image('../../images/logo.png', 10,6,30);
        $this->SetFont('Arial', 'B', 12);
        $this->Cell(80);
        $this->Cell(50, 10, 'Total Spent Time', 0, 0,'C');
        $this->Ln(10);
        
        $this->SetX(70);
        $this->Cell(40,6,'User Name',1,0,'C',1);
        $this->SetX(105);
        $this->SetX(110);
        $this->Cell(42,6,'Total Spent Time',1,0,'C',1);
        $this->Ln();
    }
}

?>