2012-11-08 139 views
1

簡單的問題,但我不知道它爲什麼沒有正確打印或按預期工作。我有這個模型(工作,因爲當我的print_r($結果)它顯示的數據:Codeigniter打印查詢結果

function get_latest_entries() 
{ 
    $query = $this->db->get('property'); 
    return $query->result_array(); 
} 

而對於控制器:

public function index() 
{ 
    $resultSet['properties'] = $this->propertymodel->get_latest_entries(); 
    $this->load->view('home', $resultSet); 
} 

在我想要遍歷數組在視圖(表有說明,城市,地址欄):

<?php 
foreach($resultSet as $item) 
{ 
    echo $item->city; 
    echo $item->description; 
} 
?> 

我得到兩個記錄主頁上我在哪裏顯示結果如上:

嚴重性:注意 消息:未定義變量:的resultSet 文件名:視圖/ home.php 行號:16

而且

嚴重性:警告 消息:用於提供參數無效foreach() 文件名:views/home.php 行號:16

+0

使用$屬性,而不是$的resultSet –

+0

把它作爲一個回答@ShayanHusaini所以我可以把它標記爲一個:) –

+0

把它作爲answere –

回答

3

使用$properties代替$resultSet

2

使用這個......你是路過$properties到您的視圖,而不是$resultSet ..

<?php 
     foreach($properties as $item) // use properties 
     { 
     echo $item->city; 
     echo $item->description; 
     } 
    ?> 
0

這裏是你的代碼的問題。 - 您已將數據返回爲數組數組,並且您試圖作爲對象訪問, - 第二個是,您已傳遞屬性變量以查看並正在訪問resultSet。 - 所以這裏是看你的代碼包含錯誤

<?php 
foreach($resultSet as $item) 
{ 
    echo $item->city; 
    echo $item->description; 
} 
?> 

- 和你的代碼的正確版本是在這裏...

<?php 
foreach($properties as $item) 
{ 
    echo $item['city']; 
    echo $item['description']; 
} 
?>