PHP Pagination Class Code

Source: Internet
Author: User
This is a simple, convenient, full-featured paging class, you can change the CSS style files according to their own needs to achieve the control of page color, the use of PHP paging class, you can save yourself a lot of time, just need to embed in the place of the page, see below How to use:

1, contains pager.css in head

    <link href= "Pager.css" type= "Text/css" rel= "stylesheet"/>

2, instantiate the class at the paging point:

<?php         include "pager.class.php";      $CurrentPage =isset ($_get[' page ')? $_get[' page ']:1;      Die ($CurrentPage);         $myPage =new Pager (1300,intval ($CurrentPage));          $PAGESTR = $myPage->getpagercontent ();      echo $pageStr;         $myPage =new Pager (90,intval ($CurrentPage));          $PAGESTR = $myPage->getpagercontent ();      echo $pageStr;        ? >

3. Read the MySQL database and the paging calculation file needs to write it yourself. For example:

$info =mysql_query ("SELECT * from member the ORDER by id desc limit $offset, $info _num"); This can be transferred to the MySQL database and the current page number.

The following is a total of three files: (1) index.php: Call paging class, display paging (2) pager.class.php:php Page class (3) pager.css:css style file

(1) index.php:


(2) pager.class.php:

<?php/* PHP Page class * @package page * @Created 2013-03-27 * @Modify 2013-03-27 * @link Http://www.60ie          . NET * Example: $myPage =new Pager (1300,intval ($CurrentPage));          $PAGESTR = $myPage->getpagercontent ();    Echo $pageStr;        */class Pager {private $pageSize = 10;        Private $pageIndex;        Private $totalNum;        Private $totalPagesCount;        Private $PAGEURL;        private static $_instance; Public function __construct ($p _totalnum, $p _pageindex, $p _pagesize = ten, $p _initnum=3, $p _initmaxnum=5) {if (! I Sset ($p _totalnum) | |            !isset ($p _pageindex)) {die ("pager initial error");            } $this->totalnum = $p _totalnum;            $this->pageindex = $p _pageindex;            $this->pagesize = $p _pagesize;            $this->initnum= $p _initnum;            $this->initmaxnum= $p _initmaxnum; $this->totalpagescount= ceil ($p _totalnum/$p _pagesize);           $this->pageurl= $this->_getpageurl ();        $this->_initpagerlegal (); /** * Gets the current URL string that removes the page part * * @return string URL String */Private Function _getpag            Eurl () {$CurrentUrl = $_server["Request_uri"];            $ARRURL = Parse_url ($CURRENTURL);            $urlQuery = $ARRURL ["Query"];                if ($urlQuery) {$urlQuery = Ereg_replace ("(^|&) page=". $this->pageindex, "", $urlQuery);                $CURRENTURL = Str_replace ($arrUrl ["Query"], $urlQuery, $CURRENTURL);                if ($urlQuery) {$CurrentUrl. = "&page";            } else $CurrentUrl. = "page";            } else {$CurrentUrl. = "? page";      } return $CURRENTURL; }/* * Set page parameter legitimacy * @return void */Private Function _initpagerlegal () {if (!is_numeric ($this->pageindex))  ||        $this->pageindex<1)  {$this->pageindex=1; }elseif ($this->pageindex > $this->totalpagescount) {$this->pageindex= $this->totalpag          Escount; }}//$this->pageurl}={$i}//{$this->currenturl}={$this->totalpages} public function            Getpagercontent () {$str = "<p class=\" pagination\ ">"; First prev if ($this->pageindex==1) {$str. = "<a href= ' javascript:void (0) ' class= ' Tips ' title= ' Home > Home </a> '. '                \ n "; $str. = "<a href= ' javascript:void (0) ' class= ' tips ' title= ' previous > Prev </a> '." \ n "."            \ n "; }else {$str. = "<a href= ' {$this->pageurl}=1 ' class= ' tips ' title= ' home ' > Home </a>". "                        \ n "; $str. = "<a href= ' {$this->pageurl}=". ($this->pageindex-1). "' class= ' tips ' title= ' previous > Prev </a> '. ' \ n "."            \ n "; }/* Except for the firstLast page paging logic *///10 page (including) the following $currnt = "";                if ($this->totalpagescount<=10) {for ($i =1; $i <= $this->totalpagescount; $i + +)                            {if ($i = = $this->pageindex) {$currnt = "class= ' current '";}    else {$currnt = ""; } $str. = "<a href= ' {$this->pageurl}={$i} ' {$currnt}> $i </a>".                \ n ";                 }}else//10 page above {if ($this->pageindex<3)//Current page less than 3 {for ($i =1; $i <=3; $i + +) {if ($i = = $th                             Is->pageindex) {$currnt = "class= ' current '";}    else {$currnt = ""; } $str. = "<a href= ' {$this->pageurl}={$i} ' {$currnt}> $i </a> "."                         \ n "; } $str. = "<span class=\" dot\ ">......</span>". "                     \ n ";                          for ($i = $this->totalpagescount-3+1; $i <= $this->totalpagescount; $i + +)//function 1 { $str. = "<a href= ' {$this->pageurl}={$i} ' > $i </a>". "                     \ n "; }}elseif ($this->pageindex<=5)//5 >= Current page >= 3 {for (                           $i =1; $i <= ($this->pageindex+1); $i + +) {if ($i = = $this->pageindex)                           {$currnt = "class= '";}    else {$currnt = ""; } $str. = "<a href= ' {$this->pageurl}={$i} ' {$currnt}> $i </a>".                     \ n "; } $str. = "<span class=\" dot\ ">......</span>". "                     \ n "; for ($i = $this->totalpagescount-3+1; $i <= $this->totalpagescount; $i + +)//function 1 {$st R. = "<a href= ' {$this->pageurl}={$i} ' > $i </a>". "                     \ n ";             }}elseif (5< $this->pageindex && $this->pageindex<= $this->totalpagescount-5)                         The current page is greater than 5 and is less than the total number of pages-5 {for ($i =1; $i <=3; $i + +) { $str. = "<a href= ' {$this->pageurl}={$i} ' > $i </a>". "                     \ n ";                                  } $str. = "<span class=\" dot\ ">......</span>";                     for ($i = $this->pageindex-1; $i <= $this->pageindex+1 && $i <= $this->totalpagescount-5+1; $i + +) {if ($i = = $this->pageindex) {$currnt = "class=" current                           '";}    else {$currnt = "";         }                   $str. = "<a href= ' {$this->pageurl}={$i} ' {$currnt}> $i </a>".                     \ n ";                     } $str. = "<span class=\" dot\ ">......</span>";                          for ($i = $this->totalpagescount-3+1; $i <= $this->totalpagescount; $i + +) { $str. = "<a href= ' {$this->pageurl}={$i} ' > $i </a>". "                     \ n ";                         }}else {for ($i =1; $i <=3; $i + +) { $str. = "<a href= ' {$this->pageurl}={$i} ' > $i </a>". "                     \ n "; } $str. = "<span class=\" dot\ ">......</span>". "                      \ n ";                           for ($i = $this->totalpagescount-5; $i <= $this->totalpagescount; $i + +)//function 1 {                           if ($i = = $this->pageindex) {$currnt = "class= '";}     Else                     {$currnt = ""; } $str. = "<a href= ' {$this->pageurl}={$i} ' {$currnt}> $i </a>".                     \ n ";           }}/* Except for the end of the first page of pagination logic */ Next last if ($this->pageindex== $this->totalpagescount) {$str. = "\ n". " <a href= ' javascript:void (0) ' class= ' tips ' title= ' next page ' > Next </a> '. '                \ n "; $str. = "<a href= ' javascript:void (0) ' class= ' tips ' title= ' last ' > Last </a> '. '                            \ n "; }else {$str. = "\ n". " <a href= ' {$this->pageurl}= '. ($this->pageindex+1). "' class= ' tips ' title= ' next page > Next </a> '. '                \ n "; $str. = "<a href= ' {$this->pageurl}={$this->totalpagescount} ' class= ' tips ' title= ' last ' > End </a>". "            \ n ";            } $str. = "</p>";        return $str; }         /** * Get Instance * @return * *//static public function getinstance () {//if (Is_null (self::$_    Instance)) {//self::$_instance = new Pager ();    }//return self::$_instance; }}?>

(3) Pager.css:

body,html{padding:0px; margin:0px; color: #333333; font-family: "Song Body", Arial,lucida,verdana,helvetica,sans-serif; font-size:12px;    line-height:150%;}    h1,h2,h3,h4,h5,h6,ul,li,dl,dt,dd,form,img,p,label{margin:0; padding:0; border:none; list-style-type:none;} /** front page style **/. Pagination {margin:10px 0 0;padding:5px 0;text-align:rightright; height:20px; line-height:20px; font-family:Arial,    Helvetica, Sans-serif, "Song Body";} .    Pagination a {margin-left:2px;padding:2px 7px 2px;} . Pagination. dot{border:medium none; padding:4px 8px}. Pagination A:link,.    Pagination a:visited {border:1px solid #dedede; color: #696969; text-decoration:none;} . Pagination A:hover,. Pagination A:active,. Pagination A.current:link,. Pagination a.current:visited {border:1px solid #dedede; color: #fff; Background-color: #ff6600; background-image:none;    border: #ff6600 solid 1px;} . Pagination. selectbar{border: #dedede solid 1px; font-size:12px; width:95px; height:21px; line-height:21px; margin-left : 10px; Display:inlINE}. Pagination a.tips{_padding:4px 7px 1px;}

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.