Php paging principle instance parsing, php simple paging code
- $ Page = isset ($ _ GET ['Page'])? Intval ($ _ GET ['Page']): 1; // obtain the value of the page in page = 18. if there is no page, the page number is 1.
- $ Num = 10; // 10 data entries per page
- $ Db = mysql_connect ("host", "name", "pass"); // create a database connection
- $ Select = mysql_select_db ("db", $ db); // select the database to operate
- $ Total = mysql_num_rows (mysql_query ("select * from table"); // The total number of queried data
- $ Pagenum = ceil ($ total/$ num); // obtain the total number of pages
- // If the input page number parameter is greater than the total page number, the error message is displayed.
- If ($ page> $ pagenum | $ page = 0 ){
- Echo "Error: Can Not Found The page .";
- Exit;
- }
- $ Offset = ($ page-1) * $ num; // obtain the value of the first parameter of limit. if the first page is (1-1) * 10 = 0, the second page is (2-1) * 10 = 10.
- $ Info = mysql_query ("select * from table limit $ offset, $ num"); // obtain the data to be displayed for the corresponding page number.
- While ($ it = mysql_fetch_array ($ info )){
- Echo $ it ['name']."
";
- For ($ I = 1; $ I <= $ pagenum; $ I ++ ){
- $ Show = ($ I! = $ Page )? "$ I ":"$ I";
- Echo $ show ."";
- }
- }
- ?>
You can use the above code to replace the database connection and query tables with your own, and test the paging effect. |