How do I display data in my foreach in EOD tcpdf?

为君一笑 提交于 2019-12-13 06:40:35

问题


I am new in the tcpdf.. I am currently doing a project where I am using the tcpdf(because I want to export my table in pdf that has a template of it). I tried to do some research and I've found the method writeHTML(). In this method I can now make any html tags

Controller

public function testing(){
        $this->load->library('Pdf');
        $this->load->model('newModel');
        $data['accounts'] = $this->newModel->get('users');
        $this->load->view('admin/testing',$data);
    }

View

$txt = <<<EOD
<table cellspacing="0" cellpadding="1" border="1">
    <tr>

        <td>COL 2 - ROW 1</td>
        <td>COL 3 - ROW 1</td>
    </tr>
    <tr>
        <?php foreach($accounts as $try):?>
        <td>$try->first_name</td>
        <td>Hala2   </td>
        <?php endforeach?>
    </tr>


</table>
EOD;
$pdf->writeHTML($txt, true, false, false, false, '');

Model

public function get($table)
    {
        $result = $this->db->get($table);
        return $result->result();
    }

Question: How can I display all my data in EOD? Is there any method like writePHP()? To allow me to do some php coding?


回答1:


You can not use loop directly inside here document(<<<).You have to concatenate.Like this

  <?php
$accounts = array('name'=>'angle');//assumed array
//print_r($accounts);
$txt = <<<EOD
 <table cellspacing="0" cellpadding="1" border="1">
    <tr>

        <td>COL 2 - ROW 1</td>
        <td>COL 3 - ROW 1</td>
    </tr>
EOD;
foreach($accounts as $key=>$value){
$txt.=<<<EOD
    <tr>
      <td>{$accounts['name']}</td>
      <td>It is easy</td>
    </tr>
EOD;
 }
$txt.=<<<EOD

</table>
EOD;

echo $txt;
?>

In your model retrun result in array format.So that you can try like above

public function get($table)
    {
        $result = $this->db->get($table);
        return $result->result_array();//it returns result in array format
    }



回答2:


use mpdf insted tcpdf if you are looking for getting data from database and put into html and create pdf.

mpdf docs and library



来源:https://stackoverflow.com/questions/41917689/how-do-i-display-data-in-my-foreach-in-eod-tcpdf

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!