2016-08-19 36 views
1

我在codeigniter中的模型文件中获得警告为foreach提供了无效参数()当我试图编辑我的where条件时。在codeigniter中使用mpdf提供的foreach()的无效参数

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

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

控制器的文件

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

工程量清单表

ID |率|单位| project_id

项目表

id |位置| client_id

为了您的信息,我使用MPDF库以及我加载它在我的控制器。

任何帮助将被高度赞赏?

谢谢

+0

您已使用$ id = $ this-> uri-> segment(3)您能否让我们知道网址结构,并且是否检查了您是否获得了$ id值? –

+0

site_url('createpdf/pdf /'.$ row-> id) – ashik

+0

第二种方法是获取$ id的值。调试它。作为Rajkumar R“如果模型返回null意味着它将显示错误无效参数”。所以检查它是否通过价值不是 –

回答

0

您必须检查值是否存在。如果模型返回null意味着它将显示错误无效参数。那么,这样做: -

<?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"; 
} 
?> 
+0

在我添加'$ this-> db-> where('project.id',$ id);'之前加载值“模型中的代码。 – ashik

+0

affter添加你的代码它显示没有结果Found.please帮助我 – ashik

+0

改变这一点,并检查$ this-> db-> join('project','boq.id = project.project_id'); –

0

请检查您查询得到的查询粘贴到你的数据库之后,使用echo $this->db->last_query();检查得到的记录或没有。你可以改变$this->db->join('project', 'project.id = boq.project_id'); to $this->db->join('project', 'project.id = boq.project_id','left');

我想你使用连接然后项目表没有任何可能发出的数据。

+0

中添加你的两个表结构,不存在记录。你能告诉我通过url传递project_id的方式吗? – ashik