Home > Article > Backend Development > How to display PHP list page_PHP tutorial
id
Value
$link = mysql_connect("localhost","root",""); //Connect to the database
mysql_select_db("cs");//Which database to choose
mysql_query("set names utf-8"); //Set the database character encoding to Chinese
$sql = mysql_query("select *from persons");
$pagesize = 5; //Number of items displayed
$sum = mysql_num_rows($sql); //Judge how many pieces of data there are in total
$count = ceil($sum/$pagesize);//Find out how many pages there are in total
$pages = $count; //Display the last page
$init = 1;
$page_len = 7;
$max_p = $count;
if(empty($_GET["page"])|| $_GET["page"]<0){
$page = 1;
}else{
$page = $_GET["page"];
}
$off = ($page-1)*$pagesize; //Find the first data in the database query
$he = mysql_query("select * from persons limit $off,$pagesize");//Start from off and display regularly to pagesize
echo "A total of ".$sum." pieces of data";
while($row = mysql_fetch_array($he)){
echo "";
echo "".$row['id']."";
echo "".$row['FirstName']."";
echo " ";
}
?>
$page_len = ($page_len%2)?$page_len:$page_len+1;//If the remainder is 1, it is true, if it is 0, it is false
$pageoffset = ($page_len-1)/2;//Page number offset
$key ="";
if($page!=1){
$key.="First page";
$key.="Previous page";
}else{
$key.="First page";
$key.="Previous page";
}
if($pages>$page_len){
if($page<=$pageoffset){
$init=1;
$max_p = $page_len;
}else{
if($page+$pageoffset>=$pages+1){
$init = $pages - $page_len+1;
}else{
$init = $page-$pageoffset;
$max_p = $page + $pageoffset;
}
}
}
for($i=$init;$i<=$max_p;$i++){
if($i==$page){
$key.="[ ".$i." ]";
}else{
$key.="$i";
}
}
if($i-1!=$page){
$key.="Next page";
$key.="Last page";
}else{
$key.="Next page";
$key.="Last page";
}
echo "";
echo "".$key."";
echo "";
?>