PHP小项目之学生信息管理
程序员文章站
2022-07-14 22:32:23
...
一、学习目标
- 了解PHP语法基础使用规则,如PHP标记、标识符、变量、常量等
- 熟悉PHP中的数据类型及分类7运算符及优先级的运用
- 熟悉掌握选择结构语句、循环结构语句以及标签语法的应用
- 熟悉掌握函数、数组及包含语句在开发中定义及使用
博文主要展示源码,更多的是边学边做,做中学。
二、运行效果
三、知识点
四、源代码
//stu_list.php
<?php
include "./page.php";
$info = array(
array("name"=>"张三","birth"=>"1998-05-16","subject"=>"PHP","snum"=>"245678123"),
array("name"=>"李四","birth"=>"1996-08-15","subject"=>"PHP","snum"=>"155778723"),
array("name"=>"王二","birth"=>"2000-06-12","subject"=>"PHP","snum"=>"078978993"),
array("name"=>"刘一","birth"=>"1997-05-06","subject"=>"PHP","snum"=>"045468882"),
array("name"=>"五五","birth"=>"1998-05-16","subject"=>"PHP","snum"=>"245678773"),
array("name"=>"唐四","birth"=>"1999-08-15","subject"=>"PHP","snum"=>"155778153"),
array("name"=>"王二","birth"=>"1999-06-12","subject"=>"PHP","snum"=>"548768143"),
array("name"=>"齐一","birth"=>"1997-09-06","subject"=>"PHP","snum"=>"512168125"),
array("name"=>"苟三","birth"=>"1998-07-16","subject"=>"PHP","snum"=>"245678195"),
array("name"=>"杨四","birth"=>"1999-04-15","subject"=>"PHP","snum"=>"154568126"),
array("name"=>"李二","birth"=>"2000-01-15","subject"=>"PHP","snum"=>"078978151"),
array("name"=>"毛一","birth"=>"2000-07-28","subject"=>"PHP","snum"=>"515468123"),
array("name"=>"林三","birth"=>"2001-06-12","subject"=>"PHP","snum"=>"205678595"),
array("name"=>"宋四","birth"=>"1998-07-15","subject"=>"PHP","snum"=>"672145134"),
array("name"=>"杜二","birth"=>"1999-05-02","subject"=>"PHP","snum"=>"078887974"),
array("name"=>"王狗","birth"=>"1997-05-06","subject"=>"PHP","snum"=>"858795121"),
);
//总记录数 count用于计算数组中元素个数
$total_num = count($info);
//每页显示的条数
$perpage = 4;
//获取当前页
$Page = isset($_GET['page'])?(int)$_GET['page']:1;
//获取总页数 ceil向上取最近的整数
$total_page = ceil($total_num/$perpage);
//判断当前页是否小于1
$page = max($Page,1);
//判断当前页是否大于总页数
$page = min($page,$total_page);
//获取遍历学生数组时,每页开始的数组坐标值
$start_index = $perpage * ($page-1);
//获取遍历学生数组时,每页最大的数组坐标值
$end_index = $perpage * $page - 1;
//防止计算结果超过最大记录数
$end_index = min($end_index,$total_num-1);
?>
<style type="text/css">
#foot{
margin: 0 auto;
text-align: center;
}
table{
width: 50%;
height: 50%;
margin: 0 auto;
}
th,td{
background-color: #00CC99;
text-align: center;
}
th{
font-size: 18px;
color: #996600;
font-style:italic;
}</style>
<table>
<tr>
<th>学号</th>
<th>姓名</th>
<th>出生日期</th>
<th>详情</th>
</tr>
<?php for ($i=$start_index; $i<=$end_index ; $i++) { ?>
<tr>
<td> <?php echo $info[$i]['snum']; ?></td>
<td> <?php echo $info[$i]['name']; ?></td>
<td> <?php echo $info[$i]['birth']; ?></td>
<td> <a href="#">点击查看详情</a></td>
</tr>
<?php } ?>
</table>
<div id="foot"> <?php echo showPage($page,$total_page);?> </div>
<?php
/**
* 分页链接生成函数
* @param int $page 当前访问的页码
* @param int $total_page 总页码
* @param String 拼接好的url地址
* page.php
*/
function showPage($page,$total_page){
//拼接首页链接
$html = '<a href="?page=1">【首页】</a>';
//拼接上一页链接
$pre_page = $page-1 <= 0 ? $page : ($page - 1);
$html .= '<a href="?page='.$pre_page.'">【上一页】</a>';
//拼接下一页链接
$next_page = $page+1 > $total_page ? $page : ($page+1);
$html .= '<a href="?page='.$next_page.'">【下一页】</a>';
//拼接“尾页”链接
$html .='<a href="?page='.$total_page.'">【尾页】</a>';
//返回拼接后的分页链接
return $html;
}
?>
上一篇: Django 获取请求参数
下一篇: SpringMvc获取请求参数