商品评论

需求

 1 登陆之后可以评论

 2  打分1--2 差评 3 中评 4--5好评

 3  用户评论时 可以指定一个印象,如果这个印象已经存在就把数字+1

 4 评论的功能完全使用ajax制作 【商品页面有缓存,所有要使用局部不缓存】

   

 实际操作:

    1 建表

   

/************商品评论表***********/
drop table if exists p40_comment;
create table p40_comment
(
id mediumint unsigned not null auto_increment comment 'id',
goods_id mediumint unsigned not null comment '商品ID',
member_id mediumint unsigned not null comment '会员id',
content varchar(200) not null comment '内容',
addtime datetime not null comment '发表时间',
start tinyint unsigned not null comment '分值',
click_count smallint unsigned not null default '0' comment '有用的数字',
primary key(id),
key goods_id(goods_id)
)engine=InnoDB default charset=utf8 comment '评论';

/************商品评论回复表***********/
drop table if exists p40_comment_reply;
create table p40_comment_reply
(
id mediumint unsigned not null auto_increment comment 'id',
comment_id mediumint unsigned not null comment '评论ID',
member_id mediumint unsigned not null comment '会员id',
content varchar(200) not null comment '内容',
addtime datetime not null comment '发表时间',
primary key(id),
key comment_id(comment_id)
)engine=InnoDB default charset=utf8 comment '评论回复';


/************商品印象表***********/
drop table if exists p40_yinxiang;
create table p40_yinxiang
(
id mediumint unsigned not null auto_increment comment 'id',
goods_id mediumint unsigned not null comment '商品ID',
yx_name varchar(30) not null comment '印象名称',
yx_count smallint unsigned not null default '1' comment '印象的次数',
primary key(id),
key goods_id(goods_id)
)engine=InnoDB default charset=utf8 comment '印象';

--------------------------------

 2  ajax发表评论

 2.1  创建评论模型 放在后台

    

<?php
namespace AdminModel;
use ThinkModel;
class CommentModel extends Model
{
//评论时允许提交的字段
protected $insertFields='start,content,goods_id';

//发表评论时表单验证规则
protected $_validate=array(
array('goods_id','require','参数错误!',1),
array('start','1,2,3,4,5','分值只能是1-5!',1,'in'),
array('content','1,200','内容必须是1-200个字符!',1,'length'),
);

protected function _before_insert(&$data,$option)
{
$memberId=session('m_id');
if(!$memberId)
{
$this->error='必须先登录!';
return false;
}


$data['member_id']=$memberId;
$data['addtime']=date('Y-m-d H:i:s');
}

}

------------------------------------ 

  控制器代码

<?php
namespace HomeController;
use ThinkController;
class CommentController extends Controller
{
//发表评论
public function add()
{
if(IS_POST)
{
$model=D('Admin/Comment');
if($model->create(I('post.'),1))
{
if($model->add())
{
$this->success('添加成功!','',TRUE);   因为是ajax所以这里设置返回JSON
exit;
}
}
$this->error($model->getError(),'',TRUE); 
}
}
}

  ------------------------------

 2.3  修改表单 以AJAX方式提交

   1  先修改表单

   

<!-- 评论表单 start-->
<div class="comment_form mt20">
<form id="comment-form">
<input type="hidden" name="goods_id" value="<?php echo $info['id']; ?>">
<ul>
<li>
<label for=""> 评分:</label>
<input type="radio" name="start" value="5" checked="checked"/> <strong class="star star5"></strong>
<input type="radio" name="start" value="4"/> <strong class="star star4"></strong>
<input type="radio" name="start" value="3"/> <strong class="star star3"></strong>
<input type="radio" name="start" value="2"/> <strong class="star star2"></strong>
<input type="radio" name="start" value="1"/> <strong class="star star1"></strong>
</li>

<li>
<label for="">评价内容:</label>
<textarea name="content" id="" cols="" rows=""></textarea>
</li>
<li>
<label for="">&nbsp;</label>
<input type="button" value="提交评论" class="comment_btn"/>
</li>
</ul>
</form>
</div>
<!-- 评论表单 end-->

2 为提交按钮绑定点击事件点击时以ajax的方式提交


/************ajax发表评论*************/
$(".comment_btn").click(function(){
//先接收表单中的数据 格式 : nametom&age=23
var form = $("#comment-form");
var formData=form.serialize();

$.ajax({
type:"POST",
url:"<?php echo U('Comment/add'); ?>",
data:formData, //表单中要提交的数据
dataType:"json", //服务器返回的数据格式
success:function(data)
{
if(data.status == 0)
alert(data.info);

else
{
//清空表单
form.trigger("reset"); //触发表单的reset事件

}
}

});


});

3  发表评论成功之后在以上页面中显示出来新的评论

       修改发表评论的控制器

public function add()
{
if(IS_POST)
{
$model=D('Admin/Comment');
if($model->create(I('post.'),1))
{
if($model->add())
{
$this->success(array(
'face'=>session('face'),
'username'=>session('username'),
'addtime'=>date('Y-n-d H:i:s'),
'content'=>I('post.content'),
'start'=>I('post.start'),
),'',TRUE);
}
}
$this->error($model->getError(),'',TRUE);
}
}
}

把新评论拼成HTML 并显示在页面中

<!-- 评论容器 -->
<div id="comment_container"></div>

/************ajax发表评论*************/
$(".comment_btn").click(function(){
//先接收表单中的数据 格式 : nametom&age=23
var form = $("#comment-form");
var formData=form.serialize();

$.ajax({
type:"POST",
url:"<?php echo U('Comment/add'); ?>",
data:formData, //表单中要提交的数据
dataType:"json", //服务器返回的数据格式
success:function(data)
{
if(data.status == 0)
alert(data.info);

else
{
//清空表单
form.trigger("reset"); //触发表单的reset事件

//用新发表的评论数据拼出一个显示的HTML字符串
var html=' <div class="comment_items mt10 none"><div class="user_pic"><dl><dt><a href=""><img src="'+data.info.face+'" alt="" /></a></dt><dd><a href="">'+data.info.face+'</a></dd></dl></div><div class="item"><div class="title"><span>'+data.info.addtime+'</span><strong class="star star'+data.info.star+'"></strong></div><div class="comment_content">'+data.info.content+'</div><div class="btns"><a href="" class="reply">回复(0)</a><a href="" class="useful">有用(0)</a></div><div class="cornor"></div></div>';
//把整个评论的字符串转化成jq的对象
html = $(html);
// 把拼好的评论放到页面中
$("#comment_container").prepend(html);
// 让导航条直接滚动第一个评论处
$("body").animate({
"scrollTop" : "750px"
}, 1000, function(){
html.fadeIn(2000);
});
}
}

});


});

世上无难事,只怕有心人......
原文地址:https://www.cnblogs.com/gooderic/p/5791546.html