Project structure:
Running effect:
Database Connection code
| The code is as follows: |
Copy code |
|
<? Php
$ 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", "<br>", str_replace ("", "& nbsp;", $ content ));
Return $ content;
}
?> |
Here is an important core function.
| The code is as follows: |
Copy code |
|
<? Php
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 = "show the <B>". ($ totle? ($ Firstcount + 1): 0 ). "</B>-<B> ". min ($ firstcount + $ displaypg, $ totle ). "</B> records, total $ totle records ";
// If there is only one page, the function will jump out:
If ($ lastpg <= 1)
Return false;
$ Pagenav. = "<a href = '$ url = 1'> homepage </a> ";
If ($ prepg)
$ Pagenav. = "<a href = '$ url = $ prepg'> Previous Page </a> ";
Else
$ Pagenav. = "front page ";
If ($ nextpg)
$ Pagenav. = "<a href = '$ url = $ nextpg'> back page </a> ";
Else
$ Pagenav. = "";
$ Pagenav. = "<a href = '$ url = $ lastpg'> last page </a> ";
// Pull-down Jump List, listing all page numbers cyclically:
$ Pagenav. = "to the <select name = 'topage' size = '1' onchange = 'window. location =" $ url = "+ this. Value'> n ";
For ($ I = 1; $ I <= $ lastpg; $ I ++ ){
If ($ I = $ page)
$ Pagenav. = "<option value = '$ I' selected> $ I </option> n ";
Else
$ Pagenav. = "<option value = '$ I'> $ I </option> n ";
}
$ Pagenav. = "</select> 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 "
}
?>
|
List. php
Database query records and generate SQL query statements
| The code is as follows: |
Copy code |
<? Php
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 in total". 21 "<a href = $ url? Page = ". ($ pageval-1)."> Previous page </a> <a href = $ url? Page = ". ($ pageval + 1)."> Next page </a> ";
}
$ SQL = "SELECT * FROM 'test' limit $ page $ pagesize ";
$ Query = mysql_query ($ SQL );
While ($ row = mysql_fetch_array ($ query )){
Echo "}
?> |
Paging formula: (current page-1) * number of entries per page, number of entries per page
| The code is as follows: |
Copy code |
| 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: |
Copy code |
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