gpt4 book ai didi

php - 在 codeigniter 中使用 mpdf 为 foreach() 提供的参数无效

转载 作者:行者123 更新时间:2023-11-30 22:14:09 25 4
gpt4 key购买 nike

当我尝试编辑我的 where 条件时,我在 codeigniter 的模型文件中收到警告说 Invalid argument supplied for foreach()

其实我想根据外键加载记录。

当我添加这行代码时,$this->db->where('project.id',$id);在模型中,我收到上述错误。

Controller 文件

<?php

class Createpdf extends CI_Controller {

public function __construct() {
parent::__construct();
$this->load->helper('url');
$this->load->library('m_pdf');
$this->load->model('boq_pdf_model');
$this->load->model('project_list_model');
}
function pdf()
{
$id= $this->uri->segment(3) ;
$data['projects'] = $this->project_list_model->show_projects();
$data['boq'] = $this->boq_pdf_model->get_boq($id);
$this->load->view('boq/boq_report',$data);

}
public function topdf(){
//this data will be passed on to the view
$data['the_content']='RCJ Constructions';

//load the view, pass the variable and do not show it but "save" the output into $html variable
$html=$this->load->view('boq/boq_report', $data, true);

//this the the PDF filename that user will get to download
$pdfFilePath = "boq_report.pdf";

//load mPDF library
//$this->load->library('m_pdf');
//actually, you can pass mPDF parameter on this load() function
$pdf = $this->m_pdf->load();


$id= $this->uri->segment(3) ;
$data['projects'] = $this->project_list_model->show_projects();
$data['boq'] = $this->boq_pdf_model->get_boq($id);
$html = $this->load->view('boq/boq_report', $data, true);

//generate the PDF!
$pdf->WriteHTML($html);


//offer it to user via browser download! (The PDF won't be saved on your server HDD)
$pdf->Output($pdfFilePath, "D");
}
}
?>

模型文件

function get_boq($id){ 
$this->db->select('*');
$this->db->from('boq');
$this->db->join('project', 'project.id = boq.project_id');
$this->db->where('project.id',$id);
$this->db->order_by('item_no','ASC');
$getData = $this->db->get();
if($getData->num_rows() > 0)
return $getData->result_array();
else return null;

}
}

查看文件

 <?php
foreach ($boq as $rows) {
<tr>
<td><?php echo $rows['unit'] ?></td>
<td><?php echo $rows['rate']?></td>
<td><?php echo $rows['laboure_hrs'] ?></td>
<td><?php echo $rows['laboure_cost'] ?></td>
</tr>
<?php
//$i++;
}
?>

采购 list

编号 |率|单位|项目编号

项目表

编号 |地点| client_id

For your information, i am using MPDF library as well and i am loading it in my controller.

任何帮助将不胜感激?

谢谢

最佳答案

您必须检查值是否存在。如果模型返回 null 意味着它将显示错误 invalid argument。所以,这样做:-

<?php
if(count($boq) > 0) {
foreach ($boq as $rows) {
<tr>
<td><?php echo $rows['unit'] ?></td>
<td><?php echo $rows['rate']?></td>
<td><?php echo $rows['laboure_hrs'] ?></td>
<td><?php echo $rows['laboure_cost'] ?></td>
</tr>
<?php
//$i++;
}
} else {
echo "No Results Found";
}
?>

关于php - 在 codeigniter 中使用 mpdf 为 foreach() 提供的参数无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39031657/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com