PHP implementation of a simple pagination class code example usage

Source: Internet
Author: User
Paging (pagination), which divides a page into two or more than two pages.

There is an automatic paging mechanism that allows you to move the inside of a mobile Web Form

Partitions into a group of smaller pages to be rendered to fit a particular device. The mechanism also renders user interface elements that can be used to browse to other pages.

This article mainly introduces the PHP implementation of the simple paging class and usage, combined with the example form of PHP paging class function, definition and specific use of skills, the need for friends can refer to the following :

<?php/* * Used: * $page = new page (connector, query statement, current page number, per page size, page number) * Connector: A MySQL connection identifier, if left blank, uses the most recent connection * query statement: SQL statement * Current page: Specify current Page * per page size: Number of records displayed per page * page break: Specify the current page URL format * * Use example: * $sql = "SELECT * from AA"; * $page = new page ($conn, $sql, $_get[' page '],4, "? page="); * * Get the current page number * $page->page; * * Get Total pages * $page->pagecount; * * Total Record count * $page->rowcount; * * Get the number of records on this page * $page->listsize; * * Get record set * $page->list; * Recordset is a 2-D array, example: list[0][' ID '] accesses the ID field value of the first record. * * Get a list of page numbers * $page->getpagelist ();  */class page{//Basic data var $sql;  var $page;  var $pageSize;  var $pageStr; Statistical data var $pageCount; Number of pages Var $rowCount; Number of records//result data var $list = array ();  Result row array Var $listSize;    constructor function Page ($conn, $sql _in, $page _in, $pageSize _in, $pageStr _in) {$this->sql = $sql _in;    $this->page = intval ($page _in);    $this->pagesize = $pageSize _in;    $this->pagestr = $pageStr _in; The page number is empty or less than 1 of the processing if (! $this->page| |    $this->page<1) {$this->page = 1; }//Query the total number of records   $ROWCOUNTSQL = Preg_replace ("/([\w\w]*?select) ([\w\w]*?)] (from[\w\w]*?)    /i "," Count (0) $ $ ", $this->sql);    if (! $conn) $rs = mysql_query ($ROWCOUNTSQL) or Die ("Bnc.page:error on getting RowCount.");    else $rs = mysql_query ($ROWCOUNTSQL, $conn) or Die ("Bnc.page:error on getting RowCount.");    $rowCountRow = Mysql_fetch_row ($RS);    $this->rowcount= $rowCountRow [0]; Calculates the total number of pages if ($this->rowcount% $this->pagesize==0) $this->pagecount = intval ($this->rowcount/$this->pag    Esize);    else $this->pagecount = intval ($this->rowcount/$this->pagesize) +1;    SQL Offset $offset = ($this->page-1) * $this->pagesize; if (! $conn) $rs = mysql_query ($this->sql. "Limit $offset,". $this->pagesize) or Die ("Bnc.page:error on listing.")    ); else $rs = mysql_query ($this->sql. "Limit $offset,". $this->pagesize, $conn) or Die ("Bnc.page:error on listing.")    );    while ($row =mysql_fetch_array ($rs)) {$this->list[]= $row; }   $this->listsize = count ($this->list);   }/* * Getpagelist method generates a simpler page number list * If you need to customize the page number list, you can modify the code here, or use information such as total number of pages/Total records to generate calculations.    */function Getpagelist () {$firstPage;    $previousPage;    $pageList;    $nextPage;    $lastPage;    $currentPage; If the page number >1 displays the first page connected if ($this->page>1) {$firstPage = "<a href=\" ". $this->pagestr."    1\ "> Home </a>"; }//If the page number >1 displays the previous page connected if ($this->page>1) {$previousPage = "<a href=\" ". $this->pagestr. $this->page-1). "    \ "> Prev </a>"; }//If not to the end, the next page will be connected if ($this->page< $this->pagecount) {$nextPage = "<a href=\" ". $this->pagestr . ($this->page+1). "    \ "> Next </a>"; }//If not to the last page, display the last connection if ($this->page< $this->pagecount) {$lastPage = "<a href=\" ". $this->pagestr. $this->pagecount. "    \ "> End </a>";   }//All page list for ($counter =1; $counter <= $this->pagecount; $counter + +) {if ($this->page = = $counter)   {$currentPage = "<b>". $counter. "      </b> "; } else {$currentPage = "". " <a href=\ "". $this->pagestr. $counter. " \ ">". $counter. " </a> "."      ";    } $pageList. = $currentPage; } return $firstPage. " ". $previousPage." ". $pageList." ". $nextPage." ". $lastPage."  "; }}?>

Usage examples:

<?php@ $db = mysql_connect (' localhost ', ' root ', ' 123456 ') or Die    ("Could does connect to database."); /Connect Database mysql_query ("Set names ' UTF8 '");//Output Chinese mysql_select_db (' test ');    Select Database $sql = "SELECT * from ' users '"; A simple query $page = new Page (', $sql, $_get[' page '],5, '? page= '); $rows = $page->list;foreach ($rows as $row) {  echo $ row[' UserName ']. " <br> ";} echo $page->getpagelist (); Output Paging list?>

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.