2012-09-03 43 views
0

我編輯了這個後,弄清楚了幾件事情,但是這是一個好方法,如果我想我的索引鏈接?沒有函數頁面,如果base_url是test/index,它將無法正常工作,但test/test將起作用。Codeigniter分頁功能索引

控制器

類試驗延伸是CI_Controller {

public function __construct() 
{ 
    parent::__construct(); 
    $this->load->model('Test_model'); 
    $this->load->library('pagination'); 
} 

public function index() 
{ 
    $page['title'] = ''; 
    $page['file'] = 'test/index'; 

    $config['base_url'] = base_url().'test/page'; 
    $config['total_rows'] = $this->Test_model->record_count(); 
    $config['per_page'] = 2; 
    $config['num_links'] = 5; 

    $offset = $this->uri->segment(3,0); 

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

    $page['data']['items'] = $this->Test_model->getItems($config['per_page'], $offset); 
    $page['data']['pagination'] = $this->pagination->create_links(); 
    $this->load->view('template', $page); 
} 

public function page() 
{ 
    $page['title'] = ''; 
    $page['file'] = 'test/index'; 

    $config['base_url'] = base_url().'test/page'; 
    $config['total_rows'] = $this->Test_model->record_count(); 
    $config['per_page'] = 2; 
    $config['num_links'] = 5; 

    $offset = $this->uri->segment(3,0); 

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

    $page['data']['items'] = $this->Test_model->getItems($config['per_page'], $offset); 
    $page['data']['pagination'] = $this->pagination->create_links(); 
    $this->load->view('template', $page); 
} 

}

模型

public function record_count() 
{ 
    return $this->db->count_all('item'); 
} 

public function getItems($limit, $offset) 
{ 
    $query = $this->db->get('item', $limit, $offset); 
    $result = $query->result(); 
    return $result; 
} 

視圖

<h2><?=$pagination; ?></h2> 
<table> 
<?php foreach($items as $item) { ?> 

<tr><td><?=$item->name?></td></tr> 

<?php } ?> 
+0

你忘了'$ config ['uri_segment'] = 3;'。 –

+0

我在第一次嘗試,然後看到它更容易傳遞$模型的偏移量。如果我將'函數索引'改爲'函數頁'並將base_url改爲'測試/頁面',它也可以工作。只是由於某種原因,它不會在'函數索引' – dynamo

+0

傳遞'$ uri_segment'來索引和檢查輸出。 –

回答

2

試試這個:

function __construct() 
    { 

     parent::__construct(); 
     $this->load->helper('array'); 
     $this->load->helper('url'); 
     $this->load->library('pagination'); 
     $this->load->model('Test_model','',TRUE); 
    } 

function index($uri_segment="3") 
{ 
     $config['base_url'] = base_url('test/index'); 
     $config['total_rows'] = $this->Test_model->record_count(); 
     $config['per_page'] = 5; 
     $config['uri_segment'] = 3; 
     $this->pagination->initialize($config); 

$page['data']['products'] = $this->Test_model->getItems($config['per_page'],$this->uri->segment(3)); 

    $page['data']['pagination']= $this->pagination->create_links(); 
    $page['title'] = ''; 
    $page['file'] = 'test/index'; 

    $this->load->view('template', $page); 
} 

如果生成的清單顯示上點擊下頁隨機頁面,它應該採取下一個頁碼而不是下一個id(+5)。在這種情況下,請在初始化分頁配置之前添加

$config['use_page_numbers'] = TRUE; 

1

在控制器,你必須更新此

public function index() 
    { 

     $this->load->library('pagination'); 

     $config['base_url'] = base_url().'test/index'; // use test/test and it works 
     $config['total_rows'] = $this->Test_model->record_count(); 
     $config['per_page'] = 5; 
     $config['num_links'] = 10; 
     $config['uri_segment'] = 3; 

     $offset = $this->uri->segment(3,0); 

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

     $page['data']['products'] = $this->Test_model->getItems($config['per_page'], $offset); 
     $page['data']['pagination'] = $this->pagination->create_links(); 

     $page['title'] = ''; 
     $page['file'] = 'test/index'; 

     $this->load->view('template', $page); 
    } 

這裏是很有幫助的鏈接,你 Codeigniter pagination