1、界面制作(为了方便起见,这里我采用了Bootstrap框架制作界面);
2、数据库设计,正确创建students表;
admin表:
3、项目目录结构如下:
LaPHP框架已经在上文中列出,这里就不再列出。
Home模块(提供“新增题目”和“修改试题”、查看所有学生题目的功能):
Application/Home/Config/config.php
<?php
return array(
//'配置项'=>'配置值'
'DB_HOST' => 'localhost', //设置主机
'DB_USER' => 'root', //设置用户名
'DB_PWD' => '', //设置密码
'DB_PORT' => '3306', //设置端口号
'DB_NAME' => 'mydb_329', //设置数据库名
/* 模板设置 */
'TMPL_TEMPLATE_SUFFIX' => '.php', // 默认模板文件后缀
);
?>
Application/Home/Controller/IndexController.class.php
<?php
class IndexController extends Controller {
function index() {
//echo "<p style='width: 50%; height: 300px; line-height: 300px; padding: 10px 20px; font-family:\"微软雅黑\", \"Microsoft YaHei\"; font-size: 30px; margin: 50px auto; box-shadow: 0 0 3px #ABCDEF; text-align: center; position: relative;'>Hello LabPHP!<span style='line-height: 30px; font-size: 20px; position: absolute; bottom: 20px; right: 20px;'>欢迎使用LapPHP V1.0.0 By DreamBoy<span></p>";
$students = M('Students'); //检查是否有错误提示
if($res = $students->check()) {
$this->assign('err', $res);
}
$this->display();
}
function doAction() {
$students = M('Students');
if($res = $students->checkForm()) { //验证表单字段
$this->assign('err', $res)->display('Index/index');
} else {
if($this->validate($_POST['action'])) { //根据action执行相应操作
if($_POST['action'] == 'add') {
if($students->add()) {
$this->assign('info', 'add')->show();
} else {
$this->redirect('Index/index', 'err=add');
}
} else if($_POST['action'] == 'modify') {
if($students->modify()) {
$this->assign('info', 'modify')->show();
} else {
$this->redirect('Index/index', 'err=modify');
}
}
}
}
}
//显示所有信息
function show() {
$students = M('Students');
$cur = 1; //当前页数第一页
if($this->validate($_GET['cur'])) {
$cur = $_GET['cur'];
}
$res = $students->page($cur);
$this->assign('res', $res)->assign('start', ($cur-1) * $students->getEachPageLen() + 1)->assign('prePage', $cur-1)->assign('nextPage', $cur+1)->display('Index/showStudent');
}
}
?>
Application/Home/Model/StudentsModel.class.php
<?php
class StudentsModel extends Model {
private static $err = array('sno'=>'学生学号不能为空!', 'name'=>'学生姓名不能为空!', 'psw'=>'修改密码不能为空', 'title'=>'题目标题不能为空!', 'add'=>'新增题目失败,原因可能为已新增过题目了,请尝试选择“修改试题”进行提交!', 'modify'=>'修改试题失败,原因可能为:1.未新增过题目;2.学生学号输入错误;3.修改密码输入错误!');
private $eachPageLen = 15;
/**
* 检查是否有错误提示
* @return [type] [description]
*/
function check() {
if($this->validate($_GET['err'])) {
return self::$err[$_GET['err']];
}
return '';
}
/**
* 检查新增题目或修改试题的提交过来的表单字段
* @return [type] [description]
*/
function checkForm() {
$required = array('sno'=>'学生学号不能为空!', 'name'=>'学生姓名不能为空!', 'psw'=>'修改密码不能为空', 'title'=>'题目标题不能为空!');
foreach ($required as $key => $value) {
if(!$this->validate($_POST[$key])) {
return $value;
}
}
return '';
}
//新增数据
function add() {
$sno = $_POST['sno'];
$name = $_POST['name'];
$psw = $_POST['psw'];
$title = $_POST['title'];
$partner = $_POST['partner'];
$sql = "INSERT INTO students(`sno`, `name`, `psw`, `title`, `last_time`, `partner`) VALUES('$sno', '$name', '$psw', '$title', now(), '$partner')";
$res = $this->execute_dml($sql);
if($res == 1) {
return 1; //新增题目成功
} else {
return 0; //新增题目失败
}
}
//修改数据
function modify() {
$sno = $_POST['sno'];
$psw = $_POST['psw'];
$title = $_POST['title'];
$partner = !isset($_POST['partner']) || empty($_POST['partner']) ? '' : $_POST['partner'];
$sql = "UPDATE students SET `title` = '$title', `partner` = '$partner' WHERE `sno` = '$sno' AND `psw` = '$psw'";
$res = $this->execute_dml($sql);
if($res == 1) {
return 1; //修改题目成功
} else {
return 0; //修改题目失败
}
}
//分页查询数据
function page($cur) {
$length = $this->eachPageLen;
$offset = ($cur - 1) * $length;
$sql = "SELECT * FROM students ORDER BY sno LIMIT $offset,$length";
return $arr = $this->execute_dql_arr($sql);
}
//得到每页的页数
function getEachPageLen() {
return $this->eachPageLen;
}
}
?>
Application/Home/View/Index/index.php
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- 上述3个meta标签*必须*放在最前面,任何其他内容都*必须*跟随其后! -->
<title>课程设计题目统计系统</title>
<!-- Bootstrap -->
<link href="Public/css/bootstrap.min.css" rel="stylesheet">
<link href="Public/style.css" rel="stylesheet" type="text/css"/>
<!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
<!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
<!--[if lt IE 9]>
<script src="//cdn.bootcss.com/html5shiv/3.7.2/html5shiv.min.js"></script>
<script src="//cdn.bootcss.com/respond.js/1.4.2/respond.min.js"></script>
<![endif]-->
</head>
<body>
<div class="container">
<div class="col-sm-3"></div>
<div class="col-sm-6 content">
<h2>课程设计题目统计系统</h2>
<form class="form-horizontal" action="index.php?a=doAction" method="POST">
<div class="form-group">
<label for="number" class="col-sm-3 control-label">学生学号:</label>
<div class="col-sm-9">
<input type="text" class="form-control" id="number" placeholder="输入12位学号" name="sno">
</div>
</div>
<div class="form-group">
<label for="name" class="col-sm-3 control-label">学生姓名:</label>
<div class="col-sm-9">
<input type="text" class="form-control" id="name" placeholder="姓名" name="name">
</div>
</div>
<div class="form-group">
<label for="password" class="col-sm-3 control-label">修改密码:</label>
<div class="col-sm-9">
<input type="password" class="form-control" id="password" placeholder="首次输入作为后面修改的密码" name="psw">
</div>
</div>
<div class="form-group">
<label for="title" class="col-sm-3 control-label">你的题目:</label>
<div class="col-sm-9">
<input type="text" class="form-control" id="title" placeholder="按照课程设计题目要求" name="title">
</div>
</div>
<div class="form-group">
<label for="name" class="col-sm-3 control-label">合作学生:</label>
<div class="col-sm-9">
<input type="text" class="form-control" id="name" placeholder="姓名,没有就空,只负责不同方面" name="partner">
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-3 col-sm-9">
<label>
<input type="radio" name="action" value="add" checked="checked"> 新增题目
</label>
<label>
<input type="radio" name="action" value="modify"> 修改试题
</label>
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-3 col-sm-9">
<button type="submit" class="btn btn-info">提交操作</button>
<a href="index.php?a=show">显示全部学生题目</a>
</div>
</div>
<?php
if($err) {
?>
<p class="info"><span style="font-weight: bold">错误:</span><?php echo $err;?></p>
<?php
}
?>
</form>
</div>
<div class="col-sm-3"></div>
</div>
<!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
<script src="Public/js/jquery.min.js"></script>
<!-- Include all compiled plugins (below), or include individual files as needed -->
<script src="Public/js/bootstrap.min.js"></script>
</body>
</html>
Application/Home/View/Index/showStudent.php
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- 上述3个meta标签*必须*放在最前面,任何其他内容都*必须*跟随其后! -->
<title>课程设计题目统计系统</title>
<!-- Bootstrap -->
<link href="Public/css/bootstrap.min.css" rel="stylesheet">
<link href="Public/style.css" rel="stylesheet" type="text/css"/>
<style>
p.info {
margin-bottom: 0;
}
</style>
<!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
<!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
<!--[if lt IE 9]>
<script src="//cdn.bootcss.com/html5shiv/3.7.2/html5shiv.min.js"></script>
<script src="//cdn.bootcss.com/respond.js/1.4.2/respond.min.js"></script>
<![endif]-->
</head>
<body>
<div class="container">
<div class="content">
<?php
if($info) {
if($info == 'add') {
$fo = '新增';
} else if($info == 'modify') {
$fo = '修改';
}
?>
<p class="info"><marquee behavior="scroll" direction="left"><span style="font-weight: bold">消息:</span><?php echo $fo . '题目成功!';?></marquee></p>
<?php
}
?>
<table class="table table-hover">
<caption><h3>学生课程设计题目</h3></caption>
<?php
if(!isset($res) || empty($res)) {
echo '<tr><td>暂无学生题目</td></tr>';
} else {
?>
<thead>
<tr>
<!-- <th>删除</th> -->
<th>序号</th>
<th>学号</th>
<th>姓名</th>
<th>题目</th>
<th>状态</th>
<th>录入时间</th>
<th>合作学生</th>
</tr>
</thead>
<tbody>
<?php
$i = $start ? $start : 1;
foreach ($res as $value) {
echo '<tr>';
echo "<th scope='row'>$i</th>";
echo "<td>{$value['sno']}</td>";
echo "<td>{$value['name']}</td>";
echo "<td>{$value['title']}</td>";
echo "<td>{$value['state']}</td>";
echo "<td>{$value['last_time']}</td>";
echo "<td>{$value['partner']}</td>";
echo '</tr>';
$i++;
}
?>
</tbody>
<?php
}
?>
</table>
<nav>
<ul class="pager">
<li>
<?php
if($prePage) {
if($prePage >= 1) {
echo "<a href='index.php?a=show&cur=$prePage'>上一页</a>";
}
}
?>
</li>
<li>
<?php
if($nextPage) {
if($nextPage >= 1) {
echo "<a href='index.php?a=show&cur=$nextPage'>下一页</a>";
}
}
?>
</li>
</ul>
</nav>
<a href="index.php">返回输入界面</a>
</div>
</div>
<!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
<script src="Public/js/jquery.min.js"></script>
<!-- Include all compiled plugins (below), or include individual files as needed -->
<script src="Public/js/bootstrap.min.js"></script>
</body>
</html>
Admin模块(提供管理员登陆和删除功能):
Application/Admin/Config/config.php
<?php
return array(
//'配置项'=>'配置值'
'DB_HOST' => 'localhost', //设置主机
'DB_USER' => 'root', //设置用户名
'DB_PWD' => '', //设置密码
'DB_PORT' => '3306', //设置端口号
'DB_NAME' => 'mydb_329', //设置数据库名
/* 模板设置 */
'TMPL_TEMPLATE_SUFFIX' => '.php', // 默认模板文件后缀
);
?>
Application/Admin/Controller/IndexController.class.php
<?php
class IndexController extends Controller {
function index() {
//echo "<p style='width: 50%; height: 300px; line-height: 300px; padding: 10px 20px; font-family:\"微软雅黑\", \"Microsoft YaHei\"; font-size: 30px; margin: 50px auto; box-shadow: 0 0 3px #ABCDEF; text-align: center; position: relative;'>Hello LabPHP!<span style='line-height: 30px; font-size: 20px; position: absolute; bottom: 20px; right: 20px;'>欢迎使用LapPHP V1.0.0 By DreamBoy<span></p>";
$this->display();
}
function check() {
$admin = M('Admin'); //检查是否有错误提示
if($res = $admin->check()) { //验证失败!有错误提示
$this->assign('err', $res);
$this->display('Index/index');
} else {
$this->redirect('Index/adminShow');
}
}
/**
* 验证是否登陆过
* @return boolean [description]
*/
function isLogin() {
session_start();
if(!$this->validate($_SESSION['account'])) {
$this->assign('err', '请先登录!')->display('Index/index');
}
}
//显示所有信息
function show() {
$this->isLogin();
$students = M('Students');
$cur = 1; //当前页数第一页
if($this->validate($_GET['cur'])) {
$cur = $_GET['cur'];
}
$res = $students->page($cur);
$this->assign('res', $res)->assign('start', ($cur-1) * $students->getEachPageLen() + 1)->assign('prePage', $cur-1)->assign('nextPage', $cur+1)->display('Index/showStudent');
}
//显示所有信息+删除操作
function adminShow() {
$this->isLogin();
$students = M('Students');
$cur = 1; //当前页数第一页
if($this->validate($_GET['cur'])) {
$cur = $_GET['cur'];
}
$res = $students->page($cur);
$this->assign('res', $res)->assign('start', ($cur-1) * $students->getEachPageLen() + 1)->assign('prePage', $cur-1)->assign('nextPage', $cur+1)->display('Index/admin');
}
//删除学生题目
function del() {
//print_r($_POST['sno']);
$admin = M('Admin');
$res = $admin->del();
if($res == 1) {
$this->assign('info', '删除成功!');
} else if($res == -1) {
$this->assign('info', '删除失败!');
} else if($res == 0) {
$this->assign('info', '没有删除任何数据');
}
$this->adminShow();
}
}
?>
Application/Admin/Model/AdminModel.class.php
<?php
class AdminModel extends Model {
private static $err = array('account'=>'管理员账号不能为空!', 'psw'=>'管理员密码不能为空!', 'none'=>'该账号不存在!', 'pswErr'=>'密码错误!');
function check() {
//验证管理员账号是否为空
if(!$this->validate(daddslashes($_POST['account']))) {
return self::$err['account'];
}
//验证管理员密码是否为空
if(!$this->validate(daddslashes($_POST['psw']))) {
return self::$err['psw'];
}
$account = daddslashes($_POST['account']);
$psw = daddslashes($_POST['psw']);
$sql = "SELECT `psw` FROM `admin` WHERE `account`='$account'";
$res = $this->execute_dql_arr($sql);
if($this->validate($res)) {
if($res[0]['psw'] != $psw) {
return self::$err['pswErr'];
}
} else {
return self::$err['none'];
}
//验证通过
session_start();
$_SESSION['account'] = $account;
return '';
}
//删除学生题目
function del() {
if($this->validate($_POST['sno'])) {
$snoArr = $_POST['sno']; //获取用户选择的sno数组
$snos = implode(',', $snoArr);
$sql = "DELETE FROM `students` WHERE `sno` IN ($snos)";
$res = $this->execute_dml($sql);
if($res == 1) {
return 1; //新增题目成功
} else {
return 0; //新增题目失败
}
} else {
return -1; //没有题目可删除
}
}
}
?>
Application/Admin/Model/StudentsModel.class.php
<?php
class StudentsModel extends Model {
private $eachPageLen = 15;
//分页查询数据
function page($cur) {
$length = $this->eachPageLen;
$offset = ($cur - 1) * $length;
$sql = "SELECT * FROM students ORDER BY sno LIMIT $offset,$length";
return $arr = $this->execute_dql_arr($sql);
}
//得到每页的页数
function getEachPageLen() {
return $this->eachPageLen;
}
}
?>
Application/Admin/View/Index/admin.php
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- 上述3个meta标签*必须*放在最前面,任何其他内容都*必须*跟随其后! -->
<title>课程设计题目统计后台管理系统</title>
<!-- Bootstrap -->
<link href="Public/css/bootstrap.min.css" rel="stylesheet">
<link href="Public/style.css" rel="stylesheet" type="text/css"/>
<style>
p.info {
margin-bottom: 0;
}
</style>
<!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
<!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
<!--[if lt IE 9]>
<script src="//cdn.bootcss.com/html5shiv/3.7.2/html5shiv.min.js"></script>
<script src="//cdn.bootcss.com/respond.js/1.4.2/respond.min.js"></script>
<![endif]-->
</head>
<body>
<div class="container">
<div class="content">
<?php
if($info) {
?>
<p class="info"><marquee behavior="scroll" direction="left"><span style="font-weight: bold">消息:</span><?php echo $info;?></marquee></p>
<?php
}
?>
<form action="index.php?m=Admin&a=del" method="POST">
<table class="table table-hover">
<caption><h3>学生课程设计题目</h3></caption>
<?php
if(!isset($res) || empty($res)) {
echo '<tr><td>暂无学生题目</td></tr>';
} else {
?>
<thead>
<tr>
<th>删除</th>
<th>序号</th>
<th>学号</th>
<th>姓名</th>
<th>题目</th>
<th>状态</th>
<th>录入时间</th>
<th>合作学生</th>
</tr>
</thead>
<tbody>
<?php
$i = $start ? $start : 1;
foreach ($res as $value) {
echo '<tr>';
echo '<td><input type="checkbox" name="sno[]" value="' . $value['sno'] . '"/></td>';
echo "<th scope='row'>$i</th>";
echo "<td>{$value['sno']}</td>";
echo "<td>{$value['name']}</td>";
echo "<td>{$value['title']}</td>";
echo "<td>{$value['state']}</td>";
echo "<td>{$value['last_time']}</td>";
echo "<td>{$value['partner']}</td>";
echo '</tr>';
$i++;
}
?>
</tbody>
<?php
}
?>
</table>
<nav>
<ul class="pager">
<li>
<?php
if($prePage) {
if($prePage >= 1) {
echo "<a href='index.php?m=Admin&a=adminShow&cur=$prePage'>上一页</a>";
}
}
?>
</li>
<li>
<?php
if($nextPage) {
if($nextPage >= 1) {
echo "<a href='index.php?m=Admin&a=adminShow&cur=$nextPage'>下一页</a>";
}
}
?>
</li>
</ul>
</nav>
<a href="index.php?m=Admin">返回登陆界面</a> <input class="btn btn-default" type="submit" value="确认删除"/>
</form>
</div>
</div>
<!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
<script src="Public/js/jquery.min.js"></script>
<!-- Include all compiled plugins (below), or include individual files as needed -->
<script src="Public/js/bootstrap.min.js"></script>
</body>
</html>
Application/Admin/View/Index/index.php
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- 上述3个meta标签*必须*放在最前面,任何其他内容都*必须*跟随其后! -->
<title>课程设计题目统计后台管理系统</title>
<!-- Bootstrap -->
<link href="Public/css/bootstrap.min.css" rel="stylesheet">
<link href="Public/style.css" rel="stylesheet" type="text/css"/>
<!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
<!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
<!--[if lt IE 9]>
<script src="//cdn.bootcss.com/html5shiv/3.7.2/html5shiv.min.js"></script>
<script src="//cdn.bootcss.com/respond.js/1.4.2/respond.min.js"></script>
<![endif]-->
</head>
<body>
<div class="container">
<div class="col-sm-3"></div>
<div class="col-sm-6 content">
<h2>课程设计题目统计后台管理系统</h2>
<form class="form-horizontal" action="index.php?m=Admin&a=check" method="POST">
<div class="form-group">
<label for="number" class="col-sm-3 control-label">管理员账号:</label>
<div class="col-sm-9">
<input type="text" class="form-control" id="number" placeholder="请输入管理员账号" name="account">
</div>
</div>
<div class="form-group">
<label for="password" class="col-sm-3 control-label">修改密码:</label>
<div class="col-sm-9">
<input type="password" class="form-control" id="password" placeholder="请输入管理员密码" name="psw">
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-3 col-sm-9">
<button type="submit" class="btn btn-info">登陆</button>
<a href="index.php?m=Admin&a=show">显示全部学生题目</a>
</div>
</div>
<?php
if($err) {
?>
<p class="info"><span style="font-weight: bold">错误:</span><?php echo $err;?></p>
<?php
}
?>
</form>
</div>
<div class="col-sm-3"></div>
</div>
<!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
<script src="Public/js/jquery.min.js"></script>
<!-- Include all compiled plugins (below), or include individual files as needed -->
<script src="Public/js/bootstrap.min.js"></script>
</body>
</html>
Application/Admin/View/Index/showStudent.php
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- 上述3个meta标签*必须*放在最前面,任何其他内容都*必须*跟随其后! -->
<title>课程设计题目统计系统</title>
<!-- Bootstrap -->
<link href="Public/css/bootstrap.min.css" rel="stylesheet">
<link href="Public/style.css" rel="stylesheet" type="text/css"/>
<style>
p.info {
margin-bottom: 0;
}
</style>
<!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
<!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
<!--[if lt IE 9]>
<script src="//cdn.bootcss.com/html5shiv/3.7.2/html5shiv.min.js"></script>
<script src="//cdn.bootcss.com/respond.js/1.4.2/respond.min.js"></script>
<![endif]-->
</head>
<body>
<div class="container">
<div class="content">
<table class="table table-hover">
<caption><h3>学生课程设计题目</h3></caption>
<?php
if(!isset($res) || empty($res)) {
echo '<tr><td>暂无学生题目</td></tr>';
} else {
?>
<thead>
<tr>
<!-- <th>删除</th> -->
<th>序号</th>
<th>学号</th>
<th>姓名</th>
<th>题目</th>
<th>状态</th>
<th>录入时间</th>
<th>合作学生</th>
</tr>
</thead>
<tbody>
<?php
$i = $start ? $start : 1;
foreach ($res as $value) {
echo '<tr>';
echo "<th scope='row'>$i</th>";
echo "<td>{$value['sno']}</td>";
echo "<td>{$value['name']}</td>";
echo "<td>{$value['title']}</td>";
echo "<td>{$value['state']}</td>";
echo "<td>{$value['last_time']}</td>";
echo "<td>{$value['partner']}</td>";
echo '</tr>';
$i++;
}
?>
</tbody>
<?php
}
?>
</table>
<nav>
<ul class="pager">
<li>
<?php
if($prePage) {
if($prePage >= 1) {
echo "<a href='index.php?m=Admin&a=show&cur=$prePage'>上一页</a>";
}
}
?>
</li>
<li>
<?php
if($nextPage) {
if($nextPage >= 1) {
echo "<a href='index.php?m=Admin&a=show&cur=$nextPage'>下一页</a>";
}
}
?>
</li>
</ul>
</nav>
<a href="index.php?m=Admin">返回登陆界面</a>
</div>
</div>
<!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
<script src="Public/js/jquery.min.js"></script>
<!-- Include all compiled plugins (below), or include individual files as needed -->
<script src="Public/js/bootstrap.min.js"></script>
</body>
</html>