2016-12-05 114 views
1

我正在使用codeigniter分頁。但是在使用codeingiter分頁時我遇到了一個問題。這是我面臨的問題。Codeigniter分頁不起作用

例如:如果頁面的記錄超過10條,並且在此處每頁顯示5條記錄。如果我點擊第二頁,它會正確顯示數據,但如果我需要返回第一頁,則它不起作用。這裏是我的代碼:

控制器:

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

    $config = array(); 
    $config["base_url"] = base_url('testimonial/index'); 
    $config['total_rows'] = $this->testimonial_model->record_count();//here we will count all the data from the table 
    $config['per_page'] = 2;//number of data to be shown on single page 
    $config["uri_segment"] = 2; 
    $this->pagination->initialize($config); 
    $page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 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; 
    } 
} 

function record_count() 
{ 
    return $this->db->count_all("testimonials"); 
} 
} 
+0

請去這個網址: http://www.technicalkeeda.com/php-codeigniter/pagination-using-php-codeigniter –

+0

@HardikPaghdar試過這個不能夠取數據只獲得分頁 – user7047368

+0

信息缺失,以完全理解,...但:每頁應顯示多少項目? ...從你的控制器代碼看起來像你每頁顯示$ config ['per_page']項目。總是在您的代碼2!你應該這樣做: $ config ['per_page'] =($ this-> uri-> segment(N))? $ this-> uri-> segment(N):2; //其中N是指示每頁顯示項目的uri段。 – MarcM

回答

0

利用這個答案,請閱讀代碼一下子明白它

public function index() 
{ 

    $config = array(); 

    //$config["base_url"] = base_url('testimonial/index'); 

     $get_vars = $this->input->get(); 
     if(is_array($get_vars)) 
     $config['suffix'] = '?'.http_build_query($get_vars,'', "&"); 
     //$config['prefix'] = '?'.http_build_query($get_vars,'', "&"); 
     $config['base_url'] = base_url().$this->router->class.'/index'; 
     $config['first_url'] = $config['base_url'] . (isset($config['suffix'])?$config['suffix']:''); 

    $config['total_rows'] = $this->testimonial_model->record_count();//here we will count all the data from the table 
    $config['per_page'] = 2;//number of data to be shown on single page 


    //this line should be according to url to fetch actual value 
    $config["uri_segment"] = ($this->uri->segment('2')?$this->uri->segment('2'):0); // 2 or 3 based on ur url 


    $this->pagination->initialize($config); 
    $page = $config["uri_segment"]; 
    $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); 
} 
+0

什麼是頁面?你能解釋我嗎?它應該是$ page = $ config [「uri_segment」]; –

+0

找不到頁面 – user7047368

+0

未找到頁面的網址是什麼?請分享。 –