如何使用codeigniter分页链接页面?

时间:2015-11-10 06:03:07

标签: codeigniter codeigniter-2 codeigniter-3

如何使用codeigniter分页链接页面?

问题在于变量$ start它应该将起始值传递给数据库限制查询。但事实并非如此。

模型

public function fetch_countries($limit,$start) {
    $this->db->limit($limit, $start);

    //$query ="SELECT * FROM  mail_to Order By id Desc LIMIT ".$limit.",".$start;
    $query = $this->db->get("mail_to");

   //$qry=$this->db->query($query);
   var_dump($query->result_array());

    if($query->num_rows()>0){

       return $query->result_array();

    }
    else
    {
    return false;
}

}

控制器

public function example1() {
    $config = array();
    $config["base_url"] = base_url() . "index.php/admin/user/example1";
    $config["total_rows"] = $this->mdl_user->record_count();
    $config["per_page"] = 5;

    $config["uri_segment"] = 3;

    $this->pagination->initialize($config);




    $page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;

    //var_dump($config['per_page']);
    $data["results"] = $this->mdl_user->fetch_countries($config["per_page"],$page);
        //var_dump($data['results']);
    $data["links"] = $this->pagination->create_links();

    $this->load->view("example1", $data);
}

1 个答案:

答案 0 :(得分:1)

URI细分应为

$config["uri_segment"] = 4;
$page = ($this->uri->segment(4)) ? $this->uri->segment(4) : 0;

在模型中

public function fetch_countries($limit, $page)
{
    $query = $this->db->query("SELECT * FROM mail_to LIMIT $page, $limit");
    $result = $query->result_array();
    return $result; 
}

在控制器中

public function example1() {

    $config["base_url"] = base_url() . "index.php/admin/user/example1";
    $config["total_rows"] = $this->mdl_user->record_count();
    $config["per_page"] = 5;
    $config['uri_segment'] = 4;
    $limit = $config['per_page'];
    $this->pagination->initialize($config);

    $page = ($this->uri->segment(4)) ? $this->uri->segment(4) : 0;

    $data["results"] = $this->mdl_user->fetch_countries($page $limit);
    $data["links"] = $this->pagination->create_links();

    $this->load->view("example1", $data);
}