效果图:
控制器:
<?php
namespace frontendcontrollers;
use Yii;
use frontendmodelsUser;
use yiiwebController;
use yiiwebsession;
class UserController extends Controller
{
public $enableCsrfValidation = false;
public function actionCreate()
{
$model = new User();
if ($model->load(Yii::$app->request->post()) ) {
$res=$model->getpost();
//判断用户名称是否正确
if($res['u_name'])
{
//判断用户密码是否正确
if($res['u_pwd']==$_POST['User']['u_pwd']){
$session = Yii::$app->session;
$session->set('u_id',$res['u_id']);
echo "<script>alert('登陆成功');location.href='index.php?r=user/question'</script>";
}else{
echo "<script>alert('密码错误,重新登陆');location.href='index.php?r=user/create'</script>";
}
}else{
echo "<script>alert('用户名不存在,重新登陆');location.href='index.php?r=user/create'</script>";
}
} else {
return $this->render('create', [
'model' => $model,
]);
}
}
//显示问题
public function actionQuestion(){
$model = new User();
//查出所有问题
$row=$model->getList();
//查询所有答案
$res=$model->getanswer();
//取出十条数据里的值
$key=array_rand($row,10);//键名
foreach($key as $key1=>$val)
{
$que[]=$row[$val];
}
//遍历数组
foreach($que as $key=>$row1){
foreach($res as $key2=>$res1){
if($row1['question_id']==$res1['question_id']){
$que[$key]['answer'][]=$res1;
}
}
}
return $this->renderPartial('test',['row'=>$que]);
}
//得出成绩
public function actionAdd1(){
//var_dump($_POST);die;
$model = new User();
$rank=$model->getinfo();
//echo $rank;die;
return $this->renderPartial('rank',['rank'=>$rank]);
}
}
模型层:
//判断用户登陆是否正确
public function getpost()
{
//var_dump($_POST);die;
//接收数据
$u_name=$_POST['User']['u_name'];
$u_pwd=$_POST['User']['u_pwd'];
//查询语句
$row=Yii::$app->db->CreateCommand("select * from user where u_name='$u_name'")->queryone();
//var_dump($row);die;
return $row;
}
//先查出所有问题
public function getList(){
//随机查询10条语句
//$row=Yii::$app->db->CreateCommand("select * from question order by rand() limit 10")->queryall();
$row=Yii::$app->db->CreateCommand("select * from question")->queryall();
//var_dump($row);die;
return $row;
}
//查询所有问题
public function getanswer(){
//查询语句
$row=Yii::$app->db->CreateCommand("select * from answer")->queryall();
//var_dump($row);die;
return $row;
}
//查询答案id下的正确结果
public function getinfo()
{
$answer_id=implode(",",$_POST);
//var_dump($_POST);die;
//查询语句
$row=Yii::$app->db->CreateCommand("select count(*) from answer where answer_id in($answer_id) and answer_state=1")->queryScalar();
//var_dump($row);die;
//获取登录的用户id
$session = Yii::$app->session;
$u_id = $session->get('u_id');
//添加成绩入库
$data=Yii::$app->db->CreateCommand()->insert("rank",['rank'=>$row,'u_id'=>$u_id])->execute();
return $row;
}
视图层:
<center><h3>欢迎来到考试页面</h3></center>
<form action="index.php?r=user/add1" method="post">
<table>
<?php foreach($row as $key=>$v) {?>
<tr>
<td>
<?php echo $v['question_id'];?>.<?php echo $v['question'];?>()
</td>
</tr>
<tr>
<?php foreach($v['answer'] as $key=>$v1) {?>
<td><input type="radio" name="<?php echo $v1['question_id'];?>" value="<?php echo $v1['answer_id'];?>">
<?php echo $v1['answer']?>
</td>
</tr>
<?php }?>
<?php }?>
<tr>
<td><input type="submit" value="提交试卷" /></td>
</tr>
</form>
</table>