2011-10-26 73 views
-1

我正在使用PHP和fpdf在pdf中生成我的視圖。我的問題是如何限制每頁數據量? 如果我有100行數據,結果是10頁。所以我必須每頁限制10行。 我有這樣的代碼:限制數據庫中每頁的數據量相同fpdf

<?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() 是有可能做到這一點..? 如何做到這一點..? 感謝

回答

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 
    } 
}