2016-10-03 37 views
0

嗨我已经在PHP代码中实现了分页但是它在点击分页链接时不工作。它为所有页面显示相同的数据。这里是代码。如何在codeigniter中创建分页

控制器:

class Testimonial extends CI_Controller { 
function __construct() { 
     parent::__construct(); 
     //here we will autoload the pagination library 
     $this->load->library('pagination'); 
    } 

public function index() 
{ 
    $this->load->model('testimonial_model'); 
    $config = array(); 
    $config["base_url"] = base_url('testimonial/index'); 
    $config['total_rows'] = $this->db->count_all("testimonials");//here we will count all the data from the table 
    $config['per_page'] = 6;//number of data to be shown on single page 
    $config["uri_segment"] = 2; 
    $this->pagination->initialize($config); 
    $page = ($this->uri->segment(2)) ? $this->uri->segment(2) : 0; 
    $data["records2"] = $this->testimonial_model->get_all_testimonials($config["per_page"], $page); 
    $data["links"] = $this->pagination->create_links();//create the link for pagination 
    $data['mainpage'] = "testimonial"; 
    $this->load->view('templates/template',$data); 
} 

型号:

class Testimonial_model extends CI_Model 
{  
function get_all_testimonials($limit, $start) 
{ 
    $this->db->limit($limit, $start); 
    $this->db->select('T.*'); 
    $this->db->from('testimonials AS T'); 
    $this->db->where(array('T.status'=>1)); 
    $q = $this->db->get(); 
    if($q->num_rows()>0) 
    { 
     return $q->result(); 
    } 
    else 
    { 
     return false; 
    } 
} 
} 

查看:

<div class="pagination"><?php echo $links; ?></div>  
+0

任何人谁可以帮我这一点 – user6728960

+1

尝试使用URI segmant 3:$页=($这个 - > URI->段(3))? $ this-> uri-> segment(3):0; – Rijin

+0

@日金谢谢你的工作 – user6728960

回答

0

尝试以下的意志可以帮助你,

public function index() 
{ 
    $this->load->model('testimonial_model'); 
    $config = array(); 
    $config["base_url"] = base_url('testimonial/index'); 
    $config['total_rows'] = $this->db->count_all("testimonials");//here we will count all the data from the table 
    $config['per_page'] = 6;//number of data to be shown on single page 
    $config["uri_segment"] = 2; 
    $this->pagination->initialize($config); 
    $page = ($this->uri->segment(2)) ? $this->uri->segment(2) : 0; 
    $data["records2"] = $this->testimonial_model->get_all_testimonials($config["per_page"], (($page-1)*$config["per_page"])); 
    $data["links"] = $this->pagination->create_links();//create the link for pagination 
    $data['mainpage'] = "testimonial"; 
    $this->load->view('templates/template',$data); 
} 
+0

在第一页我有6个职位和在第二页,我有2个职位对于一个页面,它应该显示6如果我点击第二页,它显示的是第一页相同的数据。 – user6728960

+0

你有没有试过上面的代码? – kc1994

+0

打印您的查询,看看您将在第二页点击开始变量时获得的内容 – kc1994