Php paging program implementation code-php Tutorial

Source: Internet
Author: User
Php paging program implementation code for php development. In php + mysql, it is relatively easy to implement paging. as long as the page is obtained and several records per page X and then limitn is used, M can implement paging perfectly, in this example, it is clear that it is relatively easy to implement paging in php + mysql. you only need to retrieve several records of page X and then use limit n, M can be perfectly implemented by page. this example clearly describes the reference of students who need it.

Project structure:

Running effect:

Database Connection code

The code is as follows:

$ Conn = @ mysql_connect ("localhost", "root", "") or die ("database link error"); mysql_select_db ("form", $ conn );
Mysql_query ("set names 'gbk'"); // use GBK Chinese encoding;
// Replace spaces and enter the enter key
Function htmtocode ($ content)
{
$ Content = str_replace ("n ","
", Str_replace (" "," ", $ content ));
Return $ content;
}
?>

Here is an important core function.

The code is as follows:


Function _ PAGEFT ($ totle, $ displaypg = 20, $ url = ''){

Global $ page, $ firstcount, $ pagenav, $ _ SERVER;

$ GLOBALS ["displaypg"] = $ displaypg;

If (! $ Page)
$ Page = 1;
If (! $ Url ){
$ Url = $ _ SERVER ["REQUEST_URI"];
}

// URL analysis:
$ Parse_url = parse_url ($ url );
$ Url_query = $ parse_url ["query"]; // Retrieve the query string of the URL separately.
If ($ url_query ){
$ Url_query = ereg_replace ("(^ | &) page = $ page", "", $ url_query );
$ Url = str_replace ($ parse_url ["query"], $ url_query, $ url );
If ($ url_query)
$ Url. = "& page ";
Else
$ Url. = "page ";
} Else {
$ Url. = "? Page ";
}
$ Lastpg = ceil ($ totle/$ displaypg); // The last page, also the total number of pages
$ Page = min ($ lastpg, $ page );
$ Prepg = $ page-1; // Previous page
$ Nextpg = ($ page = $ lastpg? 0: $ page + 1); // Next page
$ Firstcount = ($ page-1) * $ displaypg;

// Start the paging navigation bar code:
$ Pagenav = "display". ($ Totle? ($ Firstcount + 1): 0 )."-". Min ($ firstcount + $ displaypg, $ totle )."A total of $ totle records ";

// If there is only one page, the function will jump out:
If ($ lastpg <= 1)
Return false;

$ Pagenav. = "homepage ";
If ($ prepg)
$ Pagenav. = "front page ";
Else
$ Pagenav. = "front page ";
If ($ nextpg)
$ Pagenav. = "";
Else
$ Pagenav. = "";
$ Pagenav. = "Last page ";

// Pull-down Jump List, listing all page numbers cyclically:
$ Pagenav. ="N ";For ($ I = 1; $ I <= $ lastpg; $ I ++ ){If ($ I = $ page)$ Pagenav. ="$ IN ";Else$ Pagenav. ="$ IN ";}$ Pagenav. ="Page, total $ lastpg page ";
}


Include ("conn. php ");

$ Result = mysql_query ("SELECT * FROM 'test "');
$ Total = mysql_num_rows ($ result );
// Call pageft () to display 10 messages per page (this parameter can be omitted when the default value is 20). use the URL on this page (which is omitted by default ).
_ PAGEFT ($ total, 5 );
Echo $ pagenav;

$ Result = mysql_query ("SELECT * FROM 'test' limit $ firstcount, $ displaypg ");
While ($ row = mysql_fetch_array ($ result )){

Echo"". $ Row [name]." | ". $ row [sex];

}
?>

List. php

Database query records and generate SQL query statements

The code is as follows:
Include ("conn. php ");
$ Pagesize = 5; 5 $ url = $ _ SERVER ["REQUEST_URI"];
$ Url = parse_url ($ url );
$ Url = $ url [path];
$ Numq = mysql_query ("SELECT * FROM 'test "');
$ 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". 21 "previous page ";
}
$ SQL = "SELECT * FROM 'test' limit $ page $ pagesize ";
$ Query = mysql_query ($ SQL );
While ($ row = mysql_fetch_array ($ query )){
Echo"". $ Row [name]." | ". $ row [sex];
}
?>

Paging formula: (current page-1) * number of entries per page, number of entries per page

The code is as follows:
SQL statement: select * from test_table limit ($ page-1) * $ pageSize, $ pageSize;

Summary:

No matter what program development is separated, it is a program that uses N records from X records, so that only a few records are read, and there is a limit function in mysql to operate, for example, limit 1 and 5 is to get 5 entries from the first one.

Next we will introduce the core code. here we get the page number, and the Xpagesize code is as follows:

The code is as follows:
If ($ _ GET [page]) {
$ Pageval = $ _ GET [page];
$ Page = ($ pageval-1) * $ pagesize;
$ Page. = ',';
}
If ($ num> $ pagesize ){
If ($ pageval <= 1) $ pageval = 1;


Splitting in mysql + php is much easier because of the limit

Merge several records on each page and then use limit n. M can implement pagination perfectly. this example clearly describes the need...

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.