gpt4 book ai didi

php - 如何在每页中重复表格列标题

转载 作者:可可西里 更新时间:2023-11-01 07:34:09 24 4
gpt4 key购买 nike

我正在使用 FPDF 创建一个 PDF 文件,其中的数据是从 MySQL 数据库中提取的。创建的表格有两个主要的列标题“用户名”和“总花费时间”。

<?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 这让我的 table 看起来好多了。

最佳答案

在$pdf->AddPage();之前需要在函数Header()中指定表头;

尝试这样的事情:

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

?>

关于php - 如何在每页中重复表格列标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45561222/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com