0

codeigniter 错误:我正在尝试运行我的项目并从数据库中选择数据,但出现此错误:

遇到 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);
}

}

4

3 回答 3

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 
于 2013-10-29T09:06:16.283 回答
1

检查这一行:

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

您需要在model function此处调用 a 并且不要再次加载模型。我认为应该是:

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

或者,将其更改foreach为:

foreach($News1 as $news){  #$News1 from $News1 = $this->NewsModel->get_paged_list($this->limit, $offset)->result();
于 2013-10-29T09:04:41.027 回答
0

我想你不知道如何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)

于 2013-10-29T10:11:06.187 回答