Commit 05653d71 authored by 蒋林's avatar 蒋林

评论

parent 5462db7f
...@@ -51,6 +51,17 @@ class mysqlquery ...@@ -51,6 +51,17 @@ class mysqlquery
$this->r=mysqli_fetch_array($this->sql); $this->r=mysqli_fetch_array($this->sql);
return $this->r; return $this->r;
} }
function fetchassoc($sql)
{
$this->r = mysqli_fetch_assoc($sql);
return $this->r;
}
function fetchassoc1($sql)
{
$this->sql=$this->query($sql);
$this->r = mysqli_fetch_assoc($this->sql);
return $this->r;
}
//执行mysqli_num_rows() //执行mysqli_num_rows()
function num($query)//此类的参数是$query就是sql语句 function num($query)//此类的参数是$query就是sql语句
{ {
......
...@@ -7,29 +7,32 @@ require("../class/q_functions.php"); ...@@ -7,29 +7,32 @@ require("../class/q_functions.php");
$link=db_connect(); $link=db_connect();
$empire=new mysqlquery(); $empire=new mysqlquery();
$mod = isset($_GET['mod']) ? $_GET['mod']: '';
$user = islogin(); $user = islogin();
if (empty($user)) { if (empty($user)) {
returnmsg(false, '请登录'); returnmsg(false, '请登录');
} }
$newsid = isset($_POST['newsid']) ? intval($_POST['newsid']) : 0;
if (!$newsid) {
returnmsg(false, 'newsid参数错误');
}
if ($_POST['mod'] == 'create') { if ($mod == 'create') {
$newsid = isset($_POST['newsid']) ? $_POST['newsid'] : 0; $content = isset($_POST['content']) ? addslashes($_POST['content']) : '';
$newsid = $newsid + 0;
$content = isset($_POST['content']) ? $_POST['content'] : '';
if ($newsid == 0) {
returnmsg(false, 'newsid参数错误');
}
if ($content == '') { if ($content == '') {
returnmsg(false, '评论内容不能为空'); returnmsg(false, '评论内容不能为空');
} }
}
$result= newspls(89, 100); createComment($newsid, $content, $user);
echo "<pre>"; } elseif ($mod == 'reply') {
print_r($result);
function createComments($user, $newsid, $content, $plid=0) { } else {
comments($newsid,12);
}
//创建评论
function createComment($newsid, $content, $user) {
global $empire,$dbtbpre; global $empire,$dbtbpre;
$table = $dbtbpre.'ecms_comments'; $table = $dbtbpre.'ecms_comments';
...@@ -47,40 +50,66 @@ function createComments($user, $newsid, $content, $plid=0) { ...@@ -47,40 +50,66 @@ function createComments($user, $newsid, $content, $plid=0) {
if (!$id) { if (!$id) {
returnmsg(false, '评论失败'); returnmsg(false, '评论失败');
} else { } else {
returnmsg(true, '评论成功'); returnmsg(true, '评论成功', comment($id));
} }
} }
function newspls($newsid, $num) { //回复评论
function replyComment($plid, $content, $user) {
global $empire,$dbtbpre; global $empire,$dbtbpre;
$table = $dbtbpre.'ecms_comments';
$sql = "select id,restb from {$dbtbpre}ecms_news where id={$newsid}"; $sql = "select * from $table where id={$plid}";
$news = $empire->fetch1($sql); $comment = $empire->fetch1($sql);
if (empty($news)) { if (empty($comment)) {
returnmsg(false, '该内容不存在'); returnmsg(false, '该评论不存在');
} }
$ip = $_SERVER['REMOTE_ADDR'];
$now = time();
$sql = "insert into {$table} (user_id,classid,newsid,content,ip,created_at,updated_at) VALUES ({$user['userid']}, {$comment['classid']}, {$comment}, '{$content}', '{$ip}', {$now}, {$now})";
$empire->query($sql);
$id = $empire->lastid();
$result = []; if (!$id) {
$query = $empire->query("select * from {$dbtbpre}enewspl_{$news['restb']} where id={$newsid} and checked=0 order by plid desc limit {$num}"); returnmsg(false, '评论失败');
while ($row = $empire->fetch($query)) { } else {
$row['avatar'] = "http://u.ibanshou.cn/avatar?n=".urlencode($row['username'])."&s=m"; returnmsg(true, '评论成功', comment($id));
if ($row['userid']) { }
$member = $empire->fetch1("select userid,avatar from {$dbtbpre}enewsmember where userid={$row['userid']}"); }
$row['avatar'] = $member['avatar'];
} //评论列表
function comments($newsid, $num) {
global $empire,$dbtbpre;
$tablecomment = $dbtbpre.'ecms_comments';
$tablemember = $dbtbpre.'enewsmember';
$sql = "select c.*,u.avatar,u.nickname,u.username from $tablecomment c left join {$tablemember} u on c.user_id=u.userid where newsid={$newsid} and c.reply_id=0 order by c.id desc limit {$num}";
$query = $empire->query($sql);
$result[] = tplpl($row); $list = [];
while ($row = $empire->fetchassoc($query)) {
$list[] = handelcomment($row);
} }
return $result; returnmsg(true, '', $list);
} }
function tplpl($pl) { //获取评论
$syatime = date('Y.m.d H:i:s', $pl['syatime']); function comment($id) {
global $empire,$dbtbpre;
$tablecomment = $dbtbpre.'ecms_comments';
$tablemember = $dbtbpre.'enewsmember';
$sql = "select c.*,u.avatar,u.nickname,u.username from $tablecomment c left join {$tablemember} u on c.user_id=u.userid where c.id={$id} limit 1";
return handelcomment($empire->fetchassoc1($sql));
}
return $pl; function handelcomment($comment) {
$comment['created_at'] = date('Y-m-d H:i:s', $comment['created_at']);
return $comment;
} }
function returnmsg($status=false, $msg='', $data='') { function returnmsg($status=false, $msg='', $data='') {
$return['status'] = $status ? true : false; $return['status'] = $status ? true : false;
$return['msg'] = $msg; $return['msg'] = $msg;
......
//加载评论
var newsid = $("input[name='newsid']").val();
comments(newsid);
//评论
$(".send-pl").bind("click", function () {
var data = {
'newsid': newsid,
'content': $("textarea[name='content']").val()
};
var self = this;
if (!data['newsid']) {
pop('参数错误');
return false;
}
if (!data['content']) {
pop('评论内容不能为空');
return false;
}
$(self).attr("disabled", true);
$.post("/e/pl/comments.php?mod=create", data, function (e) {
$(self).attr("disabled", false);
if (e.status) {
$("textarea[name='content']").val('');
var html = tpl(e.data);
$(".comment-list").prepend(html);
$(".comment-send-box").hide();
$('.mask').hide();
} else {
pop(e.msg);
}
}, 'json');
});
function comments(newsid) {
$.post("/e/pl/comments.php", {newsid:newsid}, function (e) {
if (!$.isEmptyObject(e.data)) {
var comments = e.data;
var html = '';
for (i in comments) {
html += tpl(comments[i]);
}
$(".comment-list").html(html);
}
}, 'json');
}
function tpl(comment) {
var html = '<li>\
<img src="'+comment.avatar+'">\
<div class="name">'+comment.nickname+'</div>\
<div class="comment-main">'+comment.content+'</div>\
<div class="comment-footbar clearfix">\
<span>'+comment.created_at+'</span>\
<a href="javascript:replyComment('+comment.id+');" class="pl-reply"><i class="iconfont icon-comment_light"></i>回复</a> \
<a href="javascript:;" class="pl-zan" data-id="'+comment.id+'" style="display: none"><i class="iconfont icon-zan"></i>赞</a> \
</div>\
</li>';
return html;
}
//回复评论
function replyComment(id) {
$('.mask').show();
$(".comment-reply").show();
console.log(id);
}
function pop(msg) {
alert(msg);
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment