我的if语句不能正常工作

时间:2014-04-28 23:40:28

标签: php tcpdf fpdf

if语句不能正常工作它会正确打印一页然后再打印17页而没有列标题而只打印一个数据行。我无法弄清楚这是不正常的。有一个更好的方法吗。我正在进行FPDF站点的中断处理。

链接到PDF文件以显示此代码生成的内容http://notaryaccounting.com/testpdf.pdf

   $pdf = new PDF();
$pdf->AddPage ($orientation='', $format='', $keepmargins=false, $tocpage=false);
$pdf->SetPrintHeader(true);
$pdf->SetPrintFooter(true);   
$pdf->setFontSubsetting(true);
// set margins
$pdf->SetMargins(10, PDF_MARGIN_TOP, 10);

$pdf->SetFont('Arial', 'BI', 20, '', 'false');
$year = date("Y"); ;
$html = 'Mileage for signing1 dates from 1/1/' .$year. ' to 12/31/' .$year. '';
$pdf->SetXY(2, 15);
$pdf->SetFont('times', '', 8, '', true);
$pdf->writeHTML ($html, $ln=true, $fill=false, $reseth=false, $cell=false, $align='R');


//set initial y axis position per page
$y_axis_initial = 20;

//print column titles
$pdf->SetFillColor(232,232,232);
$pdf->SetFont('Arial','B',12);
$pdf->SetY($y_axis_initial);
$pdf->SetX(10);
$pdf->Cell(20,6,'Invoice #:',1,0,'L',1);
$pdf->Cell(27,6,'Signing Date:',1,0,'L',1);
$pdf->Cell(30,6,'Name:',1,0,'L',1);
$pdf->Cell(60,6,'Location:',1,0,'L',1);
$pdf->Cell(15,6,'Start:',1,0,'L',1);
$pdf->Cell(15,6,'End:',1,0,'L',1);
$pdf->Cell(20,6,'Mileage:',1,0,'L',1);

//Go to next row
 $y_axis = 26;
 $y_axis = $y_axis + $row_height;

//initialize counter
$i = 0;

//Set maximum rows per page
$max = 25;

//Set Row Height
$row_height = 6;





$result = $db->query("SELECT invoicenumber,signstart,fname,lname,street1,city,state,mstart,mend FROM signings1 WHERE pid = $pid ORDER by signstart"); 
foreach($result as $key => $sig) {
   $totalcont = $totalcont + 1;
   echo $i;
  //If the current row is the last one, create new page and print column title
        if ($i == $max){

        $pdf->AddPage();

        //print column titles for the current page
        $pdf->SetY($y_axis_initial);
        $pdf->SetFillColor(232,232,232);
        $pdf->SetFont('Arial','B',12);
        $pdf->SetX(05);
        $pdf->Cell(20,6,'Invoice #:',1,0,'L',1);
        $pdf->Cell(27,6,'Signing Date:',1,0,'L',1);
        $pdf->Cell(30,6,'Name:',1,0,'L',1);
        $pdf->Cell(60,6,'Location:',1,0,'L',1);
        $pdf->Cell(15,6,'Start:',1,0,'L',1);
        $pdf->Cell(15,6,'End:',1,0,'L',1);
        $pdf->Cell(20,6,'Mileage:',1,0,'L',1);

         //Go to next row
       $y_axis = $y_axis + $row_height;

       //Set $i variable to 0 (first row)
       $i = 0;


    }



        $invoicenumber = $sig[invoicenumber];   
        $signdate=phpdate($sig[signstart]);
        $name = $sig[fname] . ' ' . $sig[lname];
        $location = $sig[street1] . " " . $sig[city] . " " . $sig[state];
        $mstart  = $sig[mstart];
        $mend  = $sig[mend];
        $miles  = $sig[miles];
        $miles1 = $mend - $mstart; 
        $subTotal += $miles1;
        $totalmoney = $subTotal * .56;

        $pdf->SetFont('Arial','B',9);
        $pdf->SetFillColor(255,255,255);
        $pdf->SetY($y_axis);
        $pdf->SetX(10);
        $pdf->Cell(20,6,$invoicenumber,1,0,'L',1);
        $pdf->Cell(27,6,$signdate,1,0,'L',1);
        $pdf->Cell(30,6,$name,1,0,'L',1);
        $pdf->Cell(60,6,$location,1,0,'L',1);
        $pdf->Cell(15,6,$mstart,1,0,'L',1);
        $pdf->Cell(15,6,$mend,1,0,'L',1);
        $pdf->Cell(20,6,$miles1,1,0,'L',1);

    //Go to next row
    $y_axis = $y_axis + $row_height;
    $i = $i + 1;
}

ob_end_clean();
$pdf->Output();

3 个答案:

答案 0 :(得分:1)

这没什么大不了的。一些未定义的变量。我已经更改了一些变量的名称,但我没有发现任何错误,我可以发现原始变量。

我没有Arial'字体安装,因为我刚刚下载并安装了原始&tcpdpd'软件

现在正确分页。

<?php
require __DIR__ .'/tcpdf/tcpdf.php';
require __DIR__ .'/tcpdf/tcpdf_autoconfig.php';
$pid = 1;

$dsn = 'mysql:host=localhost;dbname=testmysql';
$username = 'test';
$password = 'test';
$options = array(
    PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8',
);
$db = new PDO($dsn, $username, $password, $options);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);


// $db = new mysqli('localhost', 'test', 'test', 'testmysql');

$pdf = new TCPDF();
$pdf->AddPage ($orientation='', $format='', $keepmargins=false, $tocpage=false);
$pdf->SetPrintHeader(true);
$pdf->SetPrintFooter(true);
$pdf->setFontSubsetting(true);
// set margins
$pdf->SetMargins(10, PDF_MARGIN_TOP, 10);

$pdf->SetFont('arial', 'BI', 20, '', 'false');
$year = date("Y"); ;
$html = 'Mileage for signing1 dates from 1/1/' .$year. ' to 12/31/' .$year. '';
$pdf->SetXY(2, 15);
$pdf->SetFont('times', '', 8, '', true);
$pdf->writeHTML ($html, $ln=true, $fill=false, $reseth=false, $cell=false, $align='R');


//set initial y axis position per page
$y_axis_initial = 20;

//print column titles
$pdf->SetFillColor(232,232,232);
$pdf->SetFont('arial','B',12);
$pdf->SetY($y_axis_initial);
$pdf->SetX(10);
$pdf->Cell(20,6,'Invoice #:',1,0,'L',1);
$pdf->Cell(27,6,'Signing Date:',1,0,'L',1);
$pdf->Cell(30,6,'Name:',1,0,'L',1);
$pdf->Cell(60,6,'Location:',1,0,'L',1);
$pdf->Cell(15,6,'Start:',1,0,'L',1);
$pdf->Cell(15,6,'End:',1,0,'L',1);
$pdf->Cell(20,6,'Mileage:',1,0,'L',1);

//Set Row Height
$row_height = 6;

$totalcount = 0;
$subTotal = 0;
$miles = 0;

//Go to next row
 $y_axis = 26;
 $y_axis = $y_axis + $row_height;

//initialize counter
$pageLine = 0;

//Set maximum rows per page
$itemsPerPage = 3 /*25 */;

//
$result = $db->query("SELECT invoicenumber,signstart,fname,lname,street1,city,state,mstart,mend FROM signings1 WHERE pid = $pid ORDER by signstart");
foreach($result as $key => $sig) {
//    var_dump($key, $sig);
   $totalcount = $totalcount + 1;
   echo $pageLine;
  //If the current row is the last one, create new page and print column title
        if ($pageLine >= $itemsPerPage) {

        $pdf->AddPage();

        //print column titles for the current page
        $pdf->SetY($y_axis_initial);
        $pdf->SetFillColor(232,232,232);
        $pdf->SetFont('arial','B',12);
        $pdf->SetX(05);
        $pdf->Cell(20,6,'Invoice #:',1,0,'L',1);
        $pdf->Cell(27,6,'Signing Date:',1,0,'L',1);
        $pdf->Cell(30,6,'Name:',1,0,'L',1);
        $pdf->Cell(60,6,'Location:',1,0,'L',1);
        $pdf->Cell(15,6,'Start:',1,0,'L',1);
        $pdf->Cell(15,6,'End:',1,0,'L',1);
        $pdf->Cell(20,6,'Mileage:',1,0,'L',1);

        $subTotal = 0;
        //Go to next row
       $y_axis = $y_axis + $row_height;

       //Set $pageLine variable to 0 (first row)
       $pageLine = 0;
   }

        $invoicenumber = $sig['invoicenumber'];
        $signdate=date($sig['signstart']);
        $name = $sig['fname'] . ' ' . $sig['lname'];
        $location = $sig['street1'] . " " . $sig['city'] . " " . $sig['state'];
        $mstart  = $sig['mstart'];
        $mend  = $sig['mend'];
        $miles = $mend - $mstart;
        // $miles  =  $sig['miles'];
        $subTotal += $miles;
        $totalmoney = $subTotal * .56;

        $pdf->SetFont('arial','B',9);
        $pdf->SetFillColor(255,255,255);
        $pdf->SetY($y_axis);
        $pdf->SetX(10);
        $pdf->Cell(20,6,$invoicenumber,1,0,'L',1);
        $pdf->Cell(27,6,$signdate,1,0,'L',1);
        $pdf->Cell(30,6,$name,1,0,'L',1);
        $pdf->Cell(60,6,$location,1,0,'L',1);
        $pdf->Cell(15,6,$mstart,1,0,'L',1);
        $pdf->Cell(15,6,$mend,1,0,'L',1);
        $pdf->Cell(20,6,$miles,1,0,'L',1);

    //Go to next row
    $y_axis = $y_axis + $row_height;
    $pageLine = $pageLine + 1;
}

ob_end_clean();
$pdf->Output();

答案 1 :(得分:0)

if ($i == $max){更改为if ($i != 0 && $i % $max == 0) { 这将执行新页面&#39;&#39;每25条记录$i != 0就在那里,以确保新页面不会被添加到第一条记录中。

答案 2 :(得分:0)

我想出了在第一个页面之后所有其他页面上将所有数据放在页面上的页面。 在Ryan Vincent回答的帮助下,他帮助我使用上面的示例打印了17页的脚本。 我还有一个问题就是将所有数据放在页面上。就在

之后
if ($pageLine >= $itemsPerPage) {

        $pdf->AddPage();\

我添加了

$y_axis = 20; //reset Y_axis to move the data back to the top of the NEW page
                      //under the columns

这就是一切,现在脚本完美无缺

$pdf = new PDF();
$pdf->AddPage ($orientation='', $format='', $keepmargins=false, $tocpage=false);
$pdf->SetPrintHeader(true);
$pdf->SetPrintFooter(true);
$pdf->setFontSubsetting(true);
$pdf->SetAutoPageBreak(FALSE, PDF_MARGIN_BOTTOM);
// set margins
$pdf->SetMargins(10, PDF_MARGIN_TOP, 10);



//set initial y axis position per page
$y_axis = 20;

//print column titles
$pdf->addmileagetext();
$pdf->SetFillColor(232,232,232);
$pdf->SetFont('arial','B',12);
$pdf->SetY($y_axis);
$pdf->SetX(10);
$pdf->Cell(20,6,'Invoice #:',1,0,'L',1);
$pdf->Cell(20,6,'Date:',1,0,'L',1);
$pdf->Cell(40,6,'Name:',1,0,'L',1);
$pdf->Cell(60,6,'Location:',1,0,'L',1);
$pdf->Cell(15,6,'Start:',1,0,'L',1);
$pdf->Cell(15,6,'End:',1,0,'L',1);
$pdf->Cell(20,6,'Mileage:',1,0,'L',1);

//Set Row Height
$row_height = 6;


//Go to next row

 $y_axis = $y_axis + $row_height;

//initialize counter
$pageLine = 0;

//Set maximum rows per page
$itemsPerPage = 25;

//
$result = $db->query("SELECT invoicenumber,signstart,fname,lname,street1,city,state,mstart,mend FROM signings1 WHERE pid = $pid ORDER by signstart");
foreach($result as $key => $sig) {
   //var_dump($key, $sig);




  //If the current row is the last one, create new page and print column title
        if ($pageLine >= $itemsPerPage) {

        $pdf->AddPage();
        $pdf->addmileagetext();

        $y_axis = 20; //reset Y_axis to move the data back to the top of the NEW page
                      //under the columns
        //print column titles for the current page
        $pdf->SetY($y_axis);
        $pdf->SetFillColor(232,232,232);
        $pdf->SetFont('arial','B',12);
        $pdf->SetX(10);
        $pdf->Cell(20,6,'Invoice #:',1,0,'L',1);
        $pdf->Cell(20,6,'Date:',1,0,'L',1);
        $pdf->Cell(40,6,'Name:',1,0,'L',1);
        $pdf->Cell(60,6,'Location:',1,0,'L',1);
        $pdf->Cell(15,6,'Start:',1,0,'L',1);
        $pdf->Cell(15,6,'End:',1,0,'L',1);
        $pdf->Cell(20,6,'Mileage:',1,0,'L',1);

        $subTotal = 0;
        //Go to next row
       $y_axis = $y_axis + $row_height;

       //Set $pageLine variable to 0 (first row)
       $pageLine = 0;

        }

        $invoicenumber = $sig['invoicenumber'];
        $signdate=phpdate($sig['signstart']);
        $name = $sig['fname'] . ' ' . $sig['lname'];
        $location = $sig['street1'] . " " . $sig['city'] . " " . $sig['state'];
        $mstart  = $sig['mstart'];
        $mend  = $sig['mend'];
        $miles = $mend - $mstart;
        $totalcount = $totalcount + 1;
        $subTotal += $miles;
        $totalmoney = $subTotal * .56;

        $pdf->SetFont('arial','B',9);
        $pdf->SetFillColor(255,255,255);
        $pdf->SetY($y_axis);
        $pdf->SetX(10);
        $pdf->Cell(20,6,$invoicenumber,1,0,'L',0);
        $pdf->Cell(20,6,$signdate,1,0,'L',0);
        $pdf->Cell(40,6,$name,1,0,'L',0);
        $pdf->Cell(60,6,$location,1,0,'L',0);
        $pdf->Cell(15,6,$mstart,1,0,'L',0);
        $pdf->Cell(15,6,$mend,1,0,'L',0);
        $pdf->Cell(20,6,$miles,1,0,'L',0);


    //Go to next row
    $y_axis = $y_axis + $row_height;
    $pageLine = $pageLine + 1;




}

ob_end_clean();
$pdf->Output();