Home  >  Q&A  >  body text

How to automatically generate page numbers 1, 2, 3..., help, urgent

<?php

//Paging function

//Connect to database

require_once("connect.php");

$page = isset($_GET['page'])?intval($_GET['page']):1;//Set the current page number, if not, set it to 1

$num=3;// Display 3 pieces of data per page

/*

First of all, we need to get how much data there is in the database to determine how many pages it needs to be divided into. The specific formula for the total number of pages is

The total number of data is divided by the number of items displayed on each page, and the remainder is rounded to one.

That is to say, 10/3=3.3333=4 If there is a remainder, we must round it up by one.

*/

$sql="select * from article";

$result=mysqli_query($conn,$sql);

$total =mysqli_num_rows($result);//Query the total number of data items

$pagenum=ceil($total/$num);//Get the total number of pages

//If passed in If the page number parameter page is greater than the total page number pagenum, an error message will be displayed

if($page>$pagenum || $page == 0){

echo "<script>alert ('No more content');history.go(-1);</script>";

exit;

}

$offset=($page -1)*$num;

/* Get the value offset of the first parameter of limit. If the first page is (1-1)*10=0, the second page is (2-1 )*10=10. (Number of pages passed in - 1) * The data of each page gets the value of the first parameter of limit */

$sql="select * from article limit $offset,$num ";

$info=mysqli_query($conn,$sql); //Get the data that needs to be displayed for the corresponding page number

//Get the first six newly added data

$sql_new=" select id,title from article order by dateline desc limit 0,6 ";

$info_title=mysqli_query($conn,$sql_new);

?>


梁温柔梁温柔2348 days ago1146

reply all(0)I'll reply

No reply
  • Cancelreply