view\list.html
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>学生信息列表</title>
<link rel="stylesheet" href="./css/common.css" />
</head>
<body>
<div class="box list">
<div class="title">学生信息列表</div>
<div class="search"><form>快速查询:<input type="text" name="search"/> <input type="submit" value="提交"/></form></div>
<table>
<tr>
<?php
//反转排序值
$order_sort = ($order_sort=='desc'?'asc':'desc');
?>
<th width="5%"><a href="?order=id&sort=<?php echo $order_sort; ?>">ID</a></th>
<th><a href="?order=name&sort=<?php echo $order_sort; ?>">姓名</a></th>
<th><a href="?order=dept_id&sort=<?php echo $order_sort; ?>">所属学院</a></th>
<th><a href="?order=birth&sort=<?php echo $order_sort; ?>">出生日期</a></th>
<th><a href="?order=entry&sort=<?php echo $order_sort; ?>">入职时间</a></th>
<th width="25%">相关操作</th>
</tr>
<?php if(empty($emp_info)): ?>
<tr><td colspan="6">查询的结果不存在!</td></tr>
<?php else: foreach($emp_info as $v): ?>
<tr>
<td><?php echo $v['stu_id']; ?></td>
<td><?php echo $v['stu_name']; ?></td>
<td><?php echo $v['dept_name']; ?></td>
<td><?php echo date('Y-m-d',strtotime($v['stu_birth'])); ?></td>
<td><?php echo date('Y-m-d',strtotime($v['stu_entry'])); ?></td>
<td>
<a class="icon icon-edit" href="emp_edit.php?id=<?php echo $v['stu_id']; ?>">编辑</a>
<a class="icon icon-del" href="emp_del.php?id=<?php echo $v['stu_id']; ?>">删除</a>
</td>
</tr>
<?php endforeach; endif; ?>
</table>
<div class="page">
<?php echo $page_html ?>
</div>
<div class="action">
<a href="emp_add.php">添加学生</a><a href="dept.php">学院管理</a>
</div>
</div>
</body>
</html>
4、学生列表排序功能
在index.php 中已经实现
5. 学生搜索功能
在index.php中已经实现
6.学生列表分页功能
放在/lib/,文件名为page_function.php
<?php
/**
* 分页链接生成函数
* @param int $page URL传递的page值
* @param int $max_page 最大页码值
*/
function makePageHtml($page,$max_page){
//删除GET参数中的page
unset($_GET['page']);
//重新生成URL中的GET参数
$params = http_build_query($_GET);
if($params){
$params .= '&';
}
//计算下一页
$next_page = $page +1;
//判断下一页的页码是否大于最大页码值,如果大于则把最大页码值赋值给它
if($next_page > $max_page) $next_page = $max_page;
//计算上一页
$prev_page = $page - 1;
//判断上一页的页码是否小于1,如果小于则把1赋值给它
if($prev_page < 1 ) $prev_page = 1;
//重新拼接分页链接的html代码
$page_html = '<a href="?'.$params.'page=1">首页</a>';
$page_html .= '<a href="?'.$params.'page='.$prev_page.'">上一页</a>';
$page_html .= '<a href="?'.$params.'page='.$next_page.'">下一页</a>';
$page_html .= '<a href="?'.$params.'page='.$max_page.'">尾页</a>';
//返回分页链接
return $page_html;
}
7、学院管理功能
文件名为dept.php 和 dept.html
<?php
//学院管理功能
require './init.php'; //项目初始化文件
$action = input_get('action'); //获取操作参数
//学院名称修改
if($action=='rename'){
$id = (int)input_get('id');
if($_POST){
$rename_new = input_post('dept_name'); //接收新名称
$rename_new = db_escape($link,filter($rename_new)); //数据过滤
//判断名称是否重复
$sql = "select dept_id from stu_dept where dept_name='$rename_new'";
$rename_id = db_fetch_column($link,$sql);
if($rename_id && $rename_id!=$id){
alert_back('该学院名称已经存在!');
}
$sql = "update stu_dept set dept_name='$rename_new' where dept_id=$id";
db_query($link,$sql);
header('Location:dept.php'); //执行后清除参数
exit;
}
$sql = "select dept_name from stu_dept where dept_id=$id";
$rename = db_fetch_column($link,$sql);
//添加学院
}elseif($action=='add'){
if($_POST){
$add_new = input_post('dept_name'); //接收新名称
$add_new = db_escape($link,filter($add_new)); //数据过滤
//判断名称是否重复
$sql = "select dept_id from stu_dept where dept_name='$add_new'";
$add_id = db_fetch_column($link,$sql);
if($add_id){
alert_back('该学院名称已经存在!');
}
$sql = "insert into stu_dept (dept_id,dept_name) values (null,'$add_new')";
db_query($link,$sql);
header('Location:dept.php'); //执行后清除参数
exit;
}
//删除学院
}elseif($action=='del'){
$id = (int)input_get('id');
$sql = "delete from stu_dept where dept_id=$id";
db_query($link,$sql);
header('Location:dept.php');
exit;
}
//查询学院列表
$sql = "select * from stu_dept";
$dept_info = db_fetch_all($link,$sql);
//加载视图页面,显示数据
require './view/dept.html';
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>学院管理</title>
<link rel="stylesheet" href="./css/common.css" />
</head>
<body>
<div class="box list fixed">
<div class="title">学院管理</div>
<form method="post">
<table>
<tr><th>ID</th><th>学院名称</th><th width="140">操作</th></tr>
<?php foreach($dept_info as $v): ?>
<tr><td><?php echo $v['dept_id'] ?></td><td><?php echo $v['dept_name'] ?></td>
<td><a class="icon icon-edit" href="?action=rename&id=<?php echo $v['dept_id'] ?>">修改</a><a class="icon icon-del" href="?action=del&id=<?php echo $v['dept_id'] ?>">删除</a></td></tr>
<?php endforeach; ?>
</table>
</form>
<div class="action">
<form method="post" action="?action=add">
<div>添加学院:<input type="text" name="dept_name" /> <input type="submit" value="提交" /></div>
</form>
<?php if($action=="rename"): ?>
<form method="post" action="?action=rename&id=<?php echo $id; ?>">
<p>修改学院名称:<input type="text" name="dept_name" value="<?php echo $rename; ?>" /> <input type="submit" value="提交" /></p>
</form>
<?php endif; ?>
<div class="right"><a href="./index.php">返回学生列表</a></div>
</div>
</div>
</body>
</html>
8、学生添加功能
<?php
//学生添加功能
require './init.php'; //项目初始化文件
//表单处理
if($_POST){
//定义允许的字段
$fields = array('stu_name','stu_dept_id','stu_birth','stu_entry');
$values = array();
foreach($fields as $k=>$v){
$data = input_post($v); //接收$_POST数据
$data = db_escape($link,filter($data)); //数据过滤
if($data==''){
alert_back($v.'字段不能为空');
}
$fields[$k] = "`$v`"; //把字段使用反引号包裹
$values[] = "'$data'"; //把值使用单引号包裹
}
$fields = implode(',', $fields);
$values = implode(',', $values);
$sql = "insert into stu_info ($fields) values ($values)";
//执行SQL
if($res = db_query($link,$sql)){
header('location: ./index.php');
die;
}else{
//执行失败
alert_back('学生添加失败!');
}
}
//获取学院信息
$sql = "select * from stu_dept";
$dept_info = db_fetch_all($link,$sql);
//加载视图页面,显示数据
require './view/stu_add.html';
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>学生信息添加</title>
<link rel="stylesheet" href="./css/common.css" />
<link rel="stylesheet" href="./js/jquery.datetimepicker.css" />
<script src="./js/jquery.js"></script>
<script src="./js/jquery.datetimepicker.js"></script>
<script>
//jQuery日历插件
$(function(){
var options = {lang:'ch',format:'Y-m-d',timepicker:false};
$('#birth').datetimepicker(options);
$('#entry').datetimepicker(options);
});
</script>
</head>
<body class="profile">
<div class="main">
<h1>添加学生</h1>
<form method="post">
<table>
<tr><th>姓名:</th><td><input type="text" name="stu_name" /></td></tr>
<tr><th>所属学院:</th><td><select name="stu_dept_id">
<option value="0">未选择</option>
<?php foreach($dept_info as $v): ?>
<option value="<?php echo $v['dept_id']; ?>" ><?php echo $v['dept_name'] ?></option>
<?php endforeach; ?>
</select></td></tr>
<tr><th>出生年月:</th><td><input id="birth" name="stu_birth" type="text"></td></tr>
<tr><th>入学日期:</th><td><input id="entry" name="stu_entry" type="text"></td></tr>
<tr><td colspan="2" class="td-btn">
<input type="submit" value="保存资料" class="button" />
<input type="button" value="返回列表" class="button" onclick="location.href='index.php'" />
</td></tr>
</table>
</form>
</div>
</body>
</html>
9、学生信息修改功能
<?php
//学生信息编辑功能
require './init.php'; //项目初始化文件
$id = (int)input_get('id');
//表单处理
if($_POST){
//定义允许的字段
$allow_fields = array('stu_name','stu_dept_id','stu_birth','stu_entry');
//获取员工更新数据表单
$update = array();
foreach($allow_fields as $v){
$data = input_post($v); //接收$_POST数据
$data = db_escape($link,filter($data)); //数据过滤
if($data==''){
alert_back($v.'字段不能为空');
}
//把键和值按照sql更新语句中的语法要求连接,并存入到$update数组中
$update[] = "`$v`='$data'";
}
//把$update数组元素使用逗号连接
$update_sql = implode(',', $update);
$sql = "update stu_info set $update_sql where stu_id=$id";
if($res = db_query($link,$sql)){
header('Location: index.php');
die;
}else{
alert_back('员工信息修改失败');
}
}
//获取员工原来的信息
$sql = "select * from stu_info left join stu_dept on stu_dept_id=dept_id where stu_id=$id";
$emp_info = db_fetch_row($link,$sql);
//获取部门信息
$sql = "select * from stu_dept";
$dept_info = db_fetch_all($link,$sql);
//加载视图页面,显示数据
require './view/stu_edit.html';
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>学生信息编辑</title>
<link rel="stylesheet" href="./css/common.css" />
<link rel="stylesheet" href="./js/jquery.datetimepicker.css" />
<script src="./js/jquery.js"></script>
<script src="./js/jquery.datetimepicker.js"></script>
<script>
//jQuery日历插件
$(function(){
var options = {lang:'ch',format:'Y-m-d',timepicker:false};
$('#birth').datetimepicker(options);
$('#entry').datetimepicker(options);
});
</script>
</head>
<body class="profile">
<div class="main">
<h1>编辑学生</h1>
<form method="post">
<table>
<tr><th>姓名:</th><td><input type="text" name="stu_name" value="<?php echo $emp_info['stu_name']; ?>" /></td></tr>
<tr><th>所属学院:</th><td><select name="stu_dept_id">
<option value="0">未选择</option>
<?php foreach($dept_info as $v): ?>
<option value="<?php echo $v['dept_id']; ?>" <?php if($v['dept_id']==$emp_info['stu_dept_id']) echo "selected"; ?> ><?php echo $v['dept_name'] ?></option>
<?php endforeach; ?>
</select></td></tr>
<tr><th>出生年月:</th><td><input id="birth" name="stu_birth" type="text" value="<?php echo date('Y-m-d',strtotime($emp_info['stu_birth'])); ?>"></td></tr>
<tr><th>入学日期:</th><td><input id="entry" name="stu_entry" type="text" value="<?php echo date('Y-m-d',strtotime($emp_info['stu_entry'])); ?>"></td></tr>
<tr><td colspan="2" class="td-btn">
<input type="submit" value="保存资料" class="button" />
<input type="button" value="返回列表" class="button" onclick="location.href='index.php'" />
</td></tr>
</table>
</form>
</div>
</body>
</html>
10、学生信息 删除功能
<?php
//学生信息删除功能
require './init.php'; //项目初始化文件
$id = (int)input_get('id');
$sql = "delete from stu_info where stu_id=$id";
db_query($link,$sql);
//删除后返回学生列表
header('Location:index.php');