2016-08-19 37 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