PHP implementation of a simple pagination class and usage examples, PHP page usage Example _php tutorial

Source: Internet
Author: User
Tags php database php regular expression rowcount

PHP implementation of a simple paging class and usage examples, PHP page usage example


This paper describes the simple page-paging class and usage of PHP implementation. Share to everyone for your reference, as follows:

<?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 = "pagestr."    1\ "> Home"; }//If the page number >1 displays the previous page connected if ($this->page>1) {$previousPage = "pagestr. $this->page-1). "    \ "> Previous page"; }//If not to the last page, display the next link if ($this->page< $this->pagecount) {$nextPage = "pagestr. $this->page+1). "    \ "> Next page"; }//If not to the last page, display the last connection if ($this->page< $this->pagecount) {$lastPage = "pagestr. $this->pagecount."    \ "> Last";      }//All page list for ($counter =1; $counter <= $this->pagecount; $counter + +) {if ($this->page = = $counter) {$currentPage = "". $counter.""; } else {$currentPage = "". " Pagestr. $counter. " \ ">". $counter. "". "      ";    } $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 ']. "
";} echo $page->getpagelist (); Output Paging list?>

More about PHP related content readers can view the topic: "PHP array" operation Skills Daquan, "PHP Math Skills Summary", "PHP Regular Expression Usage Summary", "Php+ajax Tips and Applications Summary", "PHP operation and operator Usage Summary", " PHP Network Programming Skills Summary, PHP Basic Grammar Introductory tutorial, PHP date and Time usage summary, PHP Object-oriented Programming primer tutorial, PHP string Usage Summary, PHP+MYSQL Database Operations primer, and A summary of common PHP database operation techniques

I hope this article is helpful to you in PHP programming.

http://www.bkjia.com/PHPjc/1123791.html www.bkjia.com true http://www.bkjia.com/PHPjc/1123791.html techarticle PHP Implementation of a simple paging class and usage examples, PHP page Usage Example This article describes the PHP implementation of the simple paging class and usage. Share to everyone for your reference, as follows: php/ ...

  • 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.