Let's talk about the fifth bullet in PHP-the 9 Yang magic in paging. In the previous article, www.2cto.comkf201204128413.html tells you how to run and use recursive functions. I don't know if you understand it. if you don't understand it, I can post it below.
During the past two days, many of you have asked questions on the page. I will give you a detailed explanation of the application of paging technology:
We often see beautiful pages in some web applications,
Click the next page to display the content of the next page. is the page quite beautiful.
Well, after reading this article, I believe that the students can write beautiful pages.
Now I have a data table named mytable with the following data:
Id name pass age sex
1 wrw7921sqa5eb72c92a549dd5avfr112 20 1
2 Gao fushuai vdf79218965eb72c92a549dd5a334534 20 0
3 gbj792saa65eb72c92a549dd5avfd321 22 1
4 brothers connect 4534718965eb72ccdd549dd5a330342 24 1
5 lampbrothers sp_79218965eb72c92a549dd5vsfdwsx 24 1
6 lamp ret79218965eb72c92a549dd5a33bgyt 25 0
7 php 123qwe18965eb72c92a549dd5a330875 26 1
8 mysql 76e79218965eb72c92a549ddvfdd0dsa 28 0
9 apache 54ebnm18965eb72c92a549dd5a330cxz 30 1
First, create a paging link.
Echo "previous page
";
Echo "Next page
";
Echo "total ";
?>
Let's think about it. click the previous page. the current page number is reduced by one and the data on the previous page is displayed.
Assume that we use a variable $ page to control the current page number. Should the previous page be $ page-1?
The next page number is $ page + 1.
So the above link should be changed:
Echo "previous page
";
Echo "Next page
";
?>
In this way, you should understand how to upload the page numbers of the previous and next pages.
However, in actual development, a web software cannot allow users to upload pages. when users access a page, they only access index. php.
So we will handle this situation:
$ Page =! Empty ($ _ GET ['Page'])? $ _ GET ['Page']: 1
?>
In this case, if $ page is not given, that is, $ _ GET ['Page'] is null, we assign $ page 1 directly.
Next, we will display the data separately. that is to say, only the data on the first page is displayed, and only the data on the second page is displayed ........ page N only displays data on page N
In this case, we need to operate SQL statements. to display data by page, we use the limit clause of SQL statements.
Well, what should the SQL statement look like if only five records are displayed on each page?
So our
Page 1:
Whether the data displayed from the first to fifth rows should be "limit ";
Page 2:
The sixth to tenth rows of data displayed should be "limit 5, 5 ";
Page 3:
The displayed data of 11th to 15th entries should be "limit ";
..............
Let's look at a rule,
Check whether the n page is
Limit (n-1) * Number of lines per page, number of lines per page
That is to say, we need the current page number and the number of entries per page.
Well, as we just said, we can GET the current page number through $ _ GET ['Page'], so we can define the number of entries displayed on each page with a variable, skilled students can define a constant and put it in the configuration file,
Here we use variables to define, set the page size to 5
$ Page = $ _ GET ['Page'];
$ Pagesize = 5;
The limit clause of the assembled SQL statement should be "limit ($ page-1) * $ pagesize, $ pagesize"
Now, we are able to process the data by page,
However, to retrieve the total number of pages, we also need to count the total number of records from the database.
$ SQL = "select count (*) from mytable ";
$ Result = mysql_query ($ SQL );
We can use the mysql_result () function to retrieve the execution result.
$ Count = mysql_result ($ result, 0 );
Okay. The total number of records is $ count,
Now our total number of data entries is 9, and each page shows 5. think about whether our total number of pages should be 2, that is, ceil (9/5) = 2.
The formula for calculating the total number of pages is ceil ($ count/$ pagesize ),
Well, all the data we need has been obtained and finally assembled into an SQL statement. it is displayed as follows:
// Set how many records are displayed on each page
$ Pagesize = 5;
// Obtain the current page number
$ Page =! Empty ($ _ GET ['Page'])? $ _ GET ['Page']: 1;
// Total number of records
$ SQL = "select count (*) from mytable ";
$ Result = mysql_query ($ SQL );
$ Count = mysql_result ($ result, 0 );
// Total number of pages
$ Page_count = ceil ($ count/$ pagesize );
// Determine whether the current given page number is out of bounds
If ($ page <1 ){
$ Page = 1;
} Elseif ($ page> $ page_count ){
$ Page = $ page_count;
}
// Start assembling SQL statements
$ SQL = "select id, name, age, sex from mytable limit". ($ page-1) * $ pagesize. ','. $ pagesize;
// Send an SQL statement
$ Result = mysql_query ($ SQL );
// Output table
Echo"
";Echo"
";Echo"
User name |
Age |
Gender | ";Echo"
";// Defines the gender array$ Sex = array ('male', 'female ');// Traverse the output result setWhile ($ row = mysql_fetch_assoc ($ result )){Echo"
";Echo"
". $ Row ['name']." | ";Echo"
". $ Row ['age']." | ";Echo"
". $ Sex [$ row ['Sex ']." | ";[Backcolor = # ffffff] [color = #008ef1] echo"
"; [/Color] [/backcolor]}Echo"
";
// Display connections by page
$ Up = ($ page-1) <1 )? 1 :( $ page-1 );
$ Down = ($ page + 1)> $ page_count )? $ Page_count :( $ page + 1 );
Echo "previous page ";
Echo "next page ";
Echo "total". $ page_count. "page ";
Echo "current page". $ page. "page ";
Echo "total". $ count. "records ";
?>
Okay, so we can add the paging function. how is it? is it very simple.
Let me summarize the detailed steps for paging:
Step 1: Set the page size (how many lines are displayed per page)
Step 2: obtain the current page number
Step 3: obtain the total number of records
Step 4: obtain the total number of pages
Step 5: Determine whether the current given page number is out of bounds
Step 6: assemble SQL statements
Step 7: send SQL statements to the mysql server
Step 8: traverse the result set output
Step 9: add a paging link
As long as you keep in mind the martial arts secret of the above Jie GE paging 9 Yang, I believe that the students will be able to ride the PHP martial arts killing ground leisurely
Author: zdrjlamp
...