Home > Backend Development > PHP Tutorial > Exquisite and beautiful php pagination class code_PHP tutorial

Exquisite and beautiful php pagination class code_PHP tutorial

WBOY
Release: 2016-07-21 15:12:27
Original
931 people have browsed it

This is a simple, convenient and fully functional paging class. You can change the CSS style file according to your own needs to control the paging color. Using the php paging class, you can save yourself a lot of time. You only need to Just embed it somewhere, see how to use it below:

1. Include pager.css in the head

Copy code The code is as follows:



2. Instantiate the class at the paging point:

Copy code The code is as follows:

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. Reading the mysql database and paging calculation files need to be written by yourself. For example:

$info=mysql_query("select * from member order by id desc limit $offset,$info_num"); In this way, the mysql database and current page number can be transferred.

The following are three files included in total: (1) index.php: call the paging class and display the paging (2) pager.class.php: php paging class (3) pager.css: css style beautification file

(1) index.php:

Copy code The code is as follows:



----Paging demonstration-----



include "pager.class.php ";
$CurrentPage=isset($_GET['page'])?$_GET['page']:1;
//die($CurrentPage);
$myPage=new pager(1300 ,intval($CurrentPage));
$pageStr= $myPage->GetPagerContent();
//echo $pageStr;
$pageStr= $myPage->GetPagerContent();
echo $pageStr;
?>


(2) pager.class.php:


Copy code The code is as follows:

/*  
 * PHP分页类  
 * @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 = 10,$p_initNum=3,$p_initMaxNum=5) {   
        if (! isset ( $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();   
    }   

       
  /**                                                                              */  
  private function _getPageUrl() {   
        $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;   

  }   
  /*  
   *设置页面参数合法性  
   *@return void  
  */  
  private function _initPagerLegal()   
  {   
      if((!is_numeric($this->pageIndex)) ||  $this->pageIndex<1)   
      {   
          $this->pageIndex=1;   
      }elseif($this->pageIndex > $this->totalPagesCount)   
      {   
          $this->pageIndex=$this->totalPagesCount;   
      }   

         

  }   
//$this->pageUrl}={$i}   
//{$this->CurrentUrl}={$this->TotalPages}   
    public function GetPagerContent() {   
        $str = "
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template