在codeigniter中使用html2pdf导出pdf上的foreach错误数组

时间:2016-12-15 16:12:06

标签: php mysql codeigniter

我从未在v2下的Codeigniter中导出过PDF MySQL。但是当我切换到v3.1.0时,在foreach导出到PDF有问题是什么问题?

ERORR

  

遇到PHP错误

     

严重性:注意

     

消息:未定义的偏移量:1

     

文件名:html2pdf / html2pdf.class.php

控制器

public function print_data(){
    ob_start();
    $class= $this->uri->segment(3); 
    $data = array(
                    'get_data' => $this->My_model->detail_id_data($class)
    );  
    $this->load->view('print_detail_data', $data);
    $html = ob_get_contents();
    ob_end_clean();

    require_once('./assets/html2pdf/html2pdf.class.php');
    $pdf = new HTML2PDF('P', 'A4', 'en', true, 'UTF-8', 3);
    $pdf->WriteHTML($html);
    $pdf->Output('id_detail.pdf');
}

Models

    function detail_id_data($class) {
        $query = $this->db->select('*')
                          ->from('tb_student')
                          ->where('class', $id_data)
                          ->get();

        if ($query->num_rows() > 0) {
            foreach ($query->result() as $data) {
                $hasil[] = $data;
            }
            return $hasil;
        } 
    }

浏览

<table style="width: 100%; border: solid 1px black;">
    <thead>
      <tr>
        <th>ID</th>
        <th>Name </th>
        <th>Class</th>
        <th>Study</th>
      </tr> 
    </thead>    
    <tbody> 
    <?php 
        $no=1;
        foreach ($get_data as $row){    
    ?>
      <tr>
        <td><?php $no; ?></td>
        <td><?php echo $row->name; ?></td>
        <td><?php echo $row->class; ?></td>
        <td><?php echo $row->study; ?>f</td>        
      </tr>   
    <?php $no++; } ?>  
    </tbody>
</table>

1 个答案:

答案 0 :(得分:0)

在模型中使用row()代替result() ..希望它能够正常运作。

if ($query->num_rows() > 0) {
            foreach ($query->row()) as $data) {
                $hasil[] = $data;
            }
            return $hasil;
        }

像这样改变你的观点......

<table style="width: 100%; border: solid 1px black;">
    <thead>
      <tr>
        <th>ID</th>
        <th>Name </th>
        <th>Class</th>
        <th>Study</th>
      </tr> 
    </thead>    
    <tbody> 
    <?php 
        $no=1;
        foreach ($get_data as $row){    
    ?>
      <tr>
        <td><?php $no; ?></td>
        <td><?php echo $row['name']; ?></td>
        <td><?php echo $row['class']; ?></td>
        <td><?php echo $row['study']; ?>f</td>        
      </tr>   
    <?php $no++; } ?>  
    </tbody>
</table>

在控制器中,

替换它..

 $data = array(
                    'get_data' => $this->My_model->detail_id_data($class)
    );

通过

$data['get_data'] = $this->My_model->detail_id_data($class);

您是否获得$class的价值。请尝试以下

echo $class;