Example of paging classes passed in codeigniter

Source: Internet
Author: User
This article mainly introduces the paging class examples passed in codeigniter. For more information, see

This article mainly introduces the paging class examples passed in codeigniter. For more information, see

Generic paging class (tested with Codeigniter)

Page_list.php

The Code is as follows:


/**
* 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 ='';
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 .'

';
If (! $ This-> is_first_page ()){
$ Str. = 'get _ first_page (), $ base_url). '"> homepage ';
}
If ($ this-> has_prev_page ()){
$ Str. = 'get _ prev_page (), $ base_url). '"> Previous Page ';
}
Foreach ($ this-> page_list as $ v ){
If ($ v = $ this-> page ){
$ Str. =''. $ V .'';
} Else {
$ Str. = ''. $ v .'';
}
}
If ($ this-> has_next_page ()){
$ Str. = 'get _ next_page (), $ base_url). '"> next page ';
}
If (! $ This-> is_last_page ()){
$ Str. = 'get _ last_page (), $ base_url). '"> last page ';
}
$ Str. ='

';
Return $ str;
}

}
?>

/Application/view/pagelist. php

The Code is as follows:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.