php - 为什么 tcpdf 库从循环中输出一条记录?

标签 php mysql codeigniter tcpdf

我将 TCPDF 与 codeigniter 一起使用,当从 sql 查询生成 PDF 时,我在 pdf 文件中得到了一条记录,但是,该表有三条记录...

print_r($pdf_data); 

给我这个

Array ( [0] => Array ( [no] => 1 [name] => Jamal [address] => Ùخر ) [1] => Array ( [no] => 2 [name] => Jina [address] => Washington D.C ) [2] => Array ( [no] => 3 [name] => Dijandra [address] => Nairboi ) )

Controller

<?php

    class example extends CI_Controller {

        function __construct() {
            parent::__construct();
            $this->load->library('pdf');
        }

        function b() {
            $this->pdf->AddPage();
            $font1 = $this->pdf->addTTFfont(K_PATH_FONTS . 'arial.ttf', 'TrueTypeUnicode', '', 8);
            $this->pdf->SetFont($font1, '', 15, '', false);
            $this->load->model('member_model');
            $pdf_data = $this->member_model->alldata();
            foreach ($pdf_data as $rows) {

                $tbl = '<table style="width: 638px;" cellspacing="0">';
                $id = $rows['no'];
                $name = $rows['name'];
                $address = $rows['address'];

                $tbl .= '<tr>
                <td style="border: 1px solid #000000; width: 150px;">' . $id . '</td>
                <td style="border: 1px solid #000000; width: 378px;">' . $name . '</td>
                <td style="border: 1px solid #000000; width: 110px; text-align:center">' . $address . '</td>
                </tr>';

                $tbl .= '</table>';

                $this->pdf->writeHTML($tbl, true, false, false, false, '');
                  $this->pdf->Output('example_001.pdf', 'I');
            }
        }
    ?>

模型

<?php

class Member_model extends CI_Model {

    function __construct() {
        parent::__construct();
    }

    function alldata() {
        $this->db->select('*')->from('tb_member')->order_by('no', 'ASC');
        $getData = $this->db->get();
        if ($getData->num_rows() > 0)
            return $getData->result_array();
        else
            return NULL;
    }

}

?>

最佳答案

从您的 foreach 循环中,删除以下片段:

$this->pdf->Output('example_001.pdf', 'I');

并将其放在循环体之外。

关于php - 为什么 tcpdf 库从循环中输出一条记录?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14304144/

相关文章:

php - 多个复选框内的 codeigniter

mysql - 未知的慢 SQL 查询

MySQL:无法创建表(错误号:150)

mysql - 如何从数据库中提取孙子

PHP:删除 `â` 或 `â€` 的正则表达式?

php - extent codeigniter session 过期

php - 消息:未定义的属性:Users::$form_validation

PHP MYSQL 从多个列中获取最小值

php - 数组中的mysql选择查询

php - 使用 Mysql Query Profiler 了解查询的执行时间和 php 脚本中的性能