如何在每个页面中重复表列标题

时间:2017-08-08 06:52:10

标签: php mysql fpdf

我使用FPDF创建一个PDF文件,从MySQL数据库中提取数据。并且该表创建时有两个主要的列标题" User Name"和"花费的总时间"。

<?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();

使用当前代码,我可以创建表,但是此代码仅在第一页上打印表列的标题,实际上只在表的开头,其余的将填充数据,这些数据一直持续到数据结束。我希望在每页的表格开头的每一页上重复这两个标题。

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

我认为我必须为页面执行手动数据量,然后关闭自动分页符,然后检查最大数据,然后在表格达到最大数据量时打印标题。但我不知道如何将其插入此代码中。

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

**更新:**感谢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循环停止后要绘制的最后一行,实际上只是离开了表的末尾而没有最后一行,所以我删除了它并启用单元格的边框并将行宽设置为0.1让我的桌子看起来好多了。

1 个答案:

答案 0 :(得分:3)

你需要在$ pdf-&gt; AddPage();

之前在函数Header()中指定表头

尝试这样的事情:

&#13;
&#13;
<?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();
    }
}

?>
&#13;
&#13;
&#13;