Affichage des première et dernière pages de pagination développée par PHP+Mysql
<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;
//连接数据库
$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>";
}
//释放结果
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="";
//计算偏移量
if($page>1){
$page_banner.="<a href='".$_SERVER['PHP_SELF']."?p=1'>首页</a>";
$page_banner.="<a href='".$_SERVER['PHP_SELF']."?p=".($page-1)."'>上一页</a>";
}
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>
Faites sauter la barre de pagination plus facilement en ajoutant la première et la dernière page
Explication du code :
if($page>1){
$page_banner.="<a href='".$_SERVER['PHP_SELF']."?p=1'>首页</a>";
$page_banner.="<a href='".$_SERVER['PHP_SELF']."?p=".($page-1)."'>上一页</a>";
}Affiché lorsque $page>1 Page d'accueil et page précédente
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>";
}Lorsque le nombre de pages est inférieur au nombre total de pages, affichez la page suivante et la dernière
$page_banner.="共{$to_pages}页";
echo $page_banner;affiche le nombre total de pages.
nouveau fichier
<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;
//连接数据库
$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>";
}
//释放结果
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="";
//计算偏移量
if($page>1){
$page_banner.="<a href='".$_SERVER['PHP_SELF']."?p=1'>首页</a>";
$page_banner.="<a href='".$_SERVER['PHP_SELF']."?p=".($page-1)."'>上一页</a>";
}
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>
Aperçu
Clear
Les étudiants qui ont regardé ce cours apprennent également
Parlons brièvement de la création d'une entreprise en PHP
Introduction rapide au développement web front-end
Développement pratique à grande échelle par Tianlongbabu du cadre MVC version Mini imitant le site Web de l'encyclopédie des choses embarrassantes
Premiers pas avec le développement pratique PHP : création rapide de PHP [Small Business Forum]
Vérification de connexion et forum de discussion classique
Collecte de connaissances sur les réseaux informatiques
Démarrage rapide de la version complète de Node.JS
Le cours front-end qui vous comprend le mieux : HTML5/CSS3/ES6/NPM/Vue/...[Original]
Écrivez votre propre framework PHP MVC (40 chapitres en profondeur/gros détails/à lire absolument pour que les débutants progressent)
















Le didacticiel n'est pas disponible au téléchargement pour le moment. Le staff est actuellement en train de l'organiser. Veuillez prêter plus d'attention à ce cours à l'avenir ~ 