闲来没事把自己的电影网添加了个评分系统,没有用飞飞cms,还是phpcms用的习惯上手,基本没怎么修改原有的程序,主要增加如下。
首先的需要两个字段存在评分和评分人数
那么我们在后台主表添加rate评分字段,选择类型就是数字,然后就是评分人数字段pnum,类型也是数字
接着我们需要对内容的控制器添加一个方法来更新个别数据库字段phpcms/modules/content/下的index.php文件增加如下方法:
//评分页面
public function rate() {
$modelid = intval($_GET['modelid']);
$id = intval($_GET['id']);
$score = intval($_GET['score']) * 2;
$this->db->set_model($modelid);
$this->db->table_name;
$this->db->update(array('rate'=>'+='.$score,'pnum'=>'+=1','updatetime'=>time()),array('id'=>$id));
}然后我们需要去模版页面添加一个评分的展示代码和评分按钮,在模版show.html里面添加
<div class="rating">
<span class="title">我也要给这部影片打分</span>
<span class="rate_list s0">
<a id="star1" href="javascript:;" title="很差" onclick="rate_sub({$id},1,1)"></a>
<a id="star2" href="javascript:;" title="一般" onclick="rate_sub({$id},2,1)"></a>
<a id="star3" href="javascript:;" title="不错" onclick="rate_sub({$id},3,1)"></a>
<a id="star4" href="javascript:;" title="很好" onclick="rate_sub({$id},4,1)"></a>
<a id="star5" href="javascript:;" title="神作" onclick="rate_sub({$id},5,1)"></a>
</span>
<span class="rate_word">{if empty($rate)}无评分{/if}</span>
</div>
<div class="score"></div>模版底部添加JS文件的引用,我们采用API的动态调用,即使生成静态也能时时更新评分
<script type="text/javascript" src="{JS_PATH}rating.js"></script>
<script language="JavaScript" src="{APP_PATH}api.php?op=get_rate&id={$id}&modelid={$modelid}"></script>我的样式也贴出来吧
.rating{float:left;clear:both;width:360px;height:28px;border:1px solid #ddd;border-radius: 2px;line-height:28px;font-size:14px;}
.rating .title{float:left;display:block;background-color:#eee;padding:0 10px;color:#666;}
.rating .rate_list{float:left;width:140px;height:28px;margin-left:10px;}
.rating .rate_list a{float:left;display:block;width:28px;height:28px;}
.rating .s0{background:url("../images/rating.png") no-repeat 0 0;}
.rating .s1{background:url("../images/rating.png") no-repeat 0 -30px;}
.rating .s2{background:url("../images/rating.png") no-repeat 0 -60px;}
.rating .s3{background:url("../images/rating.png") no-repeat 0 -90px;}
.rating .s4{background:url("../images/rating.png") no-repeat 0 -120px;}
.rating .s5{background:url("../images/rating.png") no-repeat 0 -150px;}
.rate_word{float:left;line-height:28px;margin-left:5px;}
.score{position:absolute;top:5px;right:5px;font-size:20px;}
.score em{font-size:30px;font-weight:bold;color:#ff6633;font-family:'Georgia','Microsoft YaHei','sans-serif';}然后上面的JS文件内容如下
$(".rate_list a").hover(function(){
var statid = $(this).attr('id');
var starnum = statid.replace(/star/, "");
var id = parseInt(starnum);
for (i = 0; i <= 5; i++) {
if(id >= i){
$(".rate_list").addClass("s" + i);
$(".rate_word").html($(this).attr("title"));
}else{
$(".rate_list").removeClass("s" + i);
}
}
},function(){
var index = $(".rate_list").attr("id");
$(".rate_list").attr("class","rate_list " + index);
});
function rate_sub(cid, score, modelid){
var saveid = GetCookie('rateid');
if (saveid == cid) {
alert("您已经评过分了!");
}else{
$.get("/index.php?m=content&c=index&a=rate", {id:cid, score:score, modelid:modelid},
function(data){
alert("感谢您的参与!");
location.reload();
});
SetCookie('rateid',cid,1);
}
}
function GetCookie(c_name)
{
if (document.cookie.length > 0)
{
c_start = document.cookie.indexOf(c_name + "=")
if (c_start != -1)
{
c_start = c_start + c_name.length + 1;
c_end = document.cookie.indexOf(";",c_start);
if (c_end == -1)
{
c_end = document.cookie.length;
}
return unescape(document.cookie.substring(c_start,c_end));
}
}
return null
}
function SetCookie(c_name,value,expiredays)
{
var exdate = new Date();
exdate.setDate(exdate.getDate() + expiredays);
document.cookie = c_name + "=" +escape(value) + ((expiredays == null) ? "" : ";expires=" + exdate.toGMTString()); //使设置的有效时间正确。增加toGMTString()
}然后最后是接口的文件get_rate.php,这个文件是放在根目录的api目录下,内容如下:
<?php
defined('IN_PHPCMS') or exit('No permission resources.');
$db = '';
$db = pc_base::load_model('content_model');
$modelid = intval($_GET['modelid']);
$id = intval($_GET['id']);
$MODEL = getcache('model','commons');
$tablename = $db->table_name = $db->db_tablepre.$MODEL[$modelid]['tablename'];
$r = $db->get_one(array('id'=>$id));
if($r['rate']){
$rating = sprintf("%01.1f",$r['rate']/$r['pnum']);
$round_star = round($rating/2);
$rate_status = "<em>".$rating."</em>分";
}else{
$round_star = 0;
}
?>
$('.rate_list').addClass('<?php echo "s".$round_star;?>');
$('.rate_word').html($('#star<?php echo $round_star;?>').attr('title'));
$('.score').html('<?php echo $rate_status;?>');
$('.rate_list').attr('id','<?php echo "s".$round_star;?>');自己网站用的效果如下

测试链接:http://www.xunleigu.com/dongzuo/17572.html
内容版权声明:除非注明,否则皆为本站原创文章。
转载注明出处:https://sulao.cn/post/427
评论列表