Reprint: Paging principle + paging code + paging class production
WBOY
Release: 2016-09-24 09:02:46
Original
1027 people have browsed it
Paging display is a very common method of browsing and displaying large amounts of data, and it is one of the most commonly processed events in web programming. For veterans of web programming, writing this kind of code is as natural as breathing, but for beginners, they are often confused about this issue, so I specially wrote this article to explain this issue in detail.
1. Paging principle: The so-called paging display is to artificially divide the result set in the database into sections for display. Two initial parameters are required here:
How many records per page ($PageSize)? What page is the current page ($CurrentPageID)?
Now just give me another result set, and I can display a specific result.
As for other parameters, such as: previous page ($PReviousPageID), next page ($NextPageID), total number of pages ($numPages), etc., they can be obtained based on the previous things.
Taking the MySQL database as an example, if you want to intercept a certain piece of content from the table, the sql statement can be used: select * from table limit offset, rows. Take a look at the following set of SQL statements and try to find the rules.
The first 10 records: select * from table limit 0,10 The 11th to 20th records: select * from table limit 10,10 The 21st to 30th records: select * from table limit 20,10 ……语 This group of SQL statements is actually when $ PageSize = 10, take the SQL statement of each page of the table in the table. We can summarize such a template: Select * from Table Limit ($ CurrentPageid -1) * $ PageSize $ PAGESIZE a Take this template into the corresponding value and compare the set of SQL statements above to see if it is. After solving the most important problem of how to obtain the data, all that is left is to pass the parameters, construct the appropriate SQL statement and then use PHP to obtain the data from the database and display it.
2. Pagination code description: five steps The code is fully explained and can be copied to your own notepad for direct use
Employee information list
//Display information of all emp tables
//1. Connect to the database
$conn=mysql_connect('localhost','root','1234abcd') or die('Connect to database error'.mysql_error());
库 // 2. Select the database 2
mysql_select_db('empManage');
//3. Select character set
mysql_query('set names utf8');
//4. Send sql statement and get the result for processing
页 //4.1 Pagling [Pagling to emit two SQL statements, one is to get $ Rowcount, and the other is to obtain the paging result through the Limit of SQL. So we will get two result sets. Remember to distinguish them when naming them.
Paging (four values and two sql statements). ]
$rowCount=0;//How many records are there in total
O $ pagenow = 1; // hope to display the page of the first page
C $ PageCount = 0; // How many pages are there? Since $rowCount can be obtained from the server, the initial value can be given as 0;
$pageNow indicates which page you want to display. It is best to set it to 0 here; $pageSize indicates how many records are displayed on each page. This is determined in advance according to the needs of the website.
$pageCount=ceil($rowCount/$pageSize), since $rowCount can have an initial value of 0, then $pageCount can of course be set to 0. Four indicators, two 0, one 1, and the other is the website requirement. ]
’ ’ ’ to 4.15
’ s - 1 t- it to be paging link
if(!empty($_GET['pageNow'])){
$pageNow=$_GET['pageNow'];
}[Modify the value of $pageNow based on the paging link. ]
$sql='select count(id) from emp';
$res1=mysql_query($sql);
using using using through ’’s ’ through ’ s through cue through through ’ through ’ through ’ through ’ through ’ through ’ through'' way through through out's' back through through‐‐to‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐ ‐‐‐‐‐‐
if($row=mysql_fetch_row($res1)){
$rowCount=$row[0];
}//[Get $rowCount, and once we enter, we will know the two indicators $pageCount. ]
//4.12 Calculate how many pages there are
$pageCount=ceil($rowCount/$pageSize);
$pageStart=($pageNow-1)*$pageSize;
//4.13 Send sql results with pagination
= $ SQL = "Select * from Emp Limit $ PAGESTATART, $ PAGESIZE"; // [Based on the two values (starting values, number per page) behind the limit of the $ SQL statement, to achieve paging. and find these two values.]
$res2=mysql_query($sql,$conn) or die('无法获取结果集'.mysql_error());
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn