Quickly learn PHP article paging skills. 1. preface for web programming veterans, writing such code is as natural as breathing, but for beginners, they often have no clue about this issue.
1. Preface
For veterans of web programming, writing such code is as natural as breathing, but for beginners, they often have no clue about this issue. Therefore, I wrote this article to explain this issue in detail, we strive to make friends who have read this article understand the principle and implementation methods of paging display. This article is suitable for beginners. all sample code is written in php.
2. paging principles of PHP articles
The so-called paging display means that the result set in the database is manually divided into segments for display. here two initial parameters are required:
How many records per page ($ PageSize )?
What is the current page ($ CurrentPageID )?
Now, you only need to give me another result set, so that I can display a specific result.
Other parameters, such as the previous page ($ PreviousPageID), Next page ($ NextPageID), and total page ($ numPages), can all be obtained based on the front.
Take the mysql database as an example. if you want to extract some content from the table, you can use the SQL statement: select * from table limit offset, rows. Take a look at the following SQL statements and try to find the rule rate.
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
......
This set of SQL statements is the SQL statement used to retrieve data of each page in the table when $ PageSize = 10. we can summarize this template:
Select * from table limit ($ CurrentPageID-1) * $ PageSize, $ PageSize
Use this template to compare the corresponding values with the preceding SQL statements to see if this is the case. After solving the most important problem of how to obtain data, the rest is simply passing parameters, constructing appropriate SQL statements, and then using php to obtain data from the database and display it. I will describe the code below.
3. simple code for pagination of PHP articles
Please read the following code in detail and debug and run it once. you 'd better modify it once and add your own functions, such as search.
- <? Php
- // Establish a database connection
- $ Link = mysql_connect ("localhost ",
"Mysql_user", "mysql_password ")
- Or die ("cocould not connect:". mysql_error ());
- // Obtain the current page number
- If (isset ($ _ GET ['Page']) {
- $ Page = intval ($ _ GET ['Page']);
- }
- Else {
- $ Page = 1;
- }
- // Number of pages per page
- $ PageSize = 10;
- // Obtain the total data volume
- $ SQL = "select count (*) as amount from table ";
- $ Result = mysql_query ($ SQL );
- $ Row = mysql_fetch_row ($ result );
- $ Amount = $ row ['amount'];
- // Count the total number of pages
- If ($ amount ){
- If ($ amount <$ page_size) {$ page_count = 1 ;}
- // If the total data volume is less than $ PageSize, there is only one page
- If ($ amount % $ page_size ){
- // Divide the total data volume by the remainder of each page
- $ Page_count = (int) ($ amount/$ page_size) + 1;
- // If there is a remainder, the number of pages equals the total data volume divided by the end of each page
If the result is an integer, add one.
- } Else {
- $ Page_count = $ amount/$ page_size;
- // If there is no remainder, the number of pages equals the total data volume divided by the result of each page
- }
- }
- Else {
- $ Page_count = 0;
- }
-
- // Flip link
- $ Page_string = '';
- If ($ page = 1 ){
- $ Page_string. = 'Page 1 | previous page | ';
- }
- Else {
- $ Page_string. = 'Page 1
|. ($ Page-1). '> Previous page | ';
- }
- If ($ page = $ page_count) | ($ page_count = 0 )){
- $ Page_string. = 'next page | last page ';
- }
- Else {
- $ Page_string. = '. ($ page + 1).'>
Next Page | last page ';
- }
- // Obtain data and return results in two-dimensional array format
- If ($ amount ){
- $ SQL = "select * from table order by id desc
Limit ". ($ page-1) * $ page_size.", $ page_size ";
- $ Result = mysql_query ($ SQL );
-
- While ($ row = mysql_fetch_row ($ result )){
- $ Rowset [] = $ row;
- }
- } Else {
- $ Rowset = array ();
- }
- // The code that does not contain the display result is not in the scope of the discussion,
With foreach, you can simply use the two-dimensional array to display the result.
- ?>
The above is the implementation of PHP article paging.
NLP for web programming veterans, writing this kind of code is as natural as breathing, but for beginners, it is often hard to find a clue about this problem, so I wrote it specially...