2013-10-29 58 views
0

笨的錯誤:我嘗試運行我的項目,並從datatbase選擇數據,但我得到這個錯誤:笨的錯誤:我嘗試運行我的項目,並從datatbase選擇數據,但我得到這個錯誤:

遇到

甲PHP錯誤

嚴重性:警告

消息:()提供的foreach無效參數

文件名:控制器/ NewsController.php

行號:43

控制器:

class NewsController extends CI_Controller{ 
// num of records per page 
private $limit = 10; 

function News(){ 
    parent::Controller(); 

    // load library 
    $this->load->library(array('table','validation')); 

    // load helper 
    $this->load->helper('url'); 

    // load model 
    $this->load->model('NewsModel','',TRUE); 
} 
function index($offset = 0){ 
    $this->load->model('NewsModel','',TRUE); 
    // offset 
    $uri_segment = 3; 
    $offset = $this->uri->segment($uri_segment); 

    // load data 

    $News1 = $this->NewsModel->get_paged_list($this->limit, $offset)->result(); 

    // generate pagination 
    $this->load->library('pagination'); 
    $config['base_url'] = ('News/index/'); 
    $config['total_rows'] = $this->NewsModel->count_all(); 
    $config['per_page'] = $this->limit; 
    $config['uri_segment'] = $uri_segment; 
    $this->pagination->initialize($config); 
    $data['pagination'] = $this->pagination->create_links(); 

    // generate table data 
    $this->load->library('table'); 
    $this->table->set_empty(" "); 
    $this->table->set_heading('id', 'title', 'image', 'discreption', 'Actions'); 
    $i = 0 + $offset; 
    $newss=$this->load->model('NewsModel'); 
    foreach($newss as $news){ 
     $this->table->add_row(++$i, $news->title, $news->image,$news->discreption); 
      anchor('News/view/'.$news->id,'view',array('class'=>'view')).' '. 
      anchor('News/update/'.$news->id,'update',array('class'=>'update')).' '. 
      anchor('News/delete/'.$news->id,'delete',array('class'=>'delete','onclick'=>"return confirm('Are you sure want to delete this person?')")); 

    } 
    $data['table'] = $this->table->generate(); 

    // load view 
    $this->load->view('NewsList', $data); 
} 

型號:

class NewsModel extends CI_Model{ 
// table name 
private $tbl_News= 'news'; 

function News(){ 
    parent::Model(); 
} 
// get number of News in database 
function count_all(){ 
    return $this->db->count_all($this->tbl_News); 
} 
// get with paging 
function get_paged_list($limit = 10, $offset = 0){ 
    $this->db->order_by('id','asc'); 
    return $this->db->get($this->tbl_News, $limit, $offset); 
} 
// get News by id 
function get_by_id($id){ 
    $this->db->where('id', $id); 
    return $this->db->get($this->tbl_News); 
} 
// add news 
function save($news){ 
    $this->db->insert($this->tbl_News, $news); 
    return $this->db->insert_id(); 
} 
// update News by id 
function update($id, $news){ 
    $this->db->where('id', $id); 
    $this->db->update($this->tbl_News, $news); 
} 
// delete News by id 
function delete($id){ 
    $this->db->where('id', $id); 
    $this->db->delete($this->tbl_News); 
} 

}

回答

1

檢查這一行:

$newss = $this->load->model('NewsModel'); 

您需要調用model function,並且不會再次加載模型。我覺得應該是:

$newss = $this->NewsModel->get_paged_list(); 

或者,改變foreach到:

foreach($News1 as $news){ #$News1 from $News1 = $this->NewsModel->get_paged_list($this->limit, $offset)->result(); 
+0

謝謝Nil'Z,它的工作原理。 –

2

這裏,你就錯了

$newss=$this->load->model('NewsModel'); 

做這樣的

$this->load->model('NewsModel'); //this line just loads model 
$newss=$this->NewsModel->your_function_in_model(); //here you call function from models that you ve loaded 
+0

感謝Nabin,它的工作原理 –

0

我想你不知道h ow load->model()工程。

$this->load->model('NewsModel'); // will load NewsModel on NewsController 
// you can call loaded model by $this->modelName 
$newss = $this->NewsModel->get_paged_list(10, $i); // will call get_paged_list() on NewsModel and result will save on newss 

你的代碼中沒有這樣的函數調用。現在你可以做到這一點foreach($newss as $news)

+0

我用它Aness但我得到這個消息 一個PHP錯誤遇到 嚴重性:注意 消息:試圖讓非對象 文件名的屬性:控制器/ NewsController.php 行號:45 當我用這個:foreach($ News1作爲$ news){#$ News1從$ News1 = $ this-> NewsModel-> get_paged_list($ this-> limit,$ offset) - > result() ; ...我得到了這個錯誤信息:致命錯誤:調用未定義的函數錨點()在C:\ AppServ \ www \ News \ application \ controllers \ NewsController.php上線46 –

+0

foreach($ newss as $ news)not $ News1 –

+0

$ News1 = $ this-> NewsMod el-> get_paged_list($ this-> limit,$ offset) - > result(); sry你已經加載了這個na?然後刪除 $ newss = $ this-> load-> model('NewsModel');並使用foreach($ news1作爲$ news) –

相關問題