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