PHP+Mysql development of page number paging
<html>
<head>
<meta http-equiv="CONTENT-TYPE" content="text/html;">
</head>
<body>
<?php
/** 1.传入页面 **/
$page = $_GET['p'];
/** 2.根据页面取出数据:php->mysql **/
$host = "localhost";
$username = 'root';
$password = '123456789';
$db = 'bbs2';
$PageSize=5;
$ShowPage=3;
//连接数据库
$conn = mysql_connect($host, $username, $password);
if(!$conn){
echo "数据库连接失败";
exit;
}
//选择所要操作的数据库
mysql_select_db($db);
//设置数据库编码格式
mysql_query('SET NAMES UTF8');
//编写sql获取分页数据:SELECT * FROM 表名 LIMIT 起始位置 , 显示条数
$sql = "SELECT*FROM test LIMIT ".($page-1)*$PageSize .",$PageSize";
if(!$sql){
echo "取出不成功";
};
//把sql语句传送到数据库
$result = mysql_query($sql);
//处理我们的数据
echo "<table border=1 cellspacing=0 width=15% >";
echo "<tr><td>ID</td><td>名字</td><td>性别</td></tr>";
while($row = mysql_fetch_assoc($result)){
echo "<tr>";
echo "<td>{$row['id']}</td>";
echo "<td>{$row['name']}</td>";
echo "<td>{$row['sex']}</td>";
echo "<tr>";
}
echo "</table>";
//释放结果
mysql_free_result($result);
//获取数据总数
$to_sql="SELECT COUNT(*)FROM test";
$to_result=mysql_fetch_array(mysql_query($to_sql));
$to=$to_result[0];
//计算页数
$to_pages=ceil($to/$PageSize);
mysql_close($conn);
/** 3.显示数据+分页条 **/
$page_banner="";
//计算偏移量
$pageffset=($ShowPage-1)/2;
if($page>1){
$page_banner.="<a href='".$_SERVER['PHP_SELF']."?p=1'>首页</a>";
$page_banner.="<a href='".$_SERVER['PHP_SELF']."?p=".($page-1)."'>上一页</a>";
}
//初始化数据
$start=1;
$end=$to_pages;
if ($to_pages>$ShowPage){
if($page>$pageffset+1){
$page_banner.="...";
}
if ($page>$pageffset){
$start=$page-$pageffset;
$end=$to_pages>$page+$pageffset?$page+$pageffset:$to_pages;
}else{
$start=1;
$end=$to_pages>$ShowPage?$ShowPage:$to_pages;
}
if ($page+$pageffset>$to_pages){
$start=$start-($page+$pageffset-$end);
}
}
for($i=$start;$i<=$end;$i++){
$page_banner.="<a href='".$_SERVER['PHP_SELF']."?p=".($i)."'>{$i}</a>";
}
//尾部省略
if ($to_pages>$ShowPage&&$to_pages>$page+$pageffset){
$page_banner.="...";
}
if ($page<$to_pages){
$page_banner.="<a href='".$_SERVER['PHP_SELF']."?p=".($page+1)."'>下一页</a>";
$page_banner.="<a href='".$_SERVER['PHP_SELF']."?p=".($to_pages)."'>尾页</a>";
}
$page_banner.="共{$to_pages}页";
echo $page_banner;
?>
</body>
</html>When there is too much data, the page number will also be too long. At this time, it is necessary to hide the page number to achieve a beautiful effect
Code explanation

$start=1;
$end=$to_pages;
if ($to_pages>$ShowPage){
if($page>$pageffset+1){
$page_banner.="...";
}The total number of pages is greater than the displayed number of pages, and the current page is greater than the offset + 1 display...Omitted if ($page>$pageffset){
$start=$page-$pageffset;
$end=$to_pages>$page+$pageffset?$page+$pageffset:$to_pages;$start starting position if the current page is greater than the offset Shift, then the starting position displays the current page minus the offset $end position. If the total number of pages is greater than the current page plus the offset, then the end position is the current page plus the offset. If not If it is greater, the last one will be displayed else{
$start=1;
$end=$to_pages>$ShowPage?$ShowPage:$to_pages;
}If the current page is less than the offset, the starting position is 1 if ($page+$pageffset>$to_pages){
$start=$start-($page+$pageffset-$end); If the page number plus offset is greater than the last page, the starting position is equal to the original starting position minus the current page plus offset minus Go to the end position. }
for($i=$start;$i<=$end;$i++){
$page_banner.="<a href='".$_SERVER['PHP_SELF']."?p=".($i)."'>{$i}</a>";
}Display page number//尾部省略
if ($to_pages>$ShowPage&&$to_pages>$page+$pageffset){
$page_banner.="...";
}When the total number of pages is greater than the displayed number of pages and the last page is greater than the current page plus the offset, it is omitted
new file
<html>
<head>
<meta http-equiv="CONTENT-TYPE" content="text/html;">
</head>
<body>
<?php
/** 1.传入页面 **/
$page = $_GET['p'];
/** 2.根据页面取出数据:php->mysql **/
$host = "localhost";
$username = 'root';
$password = '123456789';
$db = 'bbs2';
$PageSize=5;
$ShowPage=3;
//连接数据库
$conn = mysql_connect($host, $username, $password);
if(!$conn){
// echo "数据库连接失败";
exit;
}
//选择所要操作的数据库
mysql_select_db($db);
//设置数据库编码格式
mysql_query('SET NAMES UTF8');
//编写sql获取分页数据:SELECT * FROM 表名 LIMIT 起始位置 , 显示条数
$sql = "SELECT*FROM test LIMIT ".($page-1)*$PageSize .",$PageSize";
if(!$sql){
echo "取出不成功";
};
//把sql语句传送到数据库
$result = mysql_query($sql);
//处理我们的数据
echo "<table border=1 cellspacing=0 width=15% >";
echo "<tr><td>ID</td><td>名字</td><td>性别</td></tr>";
while($row = mysql_fetch_assoc($result)){
echo "<tr>";
echo "<td>{$row['id']}</td>";
echo "<td>{$row['name']}</td>";
echo "<td>{$row['sex']}</td>";
echo "<tr>";
}
echo "</table>";
//释放结果
mysql_free_result($result);
//获取数据总数
$to_sql="SELECT COUNT(*)FROM test";
$to_result=mysql_fetch_array(mysql_query($to_sql));
$to=$to_result[0];
//计算页数
$to_pages=ceil($to/$PageSize);
mysql_close($conn);
/** 3.显示数据+分页条 **/
$page_banner="";
//计算偏移量
$pageffset=($ShowPage-1)/2;
if($page>1){
$page_banner.="<a href='".$_SERVER['PHP_SELF']."?p=1'>首页</a>";
$page_banner.="<a href='".$_SERVER['PHP_SELF']."?p=".($page-1)."'>上一页</a>";
}
//初始化数据
$start=1;
$end=$to_pages;
if ($to_pages>$ShowPage){
if($page>$pageffset+1){
$page_banner.="...";
}
if ($page>$pageffset){
$start=$page-$pageffset;
$end=$to_pages>$page+$pageffset?$page+$pageffset:$to_pages;
}else{
$start=1;
$end=$to_pages>$ShowPage?$ShowPage:$to_pages;
}
if ($page+$pageffset>$to_pages){
$start=$start-($page+$pageffset-$end);
}
}
for($i=$start;$i<=$end;$i++){
$page_banner.="<a href='".$_SERVER['PHP_SELF']."?p=".($i)."'>{$i}</a>";
}
//尾部省略
if ($to_pages>$ShowPage&&$to_pages>$page+$pageffset){
$page_banner.="...";
}
if ($page<$to_pages){
$page_banner.="<a href='".$_SERVER['PHP_SELF']."?p=".($page+1)."'>下一页</a>";
$page_banner.="<a href='".$_SERVER['PHP_SELF']."?p=".($to_pages)."'>尾页</a>";
}
$page_banner.="共{$to_pages}页";
echo $page_banner;
?>
</body>
</html>
Preview
Clear
- Course Recommendations
- Courseware download
The courseware is not available for download at the moment. The staff is currently organizing it. Please pay more attention to this course in the future~
Students who have watched this course are also learning
Let's briefly talk about starting a business in PHP
Quick introduction to web front-end development
Large-scale practical Tianlongbabu development of Mini version MVC framework imitating the encyclopedia website of embarrassing things
Getting Started with PHP Practical Development: PHP Quick Creation [Small Business Forum]
Login verification and classic message board
Computer network knowledge collection
Quick Start Node.JS Full Version
The front-end course that understands you best: HTML5/CSS3/ES6/NPM/Vue/...[Original]
Write your own PHP MVC framework (40 chapters in depth/big details/must read for newbies to advance)













