-
-
//Connect to the database
- $con = mysql_connect("localhost","root","");
- mysql_select_db("xueshengchu",$con);
- mysql_query("set names utf8 ");
-
- $pageSize = 5; //Number of data items displayed on each page
-
- $result = mysql_query("select * from stu_msg");
- $totalNum = mysql_num_rows($result); //Total number of data items
-
- $totalPageCount = intval($totalNum/$pageSize); //Total number of pages
-
- //Determine which page the current page is
- $nowPage = isset($_GET['page']) ? intval($_GET[' page']) : 1;
- //Previous page
- $prev = ($nowPage-1 //Next page
- $next = ($nowPage+1 >= $totalPageCount) ? $totalPageCount : $nowPage+1;
-
- //Offset
- $offset = ($nowPage-1)*$pageSize;
-
- /*You can get data, SQL statement paging principle
- * 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 "< ;a href="".$_SERVER['PHP_SELF']."?page=1">Homepage";
- echo "Previous page";
- echo "Next page";
- echo "Last page< ;/a>";
- ?>
Copy code
php paging code demonstration effect:
Code description:
The php code above the png has been commented very clearly
Analysis:
1. The global variable $_SERVER['PHP_SELF'] is the absolute path to obtain script variables.
2. The essence of paging is to display the database data piece by piece. For example, in the above PHP example:
Take out the first 5 items (first page): select * from table limit 0,5
Take out items 5 to 1 and 10 (second page): select * from table limit 5,5
This gives us the offset formula: $offset = ($nowPage-1)*$pageSize;
3. The intval() function is to get the integer value of the variable
|