【问题标题】:Unable to change the query in codeigniter pagination无法在 codeigniter 分页中更改查询
【发布时间】:2014-04-24 16:34:02
【问题描述】:

我正在使用 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;
    }

};
?>

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

【问题讨论】:

    标签: php database codeigniter pagination


    【解决方案1】:

    在模型中尝试以下查询。

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

    【讨论】:

      【解决方案2】:

      把这个函数改成这个

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

      【讨论】:

        猜你喜欢
        • 2015-02-13
        • 2017-09-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-02-01
        • 2015-07-03
        • 1970-01-01
        相关资源
        最近更新 更多