Generic paging class (tested with Codeigniter)
Page_list.php
Copy codeThe Code is as follows:
<? Php if (! Defined ('basepath') die ('no access ');
/**
* Paging class
*/
Class Page_list {
/**
* Total data
* @ Var int
*/
Private $ total;
/**
* Display data on each page
* @ Var int
*/
Private $ size;
/**
* Current page number
* @ Var int
*/
Private $ page;
/**
* About the number of pages in the page list
* @ Var int
*/
Private $ len;
/**
* Total number of pages
* @ Var int
*/
Private $ page_total;
/**
* Page number list
* @ Var array
*/
Private $ page_list;
/**
* Base address
* @ Var string
*/
Private $ base_url;
/**
* Replacement flag
* @ Var string
*/
Private $ place;
/**
* Paging Style
* @ Var string
*/
Private $ style;
/**
* Constructor
*
* @ Param array $ config configure an array
*/
Public function _ construct ($ config = array ()){
// Initialize the default value
$ This-> total = 0;
$ This-> size = 20;
$ This-> page = 1;
$ This-> len = 4;
$ This-> page_total = 1;
$ This-> page_list = array ();
$ This-> base_url = '? Page =-page -';
$ This-> place = '-page -';
$ This-> style = $ this-> get_default_style ();
$ This-> initialize ($ config );
}
/**
* Initialize pagination
*
* @ Param array $ config configure an array
*/
Public function initialize ($ config = array ()){
// Obtain the configuration value
If (is_array ($ config )){
If (array_key_exists ('Total', $ config) $ this-> total = @ intval ($ config ['Total']);
If (array_key_exists ('SIZE', $ config) $ this-> size = @ intval ($ config ['SIZE']);
If (array_key_exists ('page', $ config) $ this-> page = @ intval ($ config ['page']);
If (array_key_exists ('len', $ config) $ this-> len = @ intval ($ config ['len']);
If (array_key_exists ('base _ url', $ config) $ this-> base_url = @ strval ($ config ['base _ url']);
If (array_key_exists ('place', $ config) $ this-> place = @ strval ($ config ['place']);
If (array_key_exists ('style', $ config) $ this-> style = @ strval ($ config ['style']);
}
// Corrected value
If ($ this-> total <0) $ this-> total = 0;
If ($ this-> size <= 0) $ this-> size = 20;
If ($ this-> page <= 0) $ this-> page = 1;
If ($ this-> len <= 0) $ this-> len = 4;
// Execute the paging algorithm
$ This-> page_total = ceil ($ this-> total/$ this-> size );
If ($ this-> page_total <= 0) $ this-> page_total = 1;
If ($ this-> page >$ this-> page_total) $ this-> page = $ this-> page_total;
If ($ this-> page-$ this-> len> = 1 ){
For ($ I = $ this-> len; $ I> 0; $ I --){
$ This-> page_list [] = $ this-> page-$ I;
}
} Else {
For ($ I = 1; $ I <$ this-> page; $ I ++ ){
$ This-> page_list [] = $ I;
}
}
$ This-> page_list [] = $ this-> page;
If ($ this-> page + $ this-> len <= $ this-> page_total ){
For ($ I = 1; $ I <= $ this-> len; $ I ++ ){
$ This-> page_list [] = $ this-> page + $ I;
}
} Else {
For ($ I = $ this-> page + 1; $ I <= $ this-> page_total; $ I ++ ){
$ This-> page_list [] = $ I;
}
}
}
/**
* Default paging Style
*
* @ Return string
*/
Public function get_default_style (){
$ Style = '<style type = "text/css"> ';
$ Style. = 'div. page_list {margin: 0; padding: 0; overflow: hidden; zoom: 1 ;}';
$ Style. = 'div. page_list a {display: block; float: left; height: 20px; line-height: 21px; font-size: 13px; font-weight: normal; font-style: normal; color: #133DB6; text-decoration: none; margin: 0 3px; padding: 0 7px; overflow; zoom: 1 ;}';
$ Style. = 'div. page_list a. page_list_act {font-size: 13px; padding: 0 6px ;}';
$ Style. = 'div. page_list a: link, div. page_list a: visited {background: # FFF; border: 1px # EEE solid; text-decoration: none ;}';
$ Style. = 'div. page_list a: hover, div. page_list a: active {background: # EEE; text-decoration: none ;}';
$ Style. = 'div. page_list strong {display: block; float: left; height: 20px; line-height: 21px; font-size: 13px; font-weight: bold; font-style: normal; color: #000; margin: 0 3px; padding: 0 8px; overflow: hidden; zoom: 1 ;}';
$ Style. = '</style> ';
Return $ style;
}
/**
* Whether it is the first page
*
* @ Return bool
*/
Public function is_first_page (){
Return $ this-> page = 1;
}
/**
* Retrieve the first page number
*
* @ Return int
*/
Public function get_first_page (){
Return 1;
}
/**
* Whether it is the last page
*
* @ Return bool
*/
Public function is_last_page (){
Return $ this-> page ==$ this-> page_total;
}
/**
* Get the last page code
*
* @ Return int
*/
Public function get_last_page (){
Return $ this-> page_total;
}
/**
* Whether the previous page exists
*
* @ Return bool
*/
Public function has_prev_page (){
Return $ this-> page> 1;
}
/**
* Whether the next page exists
*
* @ Return bool
*/
Public function has_next_page (){
Return $ this-> page <$ this-> page_total;
}
/**
* Retrieve the previous page number
*
* @ Return int
*/
Public function get_prev_page (){
Return $ this-> has_prev_page ()? $ This-> page-1: $ this-> page;
}
/**
* Retrieve the next page number
*
* @ Return int
*/
Public function get_next_page (){
Return $ this-> has_next_page ()? $ This-> page + 1: $ this-> page;
}
/**
* Retrieve the current page number
*
* @ Return int
*/
Public function get_curr_page (){
Return $ this-> page;
}
/**
* Get the total data count
*
* @ Return int
*/
Public function get_total (){
Return $ this-> total;
}
/**
* Get the number of data displayed on each page
*
* @ Return int
*/
Public function get_size (){
Return $ this-> size;
}
/**
* Retrieve the total number of pages
*
* @ Return int
*/
Public function get_total_page (){
Return $ this-> page_total;
}
/**
* Build and return paging code
*
* @ Param string $ base_url base address
* @ Param string $ place replacement flag
* @ Param string $ style paging style
* @ Return string paging HTML code
*/
Public function display ($ base_url = '', $ place ='', $ style = ''){
If ($ base_url = '') $ base_url = $ this-> base_url;
If ($ place = '') $ place = $ this-> place;
If ($ style = '') $ style = $ this-> style;
$ Str = $ style. '<div class = "page_list"> ';
If (! $ This-> is_first_page ()){
$ Str. = '<a class = "page_list_act" href = "'. str_replace ($ place, $ this-> get_first_page (), $ base_url ). '"> homepage </a> ';
}
If ($ this-> has_prev_page ()){
$ Str. = '<a class = "page_list_act" href = "'. str_replace ($ place, $ this-> get_prev_page (), $ base_url ). '"> previous page </a> ';
}
Foreach ($ this-> page_list as $ v ){
If ($ v = $ this-> page ){
$ Str. = '<strong>'. $ v. '</strong> ';
} Else {
$ Str. = '<a href = "'. str_replace ($ place, $ v, $ base_url). '">'. $ v. '</a> ';
}
}
If ($ this-> has_next_page ()){
$ Str. = '<a class = "page_list_act" href = "'. str_replace ($ place, $ this-> get_next_page (), $ base_url ). '"> next page </a> ';
}
If (! $ This-> is_last_page ()){
$ Str. = '<a class = "page_list_act" href = "'. str_replace ($ place, $ this-> get_last_page (), $ base_url ). '"> last page </a> ';
}
$ Str. = '</div> ';
Return $ str;
}
}
?>
/Application/view/pagelist. php
Copy codeThe Code is as follows:
<? Php if (! Defined ('basepath') die ('no access ');
Class Pagelist extends CI_Controller {
Public function page (){
$ This-> load-> helper ('url ');
$ Page = $ this-> input-> get ('page ');
$ Page = @ intval ($ page );
If ($ page <= 0) $ page = 1;
$ This-> load-> library ('page _ list', array ('Total' => 10000, 'SIZE' => 16, 'page' => $ page ));
$ Pl = $ this-> page_list-> display (site_url ('pagelist/page/-page -'));
$ This-> load-> view ('pagelist', array ('pl '=> $ pl ));
}
}
?>
/Application/view/pagelist. php
Copy codeThe Code is as follows:
<! DOCTYPE html>
<Html>
<Head>
<Meta http-equiv = "content-type" content = "text/html; charset = UTF-8"/>
<Title> paging test </title>
</Head>
<Body>
<? Php echo $ pl;?>
</Body>
</Html>