duancenxie2233 2013-01-13 13:53
浏览 48
已采纳

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

i used TCPDF with codeigniter ,when PDF generated from sql query i got one record in the pdf file ,however,the table has three records...

print_r($pdf_data); 

gave me this

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

the controler

<?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');
            }
        }
    ?>

the model

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

}

?>
  • 写回答

1条回答 默认 最新

  • duanqiao1949 2013-01-13 13:56
    关注

    From inside your foreach loop, remove the following segment:

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

    And put it outside the loop body.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 关于用python写支付宝扫码付异步通知收不到的问题
  • ¥50 vue组件中无法正确接收并处理axios请求
  • ¥15 隐藏系统界面pdf的打印、下载按钮
  • ¥15 MATLAB联合adams仿真卡死如何解决(代码模型无问题)
  • ¥15 基于pso参数优化的LightGBM分类模型
  • ¥15 安装Paddleocr时报错无法解决
  • ¥15 python中transformers可以正常下载,但是没有办法使用pipeline
  • ¥50 分布式追踪trace异常问题
  • ¥15 人在外地出差,速帮一点点
  • ¥15 如何使用canvas在图片上进行如下的标注,以下代码不起作用,如何修改