Php paging principle

Source: Internet
Author: User
Php paging principle 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 example, select * from table LIMIT 5; // return the first five rows, the same as select * from table LIMIT

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)

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; obtain the 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

$ Conn = @ mysql_connect ("localhost", "root", "123456") or die ("database link error ");
Mysql_select_db ("bbs", $ conn );
Mysql_query ("set names 'gbk'"); // use GBK Chinese encoding;

// Convert spaces and line breaks into HTML parses

Function htmtocode ($ content ){
$ Content = str_replace ("\ n ","
", Str_replace (" "," ", $ content); // Two str_replace nesting

Return $ content;
}

// $ Content = str_replace ("'", "'", $ content );
// Htmlspecialchars ();

(3) display page

Include ("conn. php ");

$ Pagesize = 2; // set to display 2 records per 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 [page]) {
$ Pageval = $ _ GET [page];
$ Page = ($ pageval-1) * $ pagesize;
$ Page. = ',';
}
If ($ num> $ pagesize ){
If ($ pageval <= 1) $ pageval = 1;

Echo "$ num total ".
"Next page of the previous page ";
}
$ SQL = "SELECT * FROM 'message' limit $ page $ pagesize ";
$ Query = mysql_query ($ SQL );

While ($ row = mysql_fetch_array ($ query )){
?>













Title: Time:
User:
Content:

}
?>
(4) Last display

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.