Simple paging class and usage example implemented by PHP, php paging usage example _ PHP Tutorial-php Tutorial

Source: Internet
Author: User
Simple paging class and usage example implemented by PHP, and php paging usage example. PHP simple paging class and usage example. php paging usage example this article describes the simple paging class and usage of PHP implementation. For your reference, refer to the simple pagination class and usage example implemented by php and the PHP paging usage example.

This article describes the simple paging class implemented by PHP and its usage. We will share this with you for your reference. The details are as follows:

<? Php/** usage: * $ page = new Page (connector, query statement, current page number, Page size, page number) * connector: a MYSQL connection identifier, if this parameter is left blank, use the last connection * query statement: SQL statement * Current page number: specify the current page number * size of each page: number of records displayed per page * page number character: specify the URL format of the current page ** example: * $ SQL = "select * from aa "; * $ page = new Page ($ conn, $ SQL, $ _ GET ['Page'], 4 ,"? Page = "); ** get the current page number * $ page-> page; ** get the total page number * $ page-> pageCount; ** get the total number of records ** $ page-> rowCount; ** get the number of records on the current page * $ page-> listSize; ** get the record set ** $ page-> list; * the record set is a two-dimensional array. for example, list [0] ['id'] accesses the id field value of the first record. ** obtain the page number list * $ Page-> getPageList (); */class page {// basic data var $ SQL; var $ page; var $ pageSize; var $ pageStr; // statistics var $ pageCount; // Number of pages var $ rowCount; // number of records // result data var $ list = array (); // result row array var $ listSize; // Constructor 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; // process 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 "," $1 count (0) $3 ", $ 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]; // calculate the total number of pages if ($ this-> rowCount % $ this-> pageSize = 0) $ this-> pageCount = intval ($ this-> rowCount/$ this-> pageSize); 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);}/** generate a simple page number list using the getPageList method * if you need to customize the page number list, you can modify the code here, or use the total number of pages/total number of records and other information for calculation and generation. */function getPageList () {$ firstPage; $ previousPage; $ pageList; $ nextPage; $ lastPage; $ currentPage; // if the page number is greater than 1, if ($ this-> page> 1) {$ firstPage = "pageStr. "1 \"> homepage ";} // if the page number is greater than 1, if ($ this-> page> 1) {$ previousPage =" pageStr. ($ this-> page-1 ). "\"> Previous page ";} // if the last page is not reached, the next page connection if ($ this-> page <$ this-> pageCount) {$ nextPage =" pageStr. ($ this-> page + 1 ). "\"> Next page ";} // if the last page is not reached, if ($ this-> page <$ this-> pageCount) {$ lastPage =" pageStr. $ this-> pageCount. "\"> last page ";} // list of all page numbers 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 example:

<? Php @ $ db = mysql_connect ('localhost', 'root', '000000') or die ("cocould not connect to database. "); // connect to the database mysql_query (" set names 'utf8' "); // output the 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 pagelist 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.