2017-08-16 150 views
1

我试图在不使用框架的分页库的情况下对Codeigniter 3应用进行分页。更确切地说,我想分页约100行。为此:Codeigniter 3:分页只显示前10条记录

在家用控制器我有:

public function index() { 
    $this->load->model('Customer'); 
    $this->load->library('pagination'); 
    $config = [ 
     'base_url' => base_url("index.php"), 
     'per_page' => 10, 
     'total_rows' => $this->Customer->get_num_rows(), 
     'uri_segment' => 3, 
     'first_tag_open' => '<li>', 
     'first_tag_close' => '</li>', 
     'last_tag_open' => '<li>', 
     'last_tag_close' => '</li>', 
     'full_tag_open' => '<ul class="pagination">', 
     'full_tag_close' => '</ul>', 
     'next_tag_open' => '<li>', 
     'next_tag_close' => '</li>', 
     'prev_tag_open' => '<li>', 
     'prev_tag_close' => '</li>', 
     'num_tag_open' => '<li>', 
     'num_tag_close' => '</li>', 
     'cur_tag_open' => '<li class="active"><a>', 
     'cur_tag_close' => '</a></li>', 
    ]; 
    $this->pagination->initialize($config); 
    $customers = $this->Customer->getCustomers($config['per_page'], $this->uri->segment($config['uri_segment'])); 
    $this->load->view('home', ['records'=>$customers]); 
} 

在模型文件中我有:

class Customer extends CI_Model { 
public function __construct() { 
    $this->load->database(); 
} 

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

Finaly,认为:

<div class="pagination-container text-center"> 
    <?php echo $this->pagination->create_links(); ?> 
</div> 

分页显示并且格式正确;所以都是第10条enter image description here

但如果我点击第二页,或任何其他页面上,我得到相同的前10个记录如可以在下面的图片中可以看出:

enter image description here

2网页的网址是:

http://localhost/cicrud/index.php?page=2 

我做错了什么,我不明白是什么。任何帮助?谢谢!

+0

如果你使用的控制器是cicrud,那么你的base_url不应该是/。此外,uri_segment大概是2,而不是5. –

+0

在公共函数索引($ offset = 0)中传递偏移量作为默认值 –

+0

也在模型函数getCustomers($ limit = null,$ offset = null){} –

回答

0

如果使用GET方法,一定要加​​代替$this->uri->segment($config['uri_segment']);.

+0

我的查询字符串段,如下面所示的凸轮是'query_string_segment'=>'page','。 –

+0

它没有工作吗?你能更新代码吗? – Alexut

1

添加这些配置线太...

$config['page_query_string'] = TRUE; 
$config['query_string_segment'] = 'page'; 
$config['display_pages'] = TRUE; 
$config['use_page_numbers'] = TRUE; 
+0

,我不再得到“找不到对象!”来自浏览器的消息。感谢帮助。但点击分页项目不会有相应的记录。我仍然只看到_只有前10个_ :( –

+0

)您需要创建一个模型来从给定的起始位置查询表 (select * from table limit 0,10) - if page = 1 or empty (select * from table限制10,10) - 如果页面= 2或空 示例: ** getCustomers($ config ['per_page'],$ page_num * $ config ['per_page'] - $ config ['per_page'])** –

0

因此,该指数()函数看起来像这样:

public function index() { 
    $this->load->model('Customer'); 
    $this->load->library('pagination'); 
    $config = [ 
     'base_url' => base_url("index.php"), 
     'page_query_string' => TRUE, 
     'query_string_segment' => 'page', 
     'display_pages' => TRUE, 
     'use_page_numbers' => TRUE, 
     'per_page' => 10, 
     'total_rows' => $this->Customer->get_num_rows(), 
     'uri_segment' => 3, 
     'first_tag_open' => '<li>', 
     'first_tag_close' => '</li>', 
     'last_tag_open' => '<li>', 
     'last_tag_close' => '</li>', 
     'full_tag_open' => '<ul class="pagination">', 
     'full_tag_close' => '</ul>', 
     'next_tag_open' => '<li>', 
     'next_tag_close' => '</li>', 
     'prev_tag_open' => '<li>', 
     'prev_tag_close' => '</li>', 
     'num_tag_open' => '<li>', 
     'num_tag_close' => '</li>', 
     'cur_tag_open' => '<li class="active"><a>', 
     'cur_tag_close' => '</a></li>' 
    ]; 
    $this->pagination->initialize($config); 
    $customers = $this->Customer->getCustomers($config['per_page'], $this->input->get('page')); 
    $this->load->view('home', ['records'=>$customers]); 
} 

希望我的回答将是对别人有用。非常感谢所有帮助。

0

试图改变

'uri_segment' => 3, 

'uri_segment' => 1, 
+0

_uri_segment_如何工作? –

0

为了让您更好地运作可读我修改几个变量名称,并创建如下分页:

/* 
     $perpage - is nothing but limit value, no of records to display 
     $page - is nothing but offset, thats page number 
*/ 
public function getCustomers($perpage=10, $page=0) { 
     $page = $page-1; 
     if ($page<0) { 
      $page = 0; 
     } 
     /* decides from where to start*/ 
     $from = $page*$perpage; 
     $this->db->limit($perpage, $from); 
     $query = $this->db->get('customers'); 
     return $query->result(); 
} 
+0

它没有工作。 ! –