生活随笔
收集整理的這篇文章主要介紹了
web课设 thinkphp5+mySQL 简易教学管理系统
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
教學管理系統 需求 結果展示 數據庫設計 實現 一、文件創建 二、前端界面 三、數據庫設置 四、控制器 五、前端返回信息處理
需求
有登錄界面,不同的用戶登錄后進入不同的頁面。 能完成基本數據(包括學生、教師、課程、選課)的增刪改。 根據指定要求完成查詢。 可以查詢指定姓名的學生或教師。 可以查找某位教師的授課門數。 可以查找指定學生選修的課程列表,包括課程名、任課教師、成績等。 可以查找指定所有課程的平均成績。
結果展示
登錄界面
學生界面
教師界面
管理員界面
數據庫設計
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 ;
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 ) { 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 = $_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 ) ; } } 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 ) ; } } 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 ) ; } } 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' , data
: { 'sid' : $ ( "#sid" ) . val ( ) , 'cid' : $ ( "#cid" ) . val ( ) } , success
: function ( data
) { } } ) ; }
以學生按姓名查詢信息為例:
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
) ; } } } ) ; }
需要完整代碼可戳我。
總結
以上是生活随笔 為你收集整理的web课设 thinkphp5+mySQL 简易教学管理系统 的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔 網站內容還不錯,歡迎將生活随笔 推薦給好友。