在库fpdf上限制每页相同数量的数据

时间:2011-10-26 11:09:24

标签: php fpdf

我正在使用PHP和fpdf以pdf格式生成我的视图。我的问题是如何限制每页的数据量? 如果我有100行数据,结果是10页。所以我必须限制每页10行。 我有这段代码:

<?php

define('FPDF_FONTPATH', 'font/');
require_once("../../includes/initialize.php");

class PDF extends FPDF {

 function Header() {
     $this->Image("../icon/banner.jpg", 40, 10, 15);
 }

 function Footer() {
     $this->SetY(-15);
     $this->SetFont("Arial", "I", 10);
     $this->Cell(0, 10, "Page " . $this->PageNo() . "/{nb}", 0, 0, "C");
 } 

}
 $filter = $_GET["filter"];
 $value = $_GET["value"];
 if (!empty($_GET["filter"]) && !empty($_GET["value"])) {
 $query = "Select id_detail_ruang, id_ruang, id_barang, no_seri from detail_ruang
 where ".$filter." = '".$value."'";

} else {
 $query = "Select id_detail_ruang, id_ruang, id_barang, no_seri from detail_ruang";
}


$sql = mysql_query($query);
$data = array();
while ($row = mysql_fetch_assoc($sql)) {
 array_push($data, $row);
}

$judul = "LAPORAN KERUSAKAN";
$header = array(
 array("label" => "No. Detail Kerusakan", "length" => 25, "align" => "C"),
array("label" => "Kode Barang", "length" => 25, "align" => "C"),
array("label" => "Nama Barang", "length" => 50, "align" => "C"),
array("label" => "No Ruang", "length" => 50, "align" => "C")
);

$pdf = new PDF("L");
$pdf->AliasNbPages();
$pdf->AddPage();

$pdf->SetFont("Arial", "B", 7);
$pdf->Cell(0, 20, $judul, 0, 1, "C");
$pdf->SetFillColor(87, 190, 224);
$pdf->SetTextColor(33, 71, 84);
$pdf->SetDrawColor(255, 0, 25);
$pdf->SetAutoPageBreak(true, 30);
foreach ($header as $kolom) {
$pdf->Cell($kolom['length'], 5, $kolom['label'], 1, '0', $kolom['align'], true);
}
$pdf->Ln();

$pdf->SetFillColor(87, 190, 224);
$pdf->SetTextColor(33, 71, 84);
$pdf->SetFont('');
$fill = false;
foreach ($data as $baris) {
 $i = 0;
foreach ($baris as $cell) {
    $pdf->Cell($header[$i]['length'], 5, $cell, 1, '0', $kolom['align'], $fill);
    $i++;
}
$fill = !$fill;
$pdf->Ln();
}
$pdf->Output();
?>

我尝试使用SetAutoPageBreak() 有可能这样做吗? 怎么做..? 感谢

1 个答案:

答案 0 :(得分:0)

由于您已经在PHP数组中使用数据,因此最简单的解决方案是使用array_chunk()

define('TABLE_SIZE', 100);
$pages=array_chunk($data, TABLE_SIZE, true);

foreach ($page as $table) {
   foreach ($table as $baris) {
      $i = 0;
      foreach ($baris as $cell) {
         $pdf->Cell($header[$i]['length'], 5, $cell, 1, '0', $kolom['align'], $fill);
         $i++;
      }
   }
   if (count($table)==TABLE_SIZE) {
      // do page break
   }
}