【问题标题】:foreach loop print only one record from MySQL tableforeach 循环只打印 MySQL 表中的一条记录
【发布时间】:2020-08-29 16:05:51
【问题描述】:

我正在尝试使用 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();
    }
}

知道为什么会这样吗?

【问题讨论】:

  • 您想要每页 1 条消息吗?还是一页上的所有消息?
  • @RiggsFolly 我希望所有消息都在一页中。
  • 旁注:$id = ($_GET['show_id']); () 括号。

标签: php mysql foreach fpdf


【解决方案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();
}

【讨论】:

  • 非常感谢,我是编程的超级菜鸟:)
  • 您现在可能需要处理循环内的输出See the manual
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-17
  • 1970-01-01
  • 2021-05-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多