Example _php instance of the CodeIgniter test passed in the in-class

Source: Internet
Author: User
Tags codeigniter

Universal Paging Class (with CodeIgniter test)

page_list.php

Copy Code code as follows:

<?php if (! defined (' BasePath ')) die (' No Access ');

/**
* Page-type
*/
Class Page_list {

   /**
     * Total Data
     * @var int
      */
    private $total;
   /**
     * Display data per page
     * @var int
      */
    private $size;
   /**
     * Current page
     * @var int
      */
    private $page;
   /**
     * Page List number of pages
     * @var int
      */
    private $len;

/**
* Total Pages
* @var int
*/
Private $page _total;
/**
* List of page numbers
* @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
*
* @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);
}

/**
* Initialize pagination
*
* @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 positive
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;
To 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 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;
}

/**
* 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;
}

/**
* Whether it's 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;
}

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

/**
* Whether there is 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 current page number
*
* @return int
*/
Public Function Get_curr_page () {
return $this->page;
}

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

/**
* Get the number of display data 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;
}

/**
* Build and return the paging code
*
* @param string $base _url base Address
* @param string $place Replace flag
* @param string $style paging style
* @return String page-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). ' > Home </a> ';
}
if ($this->has_prev_page ()) {
$str. = ' <a class= ' page_list_act ' href= '. Str_replace ($place, $this->get_prev_page (), $base _url). ' > Prev </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 </a> ';
}
$str. = ' </div> ';
return $str;
}

}
?>

/application/view/pagelist.php

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

}
?>

/application/view/pagelist.php

Copy Code code as follows:

<! DOCTYPE html>
<meta http-equiv= "Content-type" content= "Text/html;charset=utf-8"/>
<title> pagination Test </title>
<body>
<?php echo $pl;?>
</body>

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.