-
- $page=isset($_GET['page']) ? intval($_GET['page']) : 1; //Get the value of page in page=18, if If page does not exist, then the page number is 1.
- $num=10; //Display 10 pieces of data 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 query data
- $pagenum=ceil($total/$num); //Get the total pages Number
- //If the page number parameter passed in is greater than the total number of pages, an error message will be displayed
- if($page>$pagenum || $page ==0){
- echo "Error : Can Not Found The page .";
- exit;
- }
- $offset=($page-1) * $num; //Get the value of the first parameter of limit, if the first page is (1-1)*10=0, the second page It is (2-1)*10=10.
- $info=mysql_query("select * from table limit$offset,$num"); //Get 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." ";
- }
- }
- ?>
-
Copy the code
You can replace the database connection and query tables with your own according to the above code, and test the paging effect.
|