0

我是 codeIgniter 的新手。我的问题是致命错误:在第 12 行的 C:\xampp\htdocs\ci_series_5\application\controllers\posts.php 中的非对象上调用成员函数 get_posts()

class Posts extends CI_Controller{

    function _construct(){
        parent::_construct();
        $this->load->model('post');

    }

    function index(){
        $data['posts']=$this->post->get_posts();         line 12
        $this->load->view('post_index',$data);
    }

模型中的 Post.php 如下

class Post extends CI_Model{

    function get_posts($num=20,$start=0){           
        $this->db->select()->from('posts')
        ->where('active',1)->order_by('date_added', 'desc')
        ->limit($num,$start);
        $query=$this->db->get();
        return $query->result_array();
    }

    function get_post($postID){
        $this->db->select()->from('posts')->where(array('active'=>1,
        'postID'=>$postID))->order_by('date_added','desc');
        $query=$this->db->get();
        return $query->first_row('array');
    }
4

2 回答 2

0

尝试添加_model您的型号名称。post_model.php

<?php
class Post_Model extends CI_Model {
  public function get_posts() {
   // code here
  }
}

在你的控制器中,

class Posts extends CI_Controller{

    public function _construct(){
        parent::_construct();
        $this->load->model('post_model');

    }

    public function index(){
        $data['posts']=$this->post_model->get_posts();         
        $this->load->view('post_index',$data);
    }
于 2013-10-05T09:58:36.567 回答
-1

如果想从任何形式获取数据,那么你的代码应该是:

$data['posts']=$this->post->('field_name')


<form action="<?php echo base_url();?>/posts" method="post">
<input type="text" name="field_name">
<input type="submit">
</form>
于 2013-10-05T09:57:42.023 回答