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

时间:2013-10-29 08:57:55

标签: php codeigniter

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

遇到PHP错误

严重性:警告

消息:为foreach()提供的参数无效

文件名:controllers / 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);
}

}

3 个答案:

答案 0 :(得分: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 

答案 1 :(得分: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();

答案 2 :(得分: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)