PHP中如何做分页显示

<?php
header("content-type:text/html;charset=utf-8");
$currentpage = 1;
if(isset($_GET['page']))
    $currentpage = $_GET['page'];

//连接数据库
$link = mysql_connect("localhost","root","") or die('连接失败');
mysql_select_db('myschool');
mysql_query('set names utf8');


$sql ="SELECT count(*) as 'count' from student";//查询记录的sql语句
$result = mysql_query($sql);
$arr = mysql_fetch_array($result);
$count = $arr['count'];
$pagesize = 3;
$pages = ceil($count/$pagesize);//共多少页


$prepage = $currentpage -1;
if($prepage<=0)
 $prepage=1;


$nextpage = $currentpage+1;
if($nextpage >= $pages){
$nextpage = $pages;
}


$start =($currentpage-1) * $pagesize;//起始位置
$sql = "SELECT * from student limit $start,$pagesize";
echo $sql;


// $sql = "select * from student";
$result = mysql_query($sql);
?>

//html部分
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<table border="1">
<tr>
<td>学号</td>
<td>姓名</td>
<td>性别</td>
<td>年龄</td>
</tr>
<?php while($arr=mysql_fetch_array($result)){ ?>
<td><?php echo $arr['number']; ?></td>
<td><?php echo $arr['name']; ?></td>
<td><?php echo $arr['sex']; ?></td>
<td><?php echo $arr['age']; ?></td>
</tr>
<?php } ?>
</table>
<a href="<?php echo $_SERVER['PHP_SELF'].'?page='.$prepage; ?>">上一页</a>&nbsp;&nbsp;<a href="<?php echo $_SERVER['PHP_SELF'].'?page='.$nextpage; ?>">下一页</a>
</body>

</html>

//当一个文件中有php和html两种时,php文件必须有结束标记

猜你喜欢

转载自blog.csdn.net/lixiaotong_123/article/details/79620228
今日推荐