如何在每个页面中重复表列标题 [英] How to repeat the table column header in each page

查看:108
本文介绍了如何在每个页面中重复表列标题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用FPDF创建一个PDF文件,该数据将从MySQL数据库中提取.并使用两个主要的列标题用户名"和花费的总时间"来创建表.

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

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

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.

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

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

我删除了while循环停止后要绘制的最后一行,实际上它只是离开了表格的末尾而没有最后一行,因此我删除了该行并启用了单元格的边框,并将行宽设置为0.1,使我的桌子看起来好很多.

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.

推荐答案

您需要在$ pdf-> AddPage();

You need to specify table header inside function Header(), before $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();
    }
}

?>

这篇关于如何在每个页面中重复表列标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆