无JS,完全php面向过程数据分页实现代码
程序员文章站
2022-11-27 18:38:35
复制代码 代码如下:
<?php
//登陆页面设置的session,当session中存在name时
//session_start();
//$name = $_session['name'];
//if (empty($name)){
// header("location: error.php");
// exit();
//}
//面向过程, 数据分页显示
if(false!=($mysql = mysql_connect('本地mysql', 'mysql用户名', 'mysql密码'))){
mysql_query('set names utf8',$mysql); //设置数据库中的编码
mysql_select_db("数据库database",$mysql);
}else{
die("连接失败");
}
$pagesize = 10; //页面显示条数
$rowcount = 0; //数据总条数,从数据库获得
$sqlcount = 'select count(id) from employee';
$res1 = mysql_query($sqlcount,$mysql);
//取出数据条数
if(false!=($row=mysql_fetch_row($res1))){
$rowcount = $row[0];
}
//总页数,通过计算得到
$pagecount = 0;
$pagecount = ceil($rowcount/$pagesize);
//获取当前页
if(!isset($_get['pagenow'])){ // 当 get/post都为空的时候赋默认值1
$pagenow = 1; //当前页数
}elseif(false!=is_numeric($_get['pagenow']) && $_get['pagenow']<=$pagecount){
$pagenow = $_get['pagenow'];
}else{
header("location: ../error/error.php");
exit();
}
//打印分页数据
echo "<div style='margin-left:300px;margin-top:1px;'>";
echo "<table style='border:1px;border-style:solid;border-width:1px;border-color:green'>";
echo "<tr><th>id</th> <th>name</th> <th>age</th> <th>sex</th> <th>birthday</th> <th>编辑雇员</th> <th>删除雇员</th> </tr>";
$sqlist = "select id,name,age,sex,birthday from employee limit ".($pagenow-1)*$pagesize.",".$pagesize;
$res2 = mysql_query($sqlist,$mysql);
while (false!=($row=mysql_fetch_assoc($res2))){
echo "<tr><td>{$row['id']}</td> <td>{$row['name']}</td> <td>{$row['age']}</td> <td>{$row['sex']}</td> <td>{$row['birthday']}</td> <td><a href=#>编辑</a></td> <td><a href=#>删除</a></td></tr>";
}
echo "</table>";
//表单控制显示页数
echo "<form action='当前页'>";
//上一页按钮
if($pagenow>1){
$pageup = $pagenow-1;
echo "<a href='?pagenow=".$pageup."'>上一页</a> ";
}
//下一页按钮
if($pagenow<$pagecount){
$pagedown = $pagenow+1;
echo "<a href='?pagenow=".$pagedown."'>下一页</a> <br/>";
}
//后退十页按钮
if($pagenow-10>0){
echo "<a href='?pagenow=".($pagenow-10)."'><<<</a> ";
}
//向本页传递当前显示的页数,并显示第几页按钮
for($i=1;$i<=$pagecount;$i++){
if($i>$pagenow-2 && $i<$pagenow+6){
if($i!=$pagenow){
echo "<a href='?pagenow=".$i."'>第".$i."页</a> ";
}
}
}
//前进十页
if($pagenow+10<=$pagecount){
echo "<a href='?pagenow=".($pagenow+10)."'>>>></a> ";
}
//显示当前页与总页数
echo "<br/>当前页".$pagenow."页/共".$pagecount."页";
//跳转页
echo "跳转到:<input type='text' name='pagenow' id='pagenow' style='width:30px;height:20px'/>页<input type='submit' style='width:37px;height:20px;font-size:11px;' value='go'/>";
echo "</form>";
echo "</div>";
?>
复制代码 代码如下:
<?php
//登陆页面设置的session,当session中存在name时
//session_start();
//$name = $_session['name'];
//if (empty($name)){
// header("location: error.php");
// exit();
//}
//面向过程, 数据分页显示
if(false!=($mysql = mysql_connect('本地mysql', 'mysql用户名', 'mysql密码'))){
mysql_query('set names utf8',$mysql); //设置数据库中的编码
mysql_select_db("数据库database",$mysql);
}else{
die("连接失败");
}
$pagesize = 10; //页面显示条数
$rowcount = 0; //数据总条数,从数据库获得
$sqlcount = 'select count(id) from employee';
$res1 = mysql_query($sqlcount,$mysql);
//取出数据条数
if(false!=($row=mysql_fetch_row($res1))){
$rowcount = $row[0];
}
//总页数,通过计算得到
$pagecount = 0;
$pagecount = ceil($rowcount/$pagesize);
//获取当前页
if(!isset($_get['pagenow'])){ // 当 get/post都为空的时候赋默认值1
$pagenow = 1; //当前页数
}elseif(false!=is_numeric($_get['pagenow']) && $_get['pagenow']<=$pagecount){
$pagenow = $_get['pagenow'];
}else{
header("location: ../error/error.php");
exit();
}
//打印分页数据
echo "<div style='margin-left:300px;margin-top:1px;'>";
echo "<table style='border:1px;border-style:solid;border-width:1px;border-color:green'>";
echo "<tr><th>id</th> <th>name</th> <th>age</th> <th>sex</th> <th>birthday</th> <th>编辑雇员</th> <th>删除雇员</th> </tr>";
$sqlist = "select id,name,age,sex,birthday from employee limit ".($pagenow-1)*$pagesize.",".$pagesize;
$res2 = mysql_query($sqlist,$mysql);
while (false!=($row=mysql_fetch_assoc($res2))){
echo "<tr><td>{$row['id']}</td> <td>{$row['name']}</td> <td>{$row['age']}</td> <td>{$row['sex']}</td> <td>{$row['birthday']}</td> <td><a href=#>编辑</a></td> <td><a href=#>删除</a></td></tr>";
}
echo "</table>";
//表单控制显示页数
echo "<form action='当前页'>";
//上一页按钮
if($pagenow>1){
$pageup = $pagenow-1;
echo "<a href='?pagenow=".$pageup."'>上一页</a> ";
}
//下一页按钮
if($pagenow<$pagecount){
$pagedown = $pagenow+1;
echo "<a href='?pagenow=".$pagedown."'>下一页</a> <br/>";
}
//后退十页按钮
if($pagenow-10>0){
echo "<a href='?pagenow=".($pagenow-10)."'><<<</a> ";
}
//向本页传递当前显示的页数,并显示第几页按钮
for($i=1;$i<=$pagecount;$i++){
if($i>$pagenow-2 && $i<$pagenow+6){
if($i!=$pagenow){
echo "<a href='?pagenow=".$i."'>第".$i."页</a> ";
}
}
}
//前进十页
if($pagenow+10<=$pagecount){
echo "<a href='?pagenow=".($pagenow+10)."'>>>></a> ";
}
//显示当前页与总页数
echo "<br/>当前页".$pagenow."页/共".$pagecount."页";
//跳转页
echo "跳转到:<input type='text' name='pagenow' id='pagenow' style='width:30px;height:20px'/>页<input type='submit' style='width:37px;height:20px;font-size:11px;' value='go'/>";
echo "</form>";
echo "</div>";
?>