Php provides the paging function in three ways: Page 1/3, page 3 _ PHP Tutorial

Source: Internet
Author: User
Php provides three paging methods: 13th pages and 3 pages. Php implements the paging function in three ways: 13th pages, and code is directly displayed on three pages. I hope you can read it carefully. Method 1: When Querying SQL queries by page, you need to call several functions. for details, see page 1/3 in three ways of implementing the paging function in php. page 3

I hope you can read the code carefully.

Method 1: SQL query is performed by page. several functions need to be called. for details, see the script:
1. pager. class. php

<? Php class pager {public $ SQL; // SQL query statement public $ datanum; // query the total number of data records. public $ page_size; // The number of records displayed on each page. protected $ _ errstr; protected $ _ conn; protected $ _ query_id; public function query ($ query) // This function is faulty, you do not need to use {$ ret = false; if (! Empty ($ query) {if ($ this-> _ conn = false |! Is_resource ($ this-> _ conn) {warningLog (_ METHOD __. ': query SQL with no connection', true); return false;} $ this-> _ query_id = @ mysql_query ($ query, $ this-> _ conn ); if ($ this-> _ query_id === false) {$ this-> _ errstr = @ mysql_error (); $ ret = false ;} else {$ this-> _ errstr = 'success'; $ ret = $ this-> _ query_id; }}$ msg = ($ ret = false )? 'False': strval ($ ret); debugLog (_ METHOD __. ": [$ msg] returned for SQL query [$ query]"); return $ ret;} function _ construct ($ SQL, $ page_size) {$ result = mysql_query ($ SQL); $ datanum = mysql_num_rows ($ result); $ this-> SQL = $ SQL; $ this-> datanum = $ datanum; $ this-> page_size = $ page_size;} // public function page_id () {if ($ _ SERVER ['query _ string'] = "") of the current page "") {return 1;} elseif (substr_count ($ _ SERVER ['query _ S TRING '], "page_id =") = 0) {return 1;} else {return intval (substr ($ _ SERVER ['query _ string'], 8) ;}} // remaining url value public function url () {if ($ _ SERVER ['query _ string'] = "") {return "";} elseif (substr_count ($ _ SERVER ['query _ string'], "page_id =") = 0) {return "&". $ _ SERVER ['query _ string'];} else {return str_replace ("page_id = ". $ this-> page_id (), "", $ _ SERVER ['query _ string']) ;}// total page number public function page_num () {If ($ this-> datanum = 0) {return 1;} else {return ceil ($ this-> datanum/$ this-> page_size );}} // offset of database query public function start () {return ($ this-> page_id ()-1) * $ this-> page_size ;} // data output public function sqlquery () {return $ this-> SQL. "limit ". $ this-> start (). ",". $ this-> page_size;} // get the current file name private function php_self () {return $ _ SERVER ['php _ SELF '];} // the previous private function pre_page () {if ($ this -> Page_id () = 1) {// The number of pages equals 1 return "php_self ()."? Page_id = 1 ". $ this-> url ()."> Previous Page ";} elseif ($ this-> page_id ()! = 1) {// The number of pages is not equal to 1 return "php_self ()."? Page_id = ". ($ this-> page_id ()-1 ). $ this-> url (). "> Previous Page" ;}}// display the private function display_page () {$ display_page = ""; if ($ this-> page_num () <= 10) {// page less than 10 for ($ I = 1; $ I <= $ this-> page_num (); $ I ++) // The page is displayed cyclically $ display_page. = "php_self (). "? Page_id = ". $ I. $ this-> url (). "> ". $ I. ""; return $ display_page;} elseif ($ this-> page_num ()> 10) {// if ($ this-> page_id () <= 6) {for ($ I = 1; $ I <= 10; $ I ++) // The page is displayed cyclically $ display_page. = "php_self (). "? Page_id = ". $ I. $ this-> url (). "> ". $ I. ""; return $ display_page;} elseif ($ this-> page_id ()> 6) & ($ this-> page_num ()-$ this-> page_id ()> = 4) {for ($ I = $ this-> page_id ()-5; $ I <= $ this-> page_id () + 4; $ I ++) // The Page $ display_page is displayed cyclically. = "php_self (). "? Page_id = ". $ I. $ this-> url (). "> ". $ I. ""; return $ display_page;} elseif ($ this-> page_id ()> 6) & ($ this-> page_num ()-$ this-> page_id () <4) {for ($ I = $ this-> page_num ()-9; $ I <= $ this-> page_num (); $ I ++) // The Page $ display_page is displayed cyclically. = "php_self (). "? Page_id = ". $ I. $ this-> url (). "> ". $ I. ""; return $ display_page ;}}// next page private function next_page () {if ($ this-> page_id () <$ this-> page_num ()) {// The number of pages is smaller than the total number of pages return "php_self (). "? Page_id = ". ($ this-> page_id () + 1 ). $ this-> url (). "> Next Page";} elseif ($ this-> page_id () = $ this-> page_num () {// The number of pages equals to the total number of pages return "php_self (). "? Page_id = ". $ this-> page_num (). $ this-> url (). "> Next Page" ;}}// set the page information public function set_page_info () {$ page_info = "total ". $ this-> datanum. "entries"; $ page_info. = "php_self (). "? Page_id = 1 ". $ this-> url (). "> homepage"; $ page_info. = $ this-> pre_page (); $ page_info. = $ this-> display_page (); $ page_info. = $ this-> next_page (); $ page_info. = "php_self (). "? Page_id = ". $ this-> page_num (). $ this-> url (). "> last page"; $ page_info. = "th ". $ this-> page_id (). "/". $ this-> page_num (). "page"; return $ page_info ;}}?>

2. Script 2:

<? Php // class usage // read paging class include ("pager. class. php "); // database connection initialization // $ db = new mysql (); $ impeach_host = '10. 81.43.139 '; $ impeach_usr = 'vmtest15'; $ region = 'vmtest15'; $ impeach_name = 'ufeature'; $ impeach_con = mysql_connect ($ impeach_host, $ impeach_usr, $ region) or die ("Can't connect ". mysql_error (); mysql_select_db ($ impeach_name, $ impeach_con); // This is an SQL query statement and gets the query result $ SQL = "select word from ufeature. spam_accuse_word_list where flag = '0' "; // page initialization $ page = new pager ($ SQL, 20 ); // 20 is the number displayed per page // $ res_1 = mysql_query ($ SQL) or // die ("Can't get result ". mysql_error (); $ result = mysql_query ($ page-> sqlquery (); while ($ info = mysql_fetch_array ($ result, MYSQL_ASSOC )) {// while ($ info = mysql_fetch_array ($ res_1, MYSQL_ASSOC) {echo $ info ["word"]."
";}// Page number index echo $ page-> set_page_info () ;?>

Method 2: ajax
1. First, understand the limit usage in SQL statements.

SELECT * FROM table ...... Limit start position, number of operations (where the start position starts from 0)

Example
Take the first 20 records: SELECT * FROM table ...... Limit 0, 20
Obtain 20 records FROM 11th: SELECT * FROM table ...... Limit 10, 20
LIMIT n is equivalent to LIMIT 0, n.
For exampleSelect * from table LIMIT 5; // return the first five rows,AndSelect * from table LIMIT 0, 5Same
2. paging principle

The so-called paging display, that is, the result set in the database, is displayed in a segment
How to segment, the current segment (the number of lines per page, and the number of pages next)
The first 10 records:Select * from table limit 0, 10
11th to 20 records:Select * from table limit 10, 10
21st to 30 records:Select * from table limit 20, 10
Paging formula:
(Current page-1) X number of entries per page, number of entries per page

Select * from table limit ($Page- 1) * $PageSize, $PageSize 

3. $ _ SERVER ["REQUEST_URI"] function
A type of predefined SERVER variables. all variables starting with $ _ SERVER are called SERVER variables.
REQUEST_URI is used to obtain the current URI, which is the complete address path after the domain name.
Example:
The current page is: http://www.test.com/home.php? Id = 23 & cid = 22
Echo $ _ SERVER ["REQUEST_URI"]
Result:/home. php? Id = 23 & cid = 22
4. parse_url () parses the URL function.
Parse_url () is a function that parses a URL into an array with a fixed key value.
Example

$ua=parse_url("http://username:password@hostname/path?arg=value#anchor");print_r($ua);

Result:

Array ([scheme] => http; protocol [host] => hostname; host domain name [user] => username; user [pass] => password; password [path] =>/path; path [query] => arg = value; parameter [fragment] => anchor ;)

5. code example
The page for this message is divided into three parts: database design, connection page, and display page.
(1) design a database
The database name is bbs. there is a data table named message, which contains the title, lastdate, user, content and other fields, indicating the message title, message date, message recipient, and message content.
(2) connection page

<? Php $ conn = @ mysql_connect ("localhost", "root", "123456") or die ("database link error"); mysql_select_db ("bbs", $ conn ); mysql_query ("set names 'gbk'"); // uses GBK Chinese encoding; // converts spaces and line breaks to HTML-resolvable function htmtocode ($ content) {$ content = str_replace ("\ n ","
", Str_replace (" "," ", $ content); // Two str_replace nested return $ content;} // $ content = str_replace ("'","'", $ content); // htmlspecialchars ();?>

(3) display page

<? Php include ("conn. php "); $ pagesize = 2; // set to display 2 records on each page $ url = $ _ SERVER [" REQUEST_URI "]; $ url = parse_url ($ url ); $ url = $ url [path]; $ numq = mysql_query ("SELECT * FROM 'message'"); $ num = mysql_num_rows ($ numq); if ($ _ GET

Pipeline directly uploads the code. I hope you can read it carefully. Method 1: SQL query is performed by page. several functions need to be called. for details, see...

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.