CI框架簡單分頁類用法示例
本文實例講述了CI框架簡單分頁類用法。分享給大家供大家參考,具體如下:
/** * * 關于 頁碼有效性的判斷需要加在 控制器中判斷,即當頁碼數(shù)<1或者>總頁數(shù) * */ class Custom_pagination { var $page_url = ''; //分頁目標URL var $page_size = 10; //每一頁行數(shù) var $page_num = 1;//頁碼 var $rows_num= '';//數(shù)據(jù)總行數(shù) var $links_num= 3;//選中鏈接前后的鏈接數(shù),必須大于等于1 var $anchor_class= '';//鏈接樣式類 var $current_class= '';//當前頁樣式類 var $full_tag_open= '';//分頁開始標簽 var $full_tag_close= '';//分頁結束標簽 var $info_tag_open= ''; var $info_tag_close= ' '; var $first_tag_open= ''; var $first_tag_close= ' '; var $last_tag_open= ' '; var $last_tag_close= ''; var $cur_tag_open= ' <strong>'; var $cur_tag_close= '</strong>'; var $next_tag_open= ' '; var $next_tag_close= ' '; var $prev_tag_open= ' '; var $prev_tag_close= ''; var $num_tag_open= ' '; var $num_tag_close= ''; public function __construct($params = array()) { if (count($params) > 0) { $this->init($params); } } function init($params = array()) //初始化數(shù)據(jù) { if (count($params) > 0) { foreach ($params as $key => $val) { if (isset($this->$key)) { $this->$key = $val; } } } } function create_links() { /////////////////////////////////////////////////////// //準備數(shù)據(jù) /////////////////////////////////////////////////////// $page_url = $this->page_url; $rows_num = $this->rows_num; $page_size = $this->page_size; $links_num = $this->links_num; if ($rows_num == 0 OR $page_size == 0) { return ''; } $pages = intval($rows_num/$page_size); if ($rows_num % $page_size) { //有余數(shù)pages+1 $pages++; }; $page_num = $this->page_num < 1 ? '1' : $this->page_num; $anchor_class = ''; if($this->anchor_class !== '') { $anchor_class = 'class="'.$this->anchor_class.'" '; } $current_class = ''; if($this->current_class !== '') { $current_class = 'class="'.$this->current_class.'" '; } if($pages == 1) { return ''; } if($links_num < 0) { return '- -!links_num必須大于等于0'; } //////////////////////////////////////////////////////// //創(chuàng)建鏈接開始 //////////////////////////////////////////////////////// $output = $this->full_tag_open; $output .= $this->info_tag_open.'共'.$rows_num.'條數(shù)據(jù) 第 '.$page_num.'/'.$pages.' 頁'.$this->info_tag_close; //首頁 if($page_num > 1) { $output .= $this->first_tag_open.'<a '.$anchor_class.' href="'.site_url($page_url).'" rel="external nofollow" >首頁</a>'.$this->first_tag_close; } //上一頁 if($page_num > 1) { $n = $page_num - 1; $output .= $this->prev_tag_open.'<a '.$anchor_class.' href="'.site_url($page_url.'/'.$n).'" rel="external nofollow" rel="external nofollow" >上一頁</a>'.$this->prev_tag_close; } //pages for($i=1;$i<=$pages;$i++) { $pl = $page_num - $links_num < 0 ? 0 : $page_num - $links_num; $pr = $page_num + $links_num > $pages ? $pages : $page_num + $links_num; //判斷鏈接個數(shù)是否太少,舉例,假設links_num = 2,則鏈接個數(shù)不可少于 5 個,主要是 當page_num 等于 1, 2 和 n,n-1的時候 if($pr < 2 * $links_num + 1) { $pr = 2 * $links_num + 1; } if($pl > $pages-2 * $links_num) { $pl = $pages - 2 * $links_num; } if($i == $page_num) { //current page $output .= $this->cur_tag_open.'<span '.$current_class.' >'.$i.'</span>'.$this->cur_tag_close; }else if($i >= $pl && $i <= $pr) { $output .= $this->num_tag_open.'<a '.$anchor_class.' href="'.site_url($page_url.'/'.$i).'" rel="external nofollow" >'.$i.'</a>'.$this->num_tag_close; } } //下一頁 if($page_num < $pages) { $n = $page_num + 1; $output .= $this->next_tag_open.'<a '.$anchor_class.' href="'.site_url($page_url.'/'.$n).'" rel="external nofollow" rel="external nofollow" >下一頁</a>'.$this->next_tag_close; } //末頁 if($page_num < $pages) { $output .= $this->last_tag_open.'<a '.$anchor_class.' href="'.site_url($page_url.'/'.$pages).'" rel="external nofollow" >末頁</a>'.$this->last_tag_close; } $output.=$this->full_tag_close; return $output; } }
控制器里調用
$config['page_url'] = 'about/science'; $config['page_size'] = $pagesize; $config['rows_num'] = $num_rows; $config['page_num'] = $page; $this->load->library('Custom_pagination'); $this->custom_pagination->init($config); echo $this->custom_pagination->create_links();
<?php class page{ public $page; //當前頁 public $pagenum; // 頁數(shù) public $pagesize; // 每頁顯示條數(shù) public function __construct($count, $pagesize){ $this->pagenum = ceil($count/$pagesize); $this->pagesize = $pagesize; $this->page =(isset($_GET['p'])&&$_GET['p']>0) ? intval($_GET['p']) : 1; } /** * 獲得 url 后面GET傳遞的參數(shù) */ public function getUrl(){ $url = 'index.php?'.http_build_query($_GET); $url = preg_replace('/[?,&]p=(\w)+/','',$url); $url .= (strpos($url,"?") === false) ? '?' : '&'; return $url; } /** * 獲得分頁HTML */ public function getPage(){ $url = $this->getUrl(); $start = $this->page-5; $start=$start>0 ? $start : 1; $end = $start+9; $end = $end<$this->pagenum ? $end : $this->pagenum; $pagestr = ''; if($this->page>5){ $pagestr = "<a href=".$url." rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" p=1".">首頁</a> "; } if($this->page!=1){ $pagestr.= "<a href=".$url." rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" p=".($this->page-1).">上一頁</a>"; } for($i=$start;$i<=$end;$i++){ $pagestr.= "<a href=".$url." rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" p=".$i.">".$i."</a> "; } if($this->page!=$this->pagenum){ $pagestr.="<a href=".$url." rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" p=".($this->page+1).">下一頁</a>"; } if($this->page+5<$this->pagenum){ $pagestr.="<a href=".$url." rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" p=".$this->pagenum.">尾頁</a> "; } return $pagestr; } } // 測試代碼 $page = new page(100,10); $str=$page->getPage(); echo $str; ?>
更多關于CodeIgniter相關內容感興趣的讀者可查看本站專題:《codeigniter入門教程》、《CI(CodeIgniter)框架進階教程》、《php優(yōu)秀開發(fā)框架總結》、《ThinkPHP入門教程》、《ThinkPHP常用方法總結》、《Zend FrameWork框架入門教程》、《php面向對象程序設計入門教程》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總》
希望本文所述對大家基于CodeIgniter框架的PHP程序設計有所幫助。
相關文章
php截取字符串之截取utf8或gbk編碼的中英文字符串示例
php中自帶strlen是返回的字節(jié)數(shù),對于utf8編碼的中文返回時3個,不滿足需求,下面給大家提供一個方法來完成這樣的功能2014-03-03針對多用戶實現(xiàn)頭像上傳功能PHP代碼 適用于登陸頁面制作
這篇文章主要為大家詳細介紹了針對多用戶實現(xiàn)頭像上傳功能PHP代碼,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-08-08Laravel如何實現(xiàn)適合Api的異常處理響應格式
這篇文章主要給大家介紹了關于Laravel如何實現(xiàn)適合Api的異常處理響應格式的相關資料,文中通過示例代碼介紹的非常詳細,對大家學習或者使用Laravel具有一定的參考學習價值,需要的朋友們下面來一起學習學習吧2020-06-06PHP實現(xiàn)微信支付(jsapi支付)流程步驟詳解
這篇文章主要介紹了PHP實現(xiàn)微信支付(jsapi支付)流程步驟詳解,需要的朋友可以參考下2018-03-03yii2.0實現(xiàn)pathinfo的形式訪問的配置方法
這篇文章主要介紹了yii2.0實現(xiàn)pathinfo的形式訪問的配置方法的相關資料,需要的朋友可以參考下2016-04-04