需求
- 有登录界面,不同的用户登录后进入不同的页面。
- 能完成基本数据(包括学生、教师、课程、选课)的增删改。
- 根据指定要求完成查询。
- 可以查询指定姓名的学生或教师。
- 可以查找某位教师的授课门数。
- 可以查找指定学生选修的课程列表,包括课程名、任课教师、成绩等。
- 可以查找指定所有课程的平均成绩。
结果展示
登录界面
学生界面
教师界面
管理员界面
数据库设计
E-R图
实现
一、文件创建
在thinkPHP5下的application内创建以下三个文件夹。
在controller文件夹内部新建一个php文件,作为控制器,命名为Signin.php。
在model文件夹内创建各个表所对应的模型
在view内创建与控制器对应的文件夹,命名为signin,在signin文件夹内部,创建各个界面的html文件。以及需要的图片
二、前端界面
以登录界面为例。
在调用控制器函数时,可以用 {:url(项目名/控制器/函数)},如此调用。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<style>
* {
margin: 0;
overflow: hidden;
padding: 0;
}
li ul {
list-style: none;
}
input {
margin: 3px;
outline: none;
}
input[type=text] {
width: 300px;
padding: 8px 20px;
margin: 4px 0px 4px 0px;
display: flex;
border: 1px solid #ccc;
border-radius: 4px;
box-sizing: border-box;
background: #F0F0F0;
}
input[type=text]:focus {
width: 300px;
padding: 8px 20px;
margin: 4px 0px 4px 0px;
display: flex;
border: 1px solid #ccc;
border-radius: 4px;
box-sizing: border-box;
background: white;
}
input[type=submit] {
width: 300px;
background-color:#A6100E;
color: white;
padding: 10px 20px;
margin: 18px 0;
border: none;
border-radius: 4px;
cursor: pointer;
}
input[type=submit]:active {
width: 300px;
background-color: #8D0E0C;
color: #BEBEBE;
padding: 10px 20px;
margin: 18px 0;
border: none;
border-radius: 4px;
outline: none;
cursor: pointer;
}
input [type=submit]:after{
outline: none;
}
select{
width: 300px;
padding: 8px 20px;
margin: 4px 0px 4px 0px;
border: 1px solid #ccc;
border-radius: 4px;
box-sizing: border-box;
outline: none;
}
select:active{
outline: none;
}
option {
outline: none;
}
#header {
display: flex;
background: black;
color: whitesmoke;
flex-direction: row;
}
#header ul {
display: flex;
margin-left: 10%;
}
#header li{
align-items: center;
display: flex;
font-weight: bold;
font-size: 16px;
}
#header ul li:hover{
background-color: #444444;
cursor: pointer;
}
#header ul li:active{
background-color: #656565;
}
#header a{
display: inline-block;
padding: 0 25px 0 25px;
height: 60px;
line-height: 60px;
font-size: 16px;
}
#signIn {
margin-top: 10%;
margin-right: 10%;
float: right;
color: white;
display: flex;
flex-direction: column;
}
#hh{
display: flex;
}
a {
direction: none;
}
a:focus{
cursor: pointer;
}
</style>
<body background="../../../../../TP/public/curricula/signin/img/02.jfif">
<div id="header">
<ul>
<li><a herf="signin">首页</a></li>
</ul>
</div>
<div>
<form id="signIn" method="post" action="{:url('curricula/Signin/checkIdentity')}">
<p>用户名</p>
<input type="text" name="username" id="username" placeholder="username" />
<p>密码</p>
<input type="text" name="password" id="password" placeholder="password" />
<p>身份</p>
<select name="identity" id="identity" >
<option value="s">学生</option>
<option value="t">教师</option>
<option value="a">尊敬的管理员</option>
</select>
<input type="submit" name="btn1" value="登录 login" />
</form>
</div>
</body>
三、数据库设置
数据库建表过程略。数据库名称为student。
在application文件夹内有一个database.php文件,修改其中的信息,以连接mysql。
修改以下信息即可。
// 数据库类型
'type' => 'mysql',
// 服务器地址
'hostname' => '127.0.0.1',
// 数据库名
'database' => 'student',
// 用户名
'username' => 'root',
// 密码
'password' => '168168',
在完成以上设置后,可以直接在表对应的model内,使用方法对完成增删改查(以student为例)
遇到比较复杂的情况可以像这样直接进行SQL查询:
use think\Model; //必须填上这句,才可使用
$M = $this;
$sql = "要执行的SQL语句";
$r = $M->execute($sql);
$r 即为查询的结果。
student.php
<?php
namespace app\curricula\model; //curricula可改成你的项目名称。
use think\Model;
class Student extends Model
{
//根据学号选择学生信息
public function selStudentById($id)
$student = Student::get($id);
if ($student) {
return true;
} else
return false;
}
//增加一名学生
public function addStudent($id, $name, $birth, $sex) //$id,$name,$sex,$brith)
{
if (!$this->selStudentById($id)) {
$M = $this;
$r = $M->execute("insert into student values('" . (string) $id . "','" . (string) $name . "','" . (string) $birth . "','" . (string) $sex . "')");
if ($r)
return $this->selAll();
else
return false;
} else
return false;
}
//删除一名学生
public function delStudent($id) //$id,$name,$sex,$brith)
{
if ($this->selStudentById($id)) {
$M = $this;
$sql = "delete from student where s_id='" . (string) $id . "'";
$r = $M->execute($sql);
if ($r) {
return $this->selAll();
} else
return false;
}
else
return false;
}
//更新学生信息
public function uptStudent($id, $name, $birth, $sex) //$id,$name,$sex,$brith)
{
$flag = false;
$M = $this;
if ($this->selStudentById($id)) {
if ($name && $name != '') {
$sql = "update student set s_name='" . (string) $name . "'where s_id='" . (string) $id . "'";
$r = $M->execute($sql);
$flag = true;
}
if ($sex && $sex != '') {
$sql = "update student set s_sex='" . (string) $sex . "'where s_id='" . (string) $id . "'";
$r = $M->execute($sql);
$flag = true;
}
if ($birth && $birth != '') {
$sql = "update student set s_birth='" . (string) $birth . "'where s_id='" . (string) $id . "'";
$r = $M->execute($sql);
$flag = true;
}
if ($flag)
return $this->selAll();
else
return 2;
} else
return 1;
}
//根据学生姓名查询学生信息
public function selStudentByName($name) //$id,$name,$sex,$brith)
{
$M = $this;
$r = $M->query("select * from student where s_name='" . $name."'");
if ($r)
return $r;
else
return false;
}
//查询全部信息
public function selAll()
{
$M = $this;
$r = $M->query("select * from student");
if ($r) {
return $r;
} else
return false;
}
}
四、控制器
注意点:
- 命名空间 namespace app\项目名\控制器文件夹名
- 必须引入几个模型的位置,
- 返回时使用json()包装数据
- 调用界面时使用view()函数
<?php
namespace app\curricula\Controller;
use think\Controller;
use think\Model;
use app\curricula\model\Student;
use app\curricula\model\Teacher;
use app\curricula\model\Users;
use app\curricula\model\Course;
use app\curricula\model\Score;
use think\migration\command\migrate\Status;
define('APP_DEBUG', true);
class Signin extends Controller
{
public function signin()
{
return view("signin");
}
public function student()
{
return view("student");
}
public function teacher()
{
return view("teacher");
}
public function admin()
{
return view("admin");
}
public function addScore($id, $id2, $s)
{
return (new \app\curricula\model\Score())->addScore($id, $id2, $s);
}
public function checkIdentity()
{
$username = $_POST['username'];
$password = $_POST['password'];
$identity = $_POST['identity'];
$r = (new \app\curricula\model\Users())->check($username, $password,$identity);
$data = array(
"status" => false,
"info" => array()
);
if (!$r) {
$this->redirect('signin');
}
if ($identity == 's')
$this->redirect('student');
else if ($identity == 't')
$this->redirect('teacher');
else if ($identity == 'a')
$this->redirect('admin');
else
$this->redirect('signin');
}
public function selScoreCT() //$id,$name,$sex,$brith)
{
$id = $_GET['id'];
$r = (new \app\curricula\model\Score())->selScoreCT($id);
$data = array(
"status" => false,
"info" => array()
);
if ($data == false) {
return (json($data));
} else {
$data["status"] = true;
$data["info"] = $r;
return (json($data));
}
}
public function deladdScore()
{
$ch = $_GET["ch"];
$sid = $_GET["sid"];
$cid = $_GET["cid"];
if ($ch == 'del') {
$r = (new \app\curricula\model\Score())->stuDelScore($sid, $cid);
$data = array(
"status" => false,
"info" => array("ERROR")
);
if ($r == -1) {
$data["status"] = -1;
$data["info"] = "成绩已登陆,别想改啦";
return (json($data));
}
if ($r == false) {
return (json($data));
} else {
$data["status"] = true;
$data["info"] = $r;
return (json($data));
}
}
else if ($ch == 'add') {
$r = (new \app\curricula\model\Score())->stuAddScore($sid, $cid);
$data = array(
"status" => false,
"info" => -1
);
if ($r == -2) {
$data["status"] = -2;
$data["info"] = "你已选择这门课程";
return (json($data));
}
if ($r == -1) {
$data["status"] = -1;
$data["info"] = "没有这门课程";
return (json($data));
}
if ($r == -3) {
$data["status"] = -3;
$data["info"] = "插入失败";
return (json($data));
}
else {
$data["status"] = true;
$data["info"] = $r;
return (json($data));
}
}
}
//选择课程信息
public function selCourseMS()
{
$tid = $_GET['tid'];
$r = (new \app\curricula\model\Course())->countCourse($tid);
$data = array(
"status" => false,
"info" => array(),
"cnum" => '0'
);
if (!$r || $r[0]['c_num'] == 0)
return json($data);
else {
$data["cnum"] = (string) $r[0]['c_num'];
$r = (new \app\curricula\model\Course())->selCourse($tid);
$data["info"] = $r;
$data["status"] = true;
return json($data);
}
}
public function selTeacherId()
{
$tname = $_GET['name'];
$r = (new \app\curricula\model\Teacher())->selTeacherId($tname);
$data = array(
"status" => false,
"info" => "查无此人"
);
if (!$r) {
return json($data);
} else {
$data["info"] = $r;
$data["status"] = true;
return json($data);
}
}
public function selCourseAVG()
{
$c = $_GET['cid'];
$cid = '';
for ($i = 0; $i < count($c); $i++) {
$cid .= "'" . $c[$i] . "'";
if ($i != count($c) - 1) {
$cid .= ',';
}
}
$r = (new \app\curricula\model\Course())->selCourseAVG($cid);
$data = array(
"status" => false,
"info" => "无课程信息"
);
if (!$r) {
return json($data);
} else {
$data["info"] = $r;
$data["status"] = true;
return json($data);
}
}
public function uptScore()
{
$sid = $_GET['sid'];
$cid = $_GET['cid'];
$sc = $_GET['sc'];
$r = (new \app\curricula\model\Score())->uptScore($sid, $cid, $sc);
$data = array(
"status" => false,
"info" => "无课程信息"
);
if (!$r) {
return json($data);
} else {
$data["info"] = $r;
$data["status"] = true;
return json($data);
}
}
public function addStudent($sid, $sname, $sbth, $sex)
{
$r = (new \app\curricula\model\Student())->addStudent($sid, $sname, $sbth, $sex);
$data = array(
"status" => false,
"info" => "失败"
);
if ($r == false) {
return json($data);
} else {
$data["info"] = $r;
$data["status"] = true;
return json($data);
}
}
// student
public function delStudent($sid)
{
$r = (new \app\curricula\model\Student())->delStudent($sid);
$data = array(
"status" => false,
"info" => "失败"
);
if ($r == false) {
return json($data);
} else {
$data["info"] = $r;
$data["status"] = true;
return json($data);
}
}
public function uptStudent($id, $name, $sbth, $sex)
{
$r = (new \app\curricula\model\Student())->uptStudent($id, $name, $sbth, $sex);
$data = array(
"status" => false,
"info" => "失败"
);
if ($r == 1 || $r == 2) {
$data["status"] = $r;
return json($data);
} else {
$data["info"] = $r;
$data["status"] = true;
return json($data);
}
}
//teacher
public function addTeacher($id, $name)
{
$r = (new \app\curricula\model\Teacher())->addTeacher($id, $name);
$data = array(
"status" => false,
"info" => "失败"
);
if ($r == false) {
return json($data);
} else {
$data["info"] = $r;
$data["status"] = true;
return json($data);
}
}
public function delTeacher($id)
{
$r = (new \app\curricula\model\Teacher())->delTeacher($id);
$data = array(
"status" => false,
"info" => "失败"
);
if ($r == false) {
return json($data);
} else {
$data["info"] = $r;
$data["status"] = true;
return json($data);
}
}
public function uptTeacher($id, $name)
{
$r = (new \app\curricula\model\Teacher())->uptTeacher($id, $name);
$data = array(
"status" => false,
"info" => "失败"
);
if ($r == 1 || $r == 2) {
$data["status"] = $r;
return json($data);
} else {
$data["info"] = $r;
$data["status"] = true;
return json($data);
}
}
//Course
public function addCourse($cid, $name, $tid)
{
$r = (new \app\curricula\model\Course())->addCourse($cid, $name, $tid);
$data = array(
"status" => false,
"info" => "失败"
);
if ($r == false) {
return json($data);
} else {
$data["info"] = $r;
$data["status"] = true;
return json($data);
}
}
public function delCourse($id)
{
$r = (new \app\curricula\model\Course())->delCourse($id);
$data = array(
"status" => false,
"info" => "失败"
);
if ($r == false) {
return json($data);
} else {
$data["info"] = $r;
$data["status"] = true;
return json($data);
}
}
public function uptCourse($cid, $name, $tid)
{
$r = (new \app\curricula\model\Course())->uptCourse($cid, $name, $tid);
$data = array(
"status" => false,
"info" => "失败"
);
if ($r == 1 || $r == 2) {
$data["status"] = $r;
return json($data);
} else {
$data["info"] = $r;
$data["status"] = true;
return json($data);
}
}
public function studentADU()
{
$ch = $_GET['ch'];
$sid = $_GET['sid'];
$sname = $_GET['sname'];
$sbth = $_GET['sbth'];
$sex = $_GET['sex'];
if ($ch == 'add') {
return $this->addStudent($sid, $sname, $sbth, $sex);
} else if ($ch == 'del') {
return $this->delStudent($sid);
} else if ($ch == 'upt') {
return $this->uptStudent($sid, $sname, $sbth, $sex);
} else
return $data = array(
"status" => false,
"info" => "失败"
);
}
public function teacherADU()
{
$ch = $_GET['ch'];
$id = $_GET['tid'];
$name = $_GET['tname'];
if ($ch == 'add') {
return $this->addTeacher($id, $name);
} else if ($ch == 'del') {
return $this->delTeacher($id);
} else if ($ch == 'upt') {
return $this->uptTeacher($id, $name);
} else
return $data = array(
"status" => false,
"info" => "失败"
);
}
public function courseADU()
{
$ch = $_GET['ch'];
$cid = $_GET['cid'];
$name = $_GET['cname'];
$tid = $_GET['tid'];
if ($ch == 'add') {
return $this->addCourse($cid, $name, $tid);
} else if ($ch == 'del') {
return $this->delCourse($cid);
} else if ($ch == 'upt') {
return $this->uptCourse($cid, $name, $tid);
} else
return $data = array(
"status" => false,
"info" => "失败"
);
}
public function selAllCourseAVG()
{
$r = (new \app\curricula\model\Course())->selAllCourseAVG();
$data = array(
"status" => false,
"info" => "无课程信息"
);
if (!$r) {
return json($data);
} else {
$data["info"] = $r;
$data["status"] = true;
return json($data);
}
}
public function selStudentByName()
{
$sname = $_GET['sname'];
$r = (new \app\curricula\model\Student())->selStudentByName($sname);
$data = array(
"status" => false,
"info" => "无信息"
);
if (!$r) {
return json($data);
} else {
$data["info"] = $r;
$data["status"] = true;
return json($data);
}
}
public function selTeacherByName()
{
$tname = $_GET['tname'];
$r = (new \app\curricula\model\Teacher())->selTeacherByName($tname);
$data = array(
"status" => false,
"info" => "无信息"
);
if (!$r) {
return json($data);
} else {
$data["info"] = $r;
$data["status"] = true;
return json($data);
}
}
}
五、前端返回信息处理
前端使用ajax提交并显示数据
ajax简单入门
//需引入jquery
<script src="http://libs.baidu.com/jquery/2.1.4/jquery.min.js"></script>
function func() {
$.ajax({
url: "delXXX", //后端函数名
type: 'GET', //方式GET or POST
data: { //要交由后端处理的数据,用逗号间隔
'sid': $("#sid").val(),
'cid': $("#cid").val()
},
success: function (data) //成功返回数据,data是服务器端返回的字符串数据
{
// .......your code
}
}); //分号结束
}
以学生按姓名查询信息为例:
function inquiryStuByName() {
if ($("#sname").val() == '') {
alert("Empty Input!");
return;
}
$.ajax({
url: "selStudentByName",
type: 'GET',
data: {
"sname": $("#sname").val()
},
success: function (data) {
if (!data.status)
alert("ERROR");
else {
var str = style;
str += "<Table class='pure-table pure-table-bordered'>";
str += "<tr> <td>学号</td> <td>姓名</td><td>出生日期</td><td>性别</td></tr>";
for (var i = 0; i < data.info.length; i++) {
str += '<tr>';
str += ("<td>" + data.info[i].s_id + "</td>");
str += ("<td>" + data.info[i].s_name + "</td>");
str += ("<td>" + data.info[i].s_birth + "</td>");
str += ("<td>" + data.info[i].s_sex + "</td>");
str += '</tr>';
}
str += "</Table>"
$("#show").html(str);
}
}
});
}