<?php
namespace app\controllers;
use Yii;
use yii\filters\AccessControl;
use yii\web\Controller;
use yii\filters\VerbFilter;
use yii\data\Pagination;
use app\models\Users;
use app\models\Yuyue;
//留言
class MessController extends Controller
{
public function actionIndex()
{ //开启session
$session = Yii::$app->session;
$session->open();
//查询所有留言
$sql = “select * from message”;
$mess = \yii::$app->db->createCommand($sql)->queryAll();
//判断是否登录
$user = $session->get(“user”);
$u_id = $session->get(“u_id”);
if(!isset($user)){
$status = 0;
}else{
$status = 1;
//查询隐藏的留言
$sql = “select * from dis_mess where u_id=”.$u_id;
$display = \yii::$app->db->createCommand($sql)->queryAll();
//将m_id遍历为一维数组
foreach($display as $key =>$val){
$display[$key] = $display[$key][‘m_id’];
}
//取出隐藏留言的id,有就 删除该变量
foreach($mess as $k=>$v){
if(in_array($mess[$k][‘id’],$display)){
unset($mess[$k]);
}
}
}
// var_dump($mess);die();
return $this->render(“index”,[‘status’=>$status,’user’=>$user,’mess’=>$mess]);
}
//登录
public function actionLogin()
{
return $this->render(“login”);
}
//ajax 隐藏 不喜欢表
public function actionNot()
{
//开启session
$session = Yii::$app->session;
$session->open();
$u_id = $session->get(“u_id”);
$m_id = \Yii::$app->request->get(‘m_id’); //不喜欢的留言id
// var_dump($u_id);die();
$sql = “insert into dis_mess(u_id,m_id) VALUES (‘$u_id’,’$m_id’)”;
$res = \yii::$app->db->createCommand($sql)->query();
if($res){
return 1;
}else{
return 0;
}
}
//加载更多
public function actionDuo()
{
//开启session
$session = Yii::$app->session;
$session->open();
$u_id = $session->get(“u_id”);
//所有留言
$sql = “select * from message”;
$arr = \yii::$app->db->createCommand($sql)->queryAll();
//查询隐藏的留言
$sql = “select * from dis_mess where u_id=”.$u_id;
$display = \yii::$app->db->createCommand($sql)->queryAll();
//将m_id遍历为一维数组
foreach($display as $key =>$val){
$display[$key] = $display[$key][‘m_id’];
}
//取出隐藏留言的id,有就 删除该变量
foreach($arr as $k=>$v){
if(in_array($arr[$k][‘id’],$display)){
unset($arr[$k]);
}
}
// var_dump($arr);die();
return json_encode($arr);
}
//验证登录
//登录
public function actionLogin_check()
{
//接值
$username = \Yii::$app->request->post(‘username’);
$pwd = \Yii::$app->request->post(‘pwd’);
$sql = “select username,u_id from mess_user where username=‘”.$username.”‘ and pwd=‘”.$pwd.”‘”;
$arr = \yii::$app->db->createCommand($sql)->queryOne();
// var_dump($arr);die();
if(!$arr){
die(“密码错误”);
}else{
//登录成功
//开启session 存u_id ,user
$session = Yii::$app->session;
$session->open();
$session->set(“user”,$arr[‘username’]);
$session->set(“u_id”,$arr[‘u_id’]);
$this->redirect([‘mess/index’]);
}
}
//添加留言 1:成功
public function actionAdd()
{
//接值
$user = \Yii::$app->request->get(‘user’);
$content = \Yii::$app->request->get(‘content’);
$time = \Yii::$app->request->get(‘time’);
//添加留言
$sql = “insert into message(time,content,username) value(‘$time’,’$content’,’$user’)”;
$res = \yii::$app->db->createCommand($sql)->query();
if($res){
return 1;
}else{
return 0;
}
}
}
以上是控制器层
==============================================================
<!doctype html>
<html lang=”en”>
<head>
<meta charset=”UTF-8″>
<title>留言</title>
</head>
<body>
<center>
<a href=”<?= \yii\helpers\Url::to([‘mess/login’]) ?>”><input type=”button” value=”登录”/></a>
<input type=”text” id=”content”/><input type=”button” value=”留言” id=”btn” /><br>
留言内容:<br>
<table border=”1″ id=”table”>
<tbody id=”blog”>
<?php $num=0;?>
<?php foreach($mess as $k=>$v) { ?>
<?php if($num==5){break;} ?>
<tr>
<td><?= $v[‘username’] ?></td>
<td><?= $v[‘content’] ?></td>
</tr>
<tr m_id=”<?= $v[‘id’]?>” class=”tr”>
<td>日期:<?= $v[‘time’] ?></td>
<td><input type=”button” value=”隐藏” class=”yin”/></td>
</tr>
<?php $num++;?>
<?php } ?>
</table>
</tbody>
<input type=”button” value=”更多” id=”btn2″/>
<script src=”./js/jquery.min.js”></script>
<script>
$(function(){
//更多
$(“#btn2″).click(function(){
$.ajax({
url:”<?= \yii\helpers\Url::to([‘mess/duo’]) ?>”,
type: “GET”,
success: function(xml){
// alert(xml)
//拼接字符串
obj= $.parseJSON(xml);
var str = new String();
$.each(obj,function(i,n){
//拼接字符串
str+=”<tr>”;
str+=”<td>”+n.username+”</td>”;
str+=”<td>”+n.content+”</td>”;
str+=”</tr>”;
str+=”<tr m_id=‘”+n.id+”‘ >”;
str+=”<td>日期:”+n.time+”</td>”;
str+=”<td><input type=’button’ value=’隐藏’ class=’yin’/></td>”;
str+=”</tr>”;
})
$(“#table”).append(str);
$(“#btn2”).css(“display”,”none”);//隐藏更多按钮
}
})
})
//隐藏 问题:追加的tr触发不了点击事件
//bind live on delegate
$(“#blog”).delegate(“.yin”,”click”,function(){
// alert(“隐藏”);
var m_id = $(this).parent().parent().attr(“m_id”); //获取到id
var obj = $(this).parent().parent();
$.ajax({
url : “<?= \yii\helpers\Url::to([‘mess/not’]) ?>”,
type: “GET”,
data: “m_id=”+m_id,
success: function (msg) {
// alert(msg)
if(msg==1){
//隐藏成功 注意移除的顺序,当先杀当前变量时,当前的兄弟节点就会找不到
obj.prev().remove();
obj.remove();
}
}
})
})
//点击留言
$(“#btn”).click(function(){
//检测是否登录
var status =”<?= $status ?>”;
//未登录时
if(status==0){
alert(“请登录”);
$(“#btn”).attr(“disabled”,”true”)
}
//留言
time =”<?= date(‘Y/m/d’,time()) ?>”;
content =$(“#content”).val();
user = “<?php echo $user ?>”;
$.ajax({
url:”<?= \yii\helpers\Url::to([‘mess/add’]) ?>”,
type: “GET”,
data: “time=”+time+”&content=”+content+”&user=”+user,
success: function(msg){
//留言成功时
if(msg==1){
//拼接字符串
var str = new String();
str+=”<tr>”;
str+=”<td>”+user+”</td>”;
str+=”<td>”+content+”</td>”;
str+=”</tr>”;
str+=”<tr>”;
str+=”<td>日期:”+time+”</td>”;
str+=”<td><input type=’button’ value=’隐藏’ class=’yin’/></td>”;
str+=”</tr>”;
$(“#table”).append(str);
}
}
})
})
})
</script>
</center>
</body>
</html>