views 登录 <?php ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"> <head> <meta http-equiv="Content-Type" content="text/html;charset=UTF-8"> <title>登陆页面</title> </head> <body> <center> <form action="?r=login/login" method="post"> <input type="hidden" id="_csrf" name="_csrf" value="<?php echo yii::$app->request->csrfToken?>"/> <table> <tr> <td>用户名</td> <td><input type="text" name="username"/></td> </tr> <tr> <td>密码</td> <td><input type="password" name="password"/></td> </tr> <tr> <td></td> <td> <input type="submit" value="登陆"/> <input type="reset" value="重置"/> </td> </tr> </table> </form> </center> </body> </html> -----------------------------------------------------------------------------------------------------------------LoginController.php<?php namespace backend\controllers; use Yii; use yii\filters\AccessControl; use yii\web\Controller; use common\models\LoginForm; use yii\filters\VerbFilter; class LoginController extends Controller { //显示登陆页面 public function actionIndex() { return $this->render('index'); } //执行登陆 public function actionLogin() { $request = yii::$app->request; $username = $request->post('username'); $password = $request->post('password'); $db = yii::$app->db; $login=$db->createCommand("select * from loginzk1 where username='$username'")->queryAll(); //var_dump($login);die; //var_dump($login[0]['username']);die; if($login){ if($password==$login[0]['password']){ $session = yii::$app->session; $session->set('username', $login[0]['username']); $session->set('l_id', $login[0]['l_id']); if($login[0]['state']==1){ //管理员登陆 $this->redirect(array('class/index')); }else{ //学生登陆 $this->redirect(array('stud/index')); } }else{ echo "密码错误"; } }else{ echo "用户名不存在"; } } //退出 public function actionLoginout(){ $session =yii::$app->session; $session->remove('username'); return $this->render('index'); } } ?> -------------------------------------------------------------------------------------------------ClassController.php(管理员c)<?php namespace backend\controllers; use Yii; use yii\filters\AccessControl; use yii\web\Controller; use common\models\LoginForm; use yii\filters\VerbFilter; class ClassController extends Controller { //非法登陆 //定义构造方法, 传参id,$models=null public function __construct($id,$models=null){ parent::__construct($id,$models); $session=yii::$app->session; $sid=$session['username']; if(!$sid){ //echo "非法登陆";die; //return $this->redirect(array('/index/index/')); return $this->redirect('?r=login/index'); } } public function actionIndex(){ return $this->render('index'); } public function actionAdd(){ $request=yii::$app->request; $data=$request->post(); //var_dump($data);die; unset($data['_csrf']); //var_dump($data);die; $db=yii::$app->db; $res=$db->createCommand()->insert('classzk1',$data)->execute(); if($res){ $this->redirect(array('class/show')); }else{ $this->redirect(array('class/index')); } } public function actionShow(){ $db=yii::$app->db; $data=$db->createCommand('select * from loginzk1 join c_lzk1 on loginzk1.l_id=c_lzk1.lid join classzk1 on c_lzk1.cid=classzk1.c_id')->queryAll(); //var_dump($data);die; return $this->render('show',['data'=>$data]); } //申请加入班级 public function actionShen(){ $request=yii::$app->request; //接收班级id $cid=$request->get('cid'); //var_dump($c_id);die; //接收状态值 $status=$request->get('status'); //var_dump($status);die; //获取用户id $session=yii::$app->session; $lid=$session->get('l_id'); //组合数组 $c_l=array( 'lid'=>$lid, 'cid'=>$cid, 'status'=>$status ); // var_dump($c_l);die; //执行添加 $db=yii::$app->db; $res=$db->createCommand()->insert('c_lzk1',$c_l)->execute(); // var_dump($c_id);die; } //管理员审核 public function actionShenhe(){ //接收班级id $request=yii::$app->request; $id=$request->get('id'); //接收状态值 $status=$request->get('status'); //修改id为$id的派生表status值 $db=yii::$app->db; $res=$db->createCommand("update c_lzk1 set status='$status'where id='$id'")->execute(); if($res){ echo "1"; } } }views
index.php<?php ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"> <head> <meta http-equiv="Content-Type" content="text/html;charset=UTF-8"> <title>管理员创建班级</title> </head> <body> <h4 align="right"><a href="?r=class/show">查看申请</a> 欢迎 <font style="color:red"><?php $session=yii::$app->session; echo $session->get('username'); ?></font>登陆 <a href="?r=login/index">退出</a></h4> <center> <form action="?r=class/add" method="post" οnsubmit="return checkAll()"><!-- 添加表单时间onsubmit--> <input type="hidden" id="_csrf" name="_csrf" value="<?php echo yii::$app->request->csrfToken?>"/> <table> <tr> <td>班级名称:</td> <td></td> </tr> <tr> <td><input type="text" name="mingcheng" οnblur="checkMen()"/><span id='sp1' style='color:red'></span><!-- 添加失去焦点事件onblur--></td> <td></td> </tr> <tr> <td>接纳新成员:</td> <td></td> </tr> <tr> <td><input type="radio" name="term" value="1"/>需要管理员同意才能加入</td> <td></td> </tr> <tr> <td> <input type="radio" name="term" value="0"/>允许任何人加入</td> <td></td> </tr> <tr> <td><input type="radio" name="term" value="2"/>不允许任何人加入</td> <td></td> </tr> <tr> <td>班级简介:</td> <td></td> </tr> <tr> <td><textarea name="intro" id="" cols="30" rows="10" οnblur="checkIntro()"> </textarea><span id='sp2' style='color:red'></span></td><!-- 添加失去焦点事件--> <td></td> </tr> <tr> <td><input type="submit" value="现在创建"/></td> <td></td> </tr> </table> </form> </center> </body> <script> //验证班级名称 function checkMen(){//采用事件名定义方法 //获取班级名称的值 var ming=document.getElementsByName('mingcheng')[0].value; // alert(mingcheng); //获取span的属性 var sp1=document.getElementById('sp1'); if(ming==""){ sp1.innerHTML="班级名称不能为空"; return false; }else{ var reg=/^\w{5,15}$/i; if(!reg.test(ming)){//注:此处不加value; sp1.innerHTML="班级名为5-15位字符"; return false; }else{ sp1.innerHTML="正确"; return true; } return true; } } //验证班级简介 function checkIntro(){ var intro=document.getElementsByName('intro')[0].value; var sp2=document.getElementById('sp2'); if(intro==""){ sp2.innerHTML="简介不能为空"; return false; }else{ var reg=/^[\u4e00-\u9fa5]{0,20}$/; if(!reg.test(intro)){ sp2.innerHTML="简介最多20个汉字"; return false; }else{ sp2.innerHTML="正确"; return true; } } return true; } //阻止提交 function checkAll(){ if(checkMen($('mingcheng'))&checkIntro($('intro'))){ return true; }else{ return false; } } </script> </html>show.php<?php ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"> <head> <meta http-equiv="Content-Type" content="text/html;charset=UTF-8"> <title>班级列表</title> </head> <script src="jquery.1.12.js"></script> <body> <h4 align="right"><a href="?r=class/index">添加班级</a> 欢迎 <font style="color:red"><?php $session=yii::$app->session; echo $session->get('username'); ?></font>登陆 <a href="?r=login/index">退出</a></h4> <center> <h3>申请列表</h3> <table> <?php foreach($data as $key=>$val) { ?> <tr> <!-- <td>--><?//=$val['id']?><!--</td>--> <td><?php echo $val['username']."申请加入班级".$val['mingcheng']?></td> <?php if($val['status']==0){?> <td> 已通过</td> <?php }elseif($val['status']==2){?> <td>已拒绝</td> <?php }else{?> <td> <button class="tong" id="<?=$val['id']?>">通过</button><button class="ju" id="<?=$val['id']?>">拒绝</button> </td> <?php }?> </tr> <?php } ?> </table> </center> </body> <script> //给按钮加点击事件 $(".tong").click(function(){ //获取本行的id var id=$(this).attr('id'); //定义状态值 var status=0; //定义当前元素变量 var tong=$(this); //get传值 $.get('?r=class/shenhe',{id:id,status:status},function(msg){ if(msg=1){ tong.parent().html('已通过') } }) }) //给按钮加点击事件 $(".ju").click(function(){ //获取本行的id var id=$(this).attr('id'); status=2; var ju=$(this); //get传值 $.get('?r=class/shenhe',{id:id,status:status},function(msg){ if(msg=1){ ju.parent().html('已拒绝') } }) }) </script> </html> --------------------------------------------------------------------------------------------StudController.php<?php namespace backend\controllers; use Yii; use yii\filters\AccessControl; use yii\web\Controller; use common\models\LoginForm; use yii\filters\VerbFilter; class StudController extends Controller { //非法登陆 //定义构造方法, 传参id,$models=null public function __construct($id,$models=null){ parent::__construct($id,$models); $session=yii::$app->session; $sid=$session['username']; if(!$sid){ //echo "非法登陆";die; //return $this->redirect(array('/index/index/')); return $this->redirect('?r=login/index'); } } //显示 public function actionIndex(){ //获取用户id $session=yii::$app->session; $l_id=$session->get('l_id'); $db=yii::$app->db; //查询班级表 $datac=$db->createCommand("select * from classzk1")->queryAll(); //两表联查查询班级表和关系表 $data=$db->createCommand("select * from classzk1 left join c_lzk1 on classzk1.c_id=c_lzk1.cid where lid='$l_id' ")->queryAll(); //定义数组,两表联查的结果为此用户操作的结果,而班级表是全部班级初始状态,将用户操作的状态添加到班级表,循环 //班级表,在foreach内再循环两表联查后用户操作的状态,判断id相同,将用户操作的状态赋给班级表数据,生成一个新数组! //将此数组数据传至前台遍历! $res=array(); foreach($datac as $v) { // var_dump($v); foreach ($data as $v1) { // var_dump($v1); if ($v['c_id'] == $v1['c_id']) { $v['status'] = $v1['status']; } } $res[]=$v; } //var_dump($res);die; return $this->render('index',['res'=>$res]); } } ?> index.php用户展示页面<?php ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"> <head> <meta http-equiv="Content-Type" content="text/html;charset=UTF-8"> <title>班级列表</title> </head> <script src="jquery.1.12.js"></script> <body> <h4 align="right">欢迎 <font style="color:red"><?php $session=yii::$app->session; echo $session->get('username'); ?></font>同学登陆 <a href="?r=login/index">退出</a></h4> <center> <h3>班级列表</h3> <table> <?php foreach($res as $key=>$val) { ?> <tr> <td><?=$val['mingcheng']?></td> <?php if(isset($val['status'])&&$val['status']==2){?> <td>已拒绝</td> <?php }elseif(isset($val['status'])&&$val['status']==0){ ?> <td>已通过申请</td> <?php }elseif(isset($val['status'])&&$val['status']==1){ ?> <td>已申请,待审核</td> <?php }else{?> <td><button class="but" id="<?=$val['c_id']?>" name="<?=$val['term']?>">申请加入</button></td> <?php }?> </tr> <?php } ?> </table> </center> </body> <script> //生成点击事件 $(".but").click(function(){ //获取id var cid=$(this).attr('id'); //获取班级状态 var term=$(this).attr('name'); if(term==1){ $(this).parent().html('已申请,待审核'); status=1; } if(term==0){ $(this).parent().html('已通过申请'); status=0; } if(term==2){ $(this).parent().html('该班不允许加入'); status=2; } $.get("?r=class/shen",{'cid':cid,'status':status},function(msg){ // alert(msg) }); }); </script> </html>
yii,两种用户登录,管理员添加班级a(状态),用户事情,管理员审核小功能
最新推荐文章于 2023-05-25 11:58:05 发布