2011-08-12 17 views
1

我想呼應我在查看數據庫的領域,但我得到的錯誤:迴盪在視圖頁

Message: Trying to get property of non-object

Filename: views/admin_view_report.php

Line Number: 7

當我呼應了整個陣列,它工作得很好。我找不到有什麼問題。我的控制器的

部分:

function index(){ 
    $this->load->model('viewreport'); 
    $data['records']=$this->viewreport->getAllByChk(); 
    $this->load->view('admin_view_report',$data); 
} 

我的模型的一部分:

function getAllByChk(){ 
    $q = $this->db->get('info'); 
    if ($q->num_rows()>0){ 
     foreach ($q->result_array() as $row) 
     { 
      $data[]=$row; 
     } 
     return $data; 
    } 
} 

的觀點:

<?php foreach($records as $row):?> 

    <?php echo $row->subject; ?> 

<?php endforeach;?> 

如果我只打印data['records']它提供了以下輸出

Array 
(
    [0] => Array 
     (
      [id] => 1 
      [address] => 11/siddeshwari 
      [area] => sid 
      [lat] => 21 
      [lng] => 21 
      [subject] => hello 
      [problem] => lots of problem 
      [image] => 
      [time] => 2011-08-11 23:49:29 
      [register_id] => 1 
      [category_id] => 1 
      [city_city_id] => 1 
      [status_status_id] => 0 
     ) 

    [1] => Array 
     (
      [id] => 2 
      [address] => 134 banani 
      [area] => banai 
      [lat] => 1223 
      [lng] => 2133 
      [subject] => not working 
      [problem] => yesproblem problem problem 
      [image] => 
      [time] => 2011-08-12 01:09:44 
      [register_id] => 1 
      [category_id] => 2 
      [city_city_id] => 1 
      [status_status_id] => 0 
     ) 

) 

但是當我嘗試打印問題或主題只有它給出了錯誤。

+0

你能改變<?php echo $ row-> subject; ?>到<?php print_r($ row); ?>並向我們顯示輸出? – mattumotu

+0

輸出上面給出 – tan

回答

2

result_array()返回數組的數組,而不是對象。

在你看來:

$row->subject 

應該是:

$row['subject'] 

如果你想$行是一個對象,改變result_array()result()。這會給你一個對象數組。

另外foreach ($q->result_array() as $row)是多餘的。

它只是複製​​至$data

你不需要那樣做,只需要return $q->result_array();

+0

它工作..感謝:) – tan

+0

@tan:不客氣:-) –

0
function getAllByChk(){ 
    $data = array(); 
    //remove if 

而且,結果取無論是作爲對象$q->result()echo $row['subject']

-1

您也可以返回結果()

  function getAllByChk(){ 

$q = $this->db->get('info'); 
     if ($q->num_rows()>0){ 
     foreach ($q->result_array() as $row) 
      { 
      $data[]=$row; 

      } 
     return $data->result(); 
      } 
      } 
+1

這實際上是錯誤的!根據代碼,$ data是一個數組,它需要是'$ q-> result()',而不是'foreach',它無論如何都是無用的。 – dakdad

+0

'$ data'沒有'result()'函數,因爲它是一個數組。你想返回'$ q-> result()'。 –

1

我會做以下

function getAllByChk() 
{ 
    $q = $this->db->get('info'); 
    if ($q->num_rows() > 0) 
    { 
     return $data->result(); 
    } 
    return false; 
} 

然後在觀點

if($records) : 
    foreach($records as $row) : 
     print_r($row); 
    endforeach; 
else: 
    echo 'No Records'; 
endif;