Php paging principle example tutorial, php paging principle demonstration
- // Connect to the database
- $ Con = mysql_connect ("localhost", "root ","");
- Mysql_select_db ("xueshengchu", $ con );
- Mysql_query ("set names utf8 ");
- $ PageSize = 5; // number of data entries per page
- $ Result = mysql_query ("select * from stu_msg ");
- $ TotalNum = mysql_num_rows ($ result); // The total number of data entries.
- $ TotalPageCount = intval ($ totalNum/$ pageSize); // total number of pages
- // Determine the current page
- $ NowPage = isset ($ _ GET ['Page'])? Intval ($ _ GET ['Page']): 1;
- // Previous Page
- $ Prev = ($ nowPage-1 <= 0 )? 1: $ nowPage-1;
- // Next page
- $ Next = ($ nowPage + 1 >=$ totalPageCount )? $ TotalPageCount: $ nowPage + 1;
- // Offset
- $ Offset = ($ nowPage-1) * $ pageSize;
- /* Data can be retrieved. The paging principle of SQL statements
- * Select * from table limit $ offset, $ size paging execution statement
- */Bbs.it-home.org
- $ SQL = "select * from stu_msg limit $ offset, $ pageSize ";
- $ Result = mysql_query ($ SQL, $ con );
- While ($ arr = mysql_fetch_array ($ result )){
- Echo $ arr ['mid ']. $ arr ['content']."
";
- }
- Echo "homepage ";
- Echo "previous page ";
- Echo "next page ";
- Echo "last page ";
- ?>
Php paging code demo:
Code description: php code above png has been commented out clearly
Analysis:
1. the global variable $ _ SERVER ['php _ SELF '] is the absolute path to get the script variable.
2. the essence of paging is to display the data of the database in a segment. for example, in the above php instance, extract the first five (the first page ): select * from table limit 5th fetch 10th to 1 (second page): select * from table limit 5 5 get the offset formula: $ offset = ($ nowPage-1) * $ pageSize;
3. the intval () function is used to obtain the integer value of the variable. |