MySQL / FPDF:如何限制PDF中每页的数据

我目前创建了一个可以生成从 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); 

    }



开满天机
浏览 86回答 1
1回答

收到一只叮咚

您需要在while循环中使用计数器,因此它的工作方式如下:$count = 0;while ($row = mysqli_fetch_array($user3)){&nbsp; &nbsp; /* put your row rendering code here */&nbsp; &nbsp; /* ... */&nbsp; &nbsp; if ((($count - 8) % 10) === 0) {&nbsp; &nbsp; &nbsp; &nbsp; $pdf->AddPage();&nbsp; &nbsp; &nbsp; &nbsp; /* put any page headers or footers here */&nbsp; &nbsp; }&nbsp; &nbsp; $count++;}这样,您将在 8、18、28 行等之后有一个新页面。我认为您有一些数据(如标题)需要在每一页(全名、徽章 ID)上重复 - 对吗?请记住,每次添加新页面时都需要再次呈现该标题。理想情况下,您会将页眉代码提取到单独的函数中。
打开App,查看更多内容
随时随地看视频慕课网APP