CodeIgniter example of a page class passed in Test _php tutorial

Source: Internet
Author: User
Tags codeigniter
Generic paging Class (tested with CodeIgniter)

page_list.php
Copy the Code code as follows:

/**
* Pagination Class
*/
Class Page_list {

/**
* Total Data
* @var int
*/
Private $total;
/**
* Show data per page
* @var int
*/
Private $size;
/**
* Current Page
* @var int
*/
Private $page;
/**
* Page number list left and right pages
* @var int
*/
Private $len;

/**
* Total Pages
* @var int
*/
Private $page _total;
/**
* Page List
* @var Array
*/
Private $page _list;

/**
* Base Address
* @var String
*/
Private $base _url;
/**
* Replacement Flag
* @var String
*/
Private $place;
/**
* Pagination Style
* @var String
*/
Private $style;


/**
* Constructor function
*
* @param array $config configuration
*/
Public function __construct ($config = Array ()) {
Initialize default values
$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);
}

/**
* Initializing Paging
*
* @param array $config configuration
*/
Public Function Initialize ($config = Array ()) {
Get 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 ']);
}
Fixed 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;
Perform a 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 page-out 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;
}

/**
* Get first page number
*
* @return int
*/
Public Function Get_first_page () {
return 1;
}

/**
* is the last page
*
* @return BOOL
*/
Public Function Is_last_page () {
return $this->page = = $this->page_total;
}

/**
* Get last page number
*
* @return int
*/
Public Function Get_last_page () {
return $this->page_total;
}

/**
* Is there a previous page
*
* @return BOOL
*/
Public Function Has_prev_page () {
Return $this->page > 1;
}

/**
* Is there a next page
*
* @return BOOL
*/
Public Function Has_next_page () {
return $this->page < $this->page_total;
}

/**
* Get Previous page number
*
* @return int
*/
Public Function Get_prev_page () {
return $this->has_prev_page ()? $this->page-1: $this->page;
}

/**
* Get Next page number
*
* @return int
*/
Public Function Get_next_page () {
return $this->has_next_page ()? $this->page + 1: $this->page;
}

/**
* Get the current page number
*
* @return int
*/
Public Function Get_curr_page () {
return $this->page;
}

/**
* Get the total number of data
*
* @return int
*/
Public Function Get_total () {
return $this->total;
}

/**
* Gets the number of data displayed per page
*
* @return int
*/
Public Function get_size () {
return $this->size;
}

/**
* Get Total pages
*
* @return int
*/
Public Function Get_total_page () {
return $this->page_total;
}

/**
* Constructs and returns the paging code
*
* @param string $base _url base Address
* @param string $place Replace flag
* @param string $styl e pagination Style
* @return string pagination 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). ' " > Home ';
}
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 ';
}
$str. = ";
return $str;
}

}
?>

/application/view/pagelist.php
Copy the Code code as follows:

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/-page-'));
$this->load->view (' pagelist ', array (' pl ' = = $pl));
}

}
?>

/application/view/pagelist.php
Copy the Code code as follows:




Paging test





http://www.bkjia.com/PHPjc/755840.html www.bkjia.com true http://www.bkjia.com/PHPjc/755840.html techarticle the generic paging class (in CodeIgniter test) page_list.php copy code code as follows:? PHP if (! defined (' BasePath ')) die (' No Access ');/** * Pagination class */class Pa ge_list {/** * Total ...

  • Related Article

    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.