我正在尝试使用php和我从会话中获取的元素生成pdf。目前生成了pdf,但无论我有多少项,它始终只显示1 ..第一项。我觉得我的查询有些不对劲但无法找到。这是我到目前为止的脚本。
$files = $_SESSION['itemid'];
if(is_array($files)) {
foreach($files as $file) {
$sql = "SELECT upload_id, upload_title, upload_description FROM document_upload WHERE upload_id = :id";
$result = $pdo->prepare($sql);
$result->bindParam(":id", $file);
$result->execute();
$resArray = $result->fetchAll();
foreach ( $resArray as $res )
{
$pdf = new FPDF();
$pdf->AddPage();
$pdf->SetFont('Arial','',18);
$pdf->Cell(0,6,'Selected',0,1,'C');
$pdf->SetFont('Arial','',12);
$pdf->Ln();
$pdf->Cell(20,12,$res['upload_id'],1);
$pdf->Cell(50,12,$res['upload_title'],1);
$pdf->Cell(120,12,$res['upload_description'],1);
$pdf->Output();
}
}
}
当我print_r($files);
时,我看到Array ( [0] => 11 [1] => 12 [2] => 14 )
但当我在foreach中print_r($file);
时,我只看到11
。
我正在使用FPDF
答案 0 :(得分:0)
问题在于我将$pdf = new FPDF();
和$pdf->Output();
放在循环中。当我把它们移到外面时,一切都很完美。
这是完整且有效的解决方案:
if(is_array($files))
{
$pdf = new FPDF();
$pdf->AddPage();
$pdf->SetFont('Arial','',18);
$pdf->Cell(0,6,'Selected',0,1,'C');
foreach($files as $file)
{
$sql = "SELECT upload_id, upload_title, upload_description FROM document_upload WHERE upload_id = :id";
$result = $pdo->prepare($sql);
$result->bindParam(":id", $file);
$result->execute();
$resArray = $result->fetchAll();
foreach ( $resArray as $res )
{
$pdf->SetFont('Arial','',12);
$pdf->Ln();
$pdf->Cell(20,12,$res['upload_id'],1);
$pdf->Cell(50,12,$res['upload_title'],1);
$pdf->Cell(120,12,$res['upload_description'],1);
}
}
$pdf->Output();
}