问题
I'm using FPDF to create a PDF file which the data is getting pulled from a MySQL database. And the table is getting created with two main column headers "User Name" and "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();
With the current code I can create the table but this code only prints the header of the table columns on the first page only actually at the beginning of the table and the rest will be filled with data which continues till the data ends. I wish to repeat these 2 header on each page at the beginning of the table on each page.
I tried to do something like this script but ended up creating an infinity loop.
I think I do have to do a manual data amount for the page, then turn off the auto page break, then check for the max data and then print the header if the table has reached it's max data amount. But I don't know how to plug that in to this code.
Can some one please show me how to do this, I'm posting this after many attempts which failed or ended up me breaking the entire script.
**UPDATE:**Thanks to kastriotcunaku I managed to solve my problem now my new code is as follows,
<?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();
I removed the last line which was to be drawn after the while loop stops which actually was just leaving the end of the table with out a last line so I removed that and enable the borders of the cells and set the line width to 0.1 which made my table look much better.
回答1:
You need to specify table header inside function Header(), before $pdf->AddPage();
Try something like this:
<?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();
}
}
?>
来源:https://stackoverflow.com/questions/45561222/how-to-repeat-the-table-column-header-in-each-page