无法更改codeigniter分页中的查询

时间:2014-03-18 19:08:54

标签: php database codeigniter pagination

我使用的是codeigniter分页类,它在控制器第一次加载时工作正常,在下次加载时它不会改变数据。

这是我的控制器:

class test extends CI_Controller{
public function __construct() {
    parent:: __construct();
    $this->load->model("getdata");
    $this->load->library("pagination");
}
public function read($page=0){
    $this->load->view('header');
    $config['base_url'] = base_url()."index.php/test/read";
    $config["total_rows"] = $this->getdata->record_count();
    $config["per_page"] = 5;
    $this->pagination->initialize($config); 
    $data["results"] = $this->getdata->basic($config["per_page"],$page);
    $data["links"] = $this->pagination->create_links();
    $this->load->view('readarea',$data);
    $this->load->view('footer');
 }
};  
?>

这是我的模特:

<?php
class getdata extends CI_Model{
    public function __construct() {
        parent::__construct();
    }

   public function record_count() {
     return $this->db->count_all("table1");
   }


    public function basic($limit,$start){
$this->db->limit($limit,$start);    
$query = $this->db->query("SELECT table1.id,table1.post_id,table1.author,table1.quest,table1.first_name,table2.last_name,table2.pic,table2.userid FROM table1,table2 WHERE table1.post_id = table2.id ORDER BY table1.id DESC");
    if($query->num_rows() > 0) {
    return $query->result();     
    }
    return FALSE;
    }

};
?>

注意:链接已创建,但无法从数据库中获取下一个数据...

2 个答案:

答案 0 :(得分:0)

在模型中尝试以下查询。

   public function basic($limit,$start){

            $query = $this->db->query("SELECT table1.id,table1.post_id,table1.author,table1.quest,table1.first_name,table2.last_name,table2.pic,table2.userid FROM table1,table2 WHERE table1.post_id = table2.id ORDER BY table1.id DESC LIMIT $start, $limit");
        if($query->num_rows() > 0) {
           return $query->result();     
        }
        return FALSE;
    }

答案 1 :(得分:0)

将此功能更改为此

public function read($page = null){
    $this->load->view('header');
    $config['base_url'] = base_url()."index.php/test/read";
    $config["total_rows"] = $this->getdata->record_count();
    $config["per_page"] = 5;

    $this->pagination->cur_page = $page; // add this line into it i hope this work

    $this->pagination->initialize($config); 
    $data["results"] = $this->getdata->basic($config["per_page"],$page);
    $data["links"] = $this->pagination->create_links();
    $this->load->view('readarea',$data);
    $this->load->view('footer');
 }
};