【问题标题】:codeigniter error: I am trying to run my project and Select data from the datatbase but I get this error:codeigniter 错误:我正在尝试运行我的项目并从数据库中选择数据,但我收到此错误:
【发布时间】:2013-11-08 08:07:59
【问题描述】:

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

}

【问题讨论】:

    标签: php codeigniter


    【解决方案1】:

    你错了

     $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 
    

    【讨论】:

      【解决方案2】:

      检查这一行:

      $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();
      

      【讨论】:

        【解决方案3】:

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

        【讨论】:

        • 我使用它 Aness 但我收到此消息遇到 PHP 错误 严重性:通知消息:尝试获取非对象文件名的属性:控制器/NewsController.php 行号:45 并且当我使用this : foreach($News1 as $news){ #$News1 from $News1 = $this->NewsModel->get_paged_list($this->limit, $offset)->result();...我收到了这个错误信息:致命错误:在第 46 行调用 C:\AppServ\www\News\application\controllers\NewsController.php 中未定义的函数 anchor()
        • foreach($newss as $news) 不是 $News1
        • $News1 = $this->NewsModel->get_paged_list($this->limit, $offset)->result();对不起,你已经在加载这个 na 了吗?然后删除 $newss=$this->load->model('NewsModel');并使用 foreach($News1 as $news)
        猜你喜欢
        • 1970-01-01
        • 2021-02-16
        • 1970-01-01
        • 2019-11-22
        • 2023-04-02
        • 1970-01-01
        • 2015-11-06
        • 2020-01-28
        • 2021-10-22
        相关资源
        最近更新 更多