Codeigniter 3.0 相关文档 part two

时间:2022-08-18 15:15:25

分页

首先,配置

$this->load->library('pagination');
$config = array();
// $this->config->load('pagination');
$config["base_url"] = site_url('lift/index');
$config["total_rows"] = $this->Lift_model->record_count();
$config["per_page"] = 15;
$config["uri_segment"] = 3;
$config['use_page_numbers'] = TRUE; $this->pagination->initialize($config);
$page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 1; $data["lm_lift"] = $this->Lift_model->get_lifts($config["per_page"], $page); $data["links"] = $this->pagination->create_links(); $this->load->view('lift/index',$data);

前端输出

<?php echo $links; ?>

样式美化,这里以bootstrap pagination为例子,在config目录下添加pagination.php的配置

<?php if(!defined('BASEPATH')) exit('Direct Access Not Allowed');

/* This Application Must Be Used With BootStrap 3 *  */
$config['full_tag_open'] = "<ul class='pagination'>";
$config['full_tag_close'] ="</ul>";
$config['num_tag_open'] = '<li>';
$config['num_tag_close'] = '</li>';
$config['cur_tag_open'] = "<li class='disabled'><li class='active'><a href='#'>";
$config['cur_tag_close'] = "<span class='sr-only'></span></a></li>";
$config['next_tag_open'] = "<li>";
$config['next_tagl_close'] = "</li>";
$config['prev_tag_open'] = "<li>";
$config['prev_tagl_close'] = "</li>";
$config['first_tag_open'] = "<li>";
$config['first_tagl_close'] = "</li>";
$config['last_tag_open'] = "<li>";
$config['last_tagl_close'] = "</li>"; // end of file Pagination.php
// Location config/pagination.php
// By @ mzhong

分页保持查询条件

http不能保持状态,可以通过session,uri来保持。如果要用uri来保持状态的话,就需要把查询表单修改为get提交方式。

<?php echo form_open('c/search',array('method'=>'get')); ?>

需要用的配置项:

$config['suffix'] = '';

给路径添加一个自定义后缀,后缀位于偏移段的后面。

$config["base_url"] = site_url('c/search');
if (count($get) > 0) {
$config['suffix'] = '?' . http_build_query($get, '', "&");
}
$config['first_url'] = site_url('c/search/1?') . http_build_query($get, '', "&query=true"); //修复第一页bug.是Pagination.php自作聪明地将第一页认为总是不必加分页号.

防止静态文件被跳转

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f #tells mod_rewrite ignore the url if it's an actual file, and
RewriteCond %{REQUEST_FILENAME} !-d #tells it to ignore it if it's a directory.
RewriteCond $1 !^(index\.php|contact\.php|images|css|js|video|robots\.txt)
RewriteRule ^(.*)$ index.php/$1 [L]