foreach循环只打印MySQL表中的一条记录

我正在尝试使用 fpdf 将表的记录打印到 pdf 文件中,对此我在每个循环中使用,但它只打印一条记录,尽管它在没有 fpdf 类的情况下工作正常。这是代码:


if(isset($_GET['show_id'])){

    $id = ($_GET['show_id']);

    $comments = selectAll('proposal_comment', ['proposal_id' => $id]);


    foreach($comments as $comment){

        $pdf = new FPDF();

        $pdf->AddPage();

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

        $pdf->Cell(100,10,$comment['message']);

        $pdf->Output();

    }

}

知道为什么会这样吗?


慕码人2483693
浏览 73回答 1
1回答

撒科打诨

将初始化代码移到循环之外,然后循环将仅添加单元格if(isset($_GET['show_id'])){    $id = ($_GET['show_id']);    $comments = selectAll('proposal_comment', ['proposal_id' => $id]);    $pdf = new FPDF();    $pdf->AddPage();    $pdf->SetFont('Arial','B',16);    foreach($comments as $comment){        $pdf->Cell(100,10,$comment['message']);    }    $pdf->Output();}
打开App,查看更多内容
随时随地看视频慕课网APP