CodeIgniter 分页类 pagination 源代码分析
来源:互联网 发布:vb可视化数据管理器 编辑:程序博客网 时间:2024/05/16 11:14
一、构造函数:构造函数的参数是初始化参数个数,先判断参数如果>0,则调用$this->initialize($params);初始化参数,然后判断是否给每一个链接添加CSS类
public function __construct($params = array()){if (count($params) > 0){$this->initialize($params);}if ($this->anchor_class != ''){$this->anchor_class = 'class="'.$this->anchor_class.'" ';}log_message('debug', "Pagination Class Initialized");}
二、initialize($params = array()):该函数首先判断初始化参数个数是否大于0,如果大于0,则利用foreach初始化参数
function initialize($params = array()){if (count($params) > 0){foreach ($params as $key => $val){if (isset($this->$key)){$this->$key = $val;}}}}三、生成分页链接的函数:create_link():
该函数
①:首先判断分页的数据总行数$this->total_rows和每页显示的数量$this->per_page如果有一个为0,则中止
if ($this->total_rows == 0 OR $this->per_page == 0){return '';}②:计算总页数:
$num_pages = ceil($this->total_rows / $this->per_page);
③:如果只有一页,则不需要进行分页了
if ($num_pages == 1){return '';}④:根据是否启用了use_page_numbers设置开始页码
if ($this->use_page_numbers){$base_page = 1;}else{$base_page = 0;}
⑤:确定当前页码:首先判断是否启用了查询字符串重写,如果是则判断页码是否等于开始页码,如果不等于开始页码,则用输入类的get方法获取$this->query_string_segment为当前页码。如果没有启用查询字符串重写,则使用uri方式
$CI =& get_instance();if ($CI->config->item('enable_query_strings') === TRUE OR $this->page_query_string === TRUE){if ($CI->input->get($this->query_string_segment) != $base_page){$this->cur_page = $CI->input->get($this->query_string_segment);// Prep the current page - no funny business!$this->cur_page = (int) $this->cur_page;}}else{if ($CI->uri->segment($this->uri_segment) != $base_page){$this->cur_page = $CI->uri->segment($this->uri_segment);// Prep the current page - no funny business!$this->cur_page = (int) $this->cur_page;}}⑥:设置当前页码为1,如果启用了use_page_numbers且$this->cur_page == 0,则当前页码设置为开始页码
if ($this->use_page_numbers AND $this->cur_page == 0){$this->cur_page = $base_page;}$this->num_links = (int)$this->num_links;if ($this->num_links < 1){show_error('Your number of links must be a positive number.');}if ( ! is_numeric($this->cur_page)){$this->cur_page = $base_page;}⑦:判断当前页码是否超出范围:
if ($this->use_page_numbers){if ($this->cur_page > $num_pages){$this->cur_page = $num_pages;}}else{if ($this->cur_page > $this->total_rows){$this->cur_page = ($num_pages - 1) * $this->per_page;}}
⑧:计算分页链接显示的开始页码和最后页码,例如,当前页码为4,$num_links设置为2,则显示23456页码
$start = (($this->cur_page - $this->num_links) > 0) ? $this->cur_page - ($this->num_links - 1) : 1;$end = (($this->cur_page + $this->num_links) < $num_pages) ? $this->cur_page + $this->num_links : $num_pages;⑨:设置$base_url
if ($CI->config->item('enable_query_strings') === TRUE OR $this->page_query_string === TRUE){$this->base_url = rtrim($this->base_url).'&'.$this->query_string_segment.'=';}else{$this->base_url = rtrim($this->base_url, '/') .'/';}⑩:初始化输出变量$output为空,且设置$first_link
$output = '';// Render the "First" linkif ($this->first_link !== FALSE AND $this->cur_page > ($this->num_links + 1)){$first_url = ($this->first_url == '') ? $this->base_url : $this->first_url;$output .= $this->first_tag_open.'<a '.$this->anchor_class.'href="'.$first_url.'">'.$this->first_link.'</a>'.$this->first_tag_close;}⑪:设置$prev_link:需要判断是否启用了$use_page_numbers,还要判断$i是否等于0
if ($this->prev_link !== FALSE AND $this->cur_page != 1){if ($this->use_page_numbers){$i = $uri_page_number - 1;}else{$i = $uri_page_number - $this->per_page;}if ($i == 0 && $this->first_url != ''){$output .= $this->prev_tag_open.'<a '.$this->anchor_class.'href="'.$this->first_url.'">'.$this->prev_link.'</a>'.$this->prev_tag_close;}else{$i = ($i == 0) ? '' : $this->prefix.$i.$this->suffix;$output .= $this->prev_tag_open.'<a '.$this->anchor_class.'href="'.$this->base_url.$i.'">'.$this->prev_link.'</a>'.$this->prev_tag_close;}}⑫:设置显示的数字页码:首先判断$this->display_pages是否不为假,则显示数字页码
if ($this->display_pages !== FALSE){// Write the digit linksfor ($loop = $start -1; $loop <= $end; $loop++){if ($this->use_page_numbers){$i = $loop;}else{$i = ($loop * $this->per_page) - $this->per_page;}if ($i >= $base_page){if ($this->cur_page == $loop){$output .= $this->cur_tag_open.$loop.$this->cur_tag_close; // Current page}else{$n = ($i == $base_page) ? '' : $i;if ($n == '' && $this->first_url != ''){$output .= $this->num_tag_open.'<a '.$this->anchor_class.'href="'.$this->first_url.'">'.$loop.'</a>'.$this->num_tag_close;}else{$n = ($n == '') ? '' : $this->prefix.$n.$this->suffix;$output .= $this->num_tag_open.'<a '.$this->anchor_class.'href="'.$this->base_url.$n.'">'.$loop.'</a>'.$this->num_tag_close;}}}}}⑬:设置$this->next_link和$this->last_link
// Render the "next" linkif ($this->next_link !== FALSE AND $this->cur_page < $num_pages){if ($this->use_page_numbers){$i = $this->cur_page + 1;}else{$i = ($this->cur_page * $this->per_page);}$output .= $this->next_tag_open.'<a '.$this->anchor_class.'href="'.$this->base_url.$this->prefix.$i.$this->suffix.'">'.$this->next_link.'</a>'.$this->next_tag_close;}// Render the "Last" linkif ($this->last_link !== FALSE AND ($this->cur_page + $this->num_links) < $num_pages){if ($this->use_page_numbers){$i = $num_pages;}else{$i = (($num_pages * $this->per_page) - $this->per_page);}$output .= $this->last_tag_open.'<a '.$this->anchor_class.'href="'.$this->base_url.$this->prefix.$i.$this->suffix.'">'.$this->last_link.'</a>'.$this->last_tag_close;}⑭:最后输出结果
$output = preg_replace("#([^:])//+#", "\\1/", $output);// Add the wrapper HTML if exists$output = $this->full_tag_open.$output.$this->full_tag_close;return $output;
- CodeIgniter 分页类 pagination 源代码分析
- CodeIgniter Session类 源代码 分析
- CodeIgniter 图像处理类 源代码分析
- codeigniter源代码分析 -安全类 Security.php
- codeigniter源代码分析 - 钩子类 Hooks.php
- codeigniter源代码分析 - 语言包类 Lang.php
- codeigniter源代码分析 - 编码类 Utf8.php
- codeigniter源代码分析 - 控制器类 Controller.php
- codeigniter源代码分析 - 模型类 Model.php
- codeigniter源代码分析 - 性能分析类 Benchmark.php
- CodeIgniter 分页类使用
- codeigniter源代码分析之配置类Config.php
- codeigniter源代码分析之路由处理类 Router.php
- codeigniter源代码分析之URI处理类 URI.php
- codeigniter源代码分析之输入类 Input.php
- codeigniter源代码分析之输出类 Output.php
- codeigniter源代码分析之文件加载类 Loader.php
- codeigniter源代码分析 - 错误处理类 Exception.php
- 软件测试面试题及解析(八)
- iOS高效开发必备的10款Objective-C类库
- 第六节(link的运用)(CSS样式表)
- Web前端书籍推荐
- 移动开发:Android_Sdk卡住不更新的解决方法
- CodeIgniter 分页类 pagination 源代码分析
- CSS基础---选择符
- 关于CArchive是无法输出到bool型变量的
- [转载]DataGridView实现添加合计行并始终显示在底部
- Delphi文件操作所涉及的一些函数
- Android文件目录选择器(可自行扩展)
- 指针函数与函数指针的区别
- Actionscript中 事件和监听器的默认参数的巧妙设计
- javascript入门技能