2012-10-18 63 views
1

我在我的視圖文件中出現此錯誤。這裏是我的代碼,請幫助我,告訴做什麼?調用未定義的函數結果()

<?php 
//foreach($records->result() as $row): 
foreach(result() as $row): 
echo $row->title; 
endforeach; 
?> 

這裏是我的控制文件:

<?php if (! defined('BASEPATH')) exit('No direct script access allowed'); 

class Hello extends CI_Controller 
{ 
    public function index() 
    { 
     $this->load->model('hello_model'); 
     $data['records']=$this->hello_model->getAll(); 
     $this->load->view('you_view',$data); 
     //$this->load->view('you_view'); 
    } 
} 
?> 

我也張貼在這裏我的模型文件。 Ihv嘗試了一些我,但仍然getiing這個錯誤。 Dnt knw wt要做。

<?php 

class Hello_model extends CI_Model 
{ 
    function __construct() 
    { 
     // Call the Model constructor 
     parent::__construct(); 
    } 

    function getAll() 
    { 
    $q=$this->db->get('test'); // query where 'test' is table name. 

     if($q->num_rows()>0) 
     { 
      foreach ($q->result() as $row) 
      { 
       $data[]=$row; 
      } 
     return $data; 
     } 
    } 
} 
?> 
+0

在模型中,檢查'num_rows',回'$ Q-> result_array後() ;' –

+0

如果你想返回結果作爲數組,你爲什麼要用'$ q-> result()'? :D很高興看到[說明](http://codeigniter.com/user_guide/database/results.html) –

+0

thnx回答frnd ...但它的工作現在.....我用$記錄在結果的位置()在查看文件中:) –

回答

1

$records已經擁有你的數據,因此這應該工作:

foreach($records as $row){ 
    echo $row->title; 
} 
+0

哦!!!!!多謝Mudshark。它像魅力一樣工作。 Thanx給你時間你也muhammet :)歡呼:) –

0

你不需要在你的視圖中使用result(),因爲在你的模型中你已經做了。

就試試這個:

​​
+0

你只是不能使用result()函數本身。你必須用一個包含result()的變量來使用它。 – 2012-10-18 12:04:42

+0

它保存的數據從模型....我高舉張貼代碼也.... –

+0

雅我hv chnaged那muhammet ...它的工作現在....謝謝你洙很多給予時間:) tkcre –