【问题标题】:MySQL / FPDF: How to limit the data by each page in PDFMySQL / FPDF:如何限制PDF中每页的数据
【发布时间】:2019-12-04 07:53:57
【问题描述】:

我目前正在创建一个可以生成从 MySQL 到 PDF 的报告的系统。我使用 FPDF 库。 现在我想在每页上通过限制显示数据。例如,如果我有 100 行数据,我想这样显示:

1) 第一个 PDF 页面将只显示 8 个数据 2) 其他页面显示10条数据

谁能帮帮我?

  <?php

  require('mem_image.php');
  require_once "../../config/config.php";
  require_once "../../config/check.php";

  $email = $_SESSION['login_user'];
  $team = $_SESSION['team'];

  $pdf = new PDF_MemImage();
  $pdf->AddPage();

  $pdf->Cell(10,7,'',0,1);

  $pdf->SetFont('Arial','B',12);
  $pdf->Cell(0,10,'Project Team Overtime Report',1,1,"C");

  $pdf->SetFont('Arial','',10);
  $pdf->Cell(20,10,'Team:',1,0);

  $pdf->SetFont('Arial','B',11);
  $user2 = mysqli_query($conn, "SELECT * FROM users WHERE email = '$email'");
  while ($row = mysqli_fetch_array($user2)){
    $pdf->Cell(170,10,$row['fullname'],1,1); 
  }

  $pdf->SetFont('Arial','',10);
  $pdf->Cell(0,10,'Workers Name:',1,1,"C");
  $pdf->SetFont('Arial','B',10);
  $pdf->Cell(20,7,'Badge ID',1,0);
  $pdf->Cell(170,7,'Fullname',1,1);

  $pdf->SetFont('Arial','',10);
  $user = mysqli_query($conn, "SELECT * FROM users INNER JOIN team on users.team_id = team.team_id WHERE users.team_id = '$team' AND (users.roles_id = 3 OR users.roles_id = 4)");
    while ($row = mysqli_fetch_array($user)){
        $pdf->Cell(20,7,$row['badgeid'],1,0);
        $pdf->Cell(170,7,$row['fullname'],1,1); 
    }

  $pdf->SetFont('Arial','B',10);

  $user3 = mysqli_query($conn, "SELECT * FROM report LEFT JOIN users ON report.badgeid = users.badgeid LEFT JOIN team ON team.team_id = users.team_id 
  WHERE team.team_id = '$team' AND report_date BETWEEN '".$_GET["from"]."' AND '".$_GET["to"]."' ORDER BY report.report_date DESC");

      while ($row = mysqli_fetch_array($user3)){
          $pdf->Cell(20,7,'Date:',1,0);
          $pdf->Cell(75,7,date('d-m-Y',strtotime($row['report_date'])),1,0);
          $pdf->Cell(20,7,'Time:',1,0);
          $pdf->Cell(38,7,"From: ".date('H:i',strtotime($row['ot_start'])),1,0);
          $pdf->Cell(37,7,"To: ".date('H:i',strtotime($row['ot_end'])),1,1); 
          $pdf->Cell(95,7,'Before',1,0,'C');
          $pdf->Cell(95,7,'After',1,1, 'C');


          $logo = file_get_contents('../../images/faces/noimage.png');
          // Output it

          if(!isset($row['photo_before']) || empty($row['photo_before'])) {
            $pdf->Cell(95, 50, $pdf->MemImage($logo, $pdf->GetX()+25, $pdf->GetY()+2, 45,45,), 1, 0, 'C');
          }else{ 
            $pdf->Cell(95, 50, $pdf->MemImage(base64_decode($row['photo_before']), $pdf->GetX()+2, $pdf->GetY()+2, 90,45,), 1, 0, 'C');
          }

          if(!isset($row['photo_after']) || empty($row['photo_after'])) {
            $pdf->Cell(95, 50, $pdf->MemImage($logo, $pdf->GetX()+25, $pdf->GetY()+2, 45,45,), 1, 1, 'C');
          }else{ 
            $pdf->Cell(95, 50, $pdf->MemImage(base64_decode($row['photo_after']), $pdf->GetX()+2, $pdf->GetY()+2, 90,45,), 1, 1, 'C');
          }


          if ($row['time_photo_before'] == '0000-00-00 00:00:00'){
            $pdf->Cell(95,7,'-',1,1, 'C');
          }else{
            $pdf->Cell(95,7,$row['time_photo_before'],1,0, 'C');
          }

          if ($row['time_photo_after'] == '0000-00-00 00:00:00'){
            $pdf->Cell(95,7,'-',1,1, 'C');
          }else{
            $pdf->Cell(95,7,$row['time_photo_after'],1,1, 'C');
          }

          $pdf->SetFillColor(216,214,214);
          $pdf->Cell(0,5,'',1,1,"C", true);
      }

  $pdf->Output();
  ?>

【问题讨论】:

  • 不要使用SELECT *。取而代之的是,命名并限定您真正想要的 couns。并了解 sql 注入以及准备好的和绑定的查询的重要性。
  • 并注意LEFT JOIN team ... WHERE team.team_id = '$team'INNER JOIN team...是一样的
  • 好吧,如果你知道我的问题的答案,那就太疯狂了。请注意我

标签: php mysql fpdf


【解决方案1】:

您需要在while 循环中使用计数器,所以它的工作原理如下:

$count = 0;
while ($row = mysqli_fetch_array($user3)){
    /* put your row rendering code here */
    /* ... */

    if ((($count - 8) % 10) === 0) {
        $pdf->AddPage();
        /* put any page headers or footers here */
    }
    $count++;
}

这样你将在 8、18、28 行等之后有一个新页面。

我认为您有一些数据(如标题)需要在每个页面上重复(全名、徽章 ID) - 对吗?请记住,每次添加新页面时都需要再次呈现该标题。理想情况下,您会将页眉代码提取到单独的函数中。

【讨论】:

  • 嗨,谢谢你的回复。没有任何标题。你能解释一下你上面评论的渲染代码吗
  • 通过渲染代码,我的意思是你已经放入 while ($row = mysqli_fetch_array($user3)){...} 的所有内容。您应该在循环末尾添加计数代码。
  • 好的。明白了。但是如果第一页,我只想显示 2 行数据,而另一页将显示 3,你能帮我给出方程式吗?
  • @RadamelFalcao 即使我也能解决这个问题。将 8 替换为 2。
猜你喜欢
  • 2017-01-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-03-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-04-08
相关资源
最近更新 更多