phpcms如何添加顶踩功能步骤:
1、在后台模型增加两个字段,一个goodpost,一个badpost;这个步骤简单,按后台新增加字段下一步就行了。
2、在模块/phpcms/modules/content/增加扩展函数newindex.php,代码如下:
<?php defined(‘IN_PHPCMS‘) or exit(‘No permission resources.‘); class newindex{ function __construct(){ $this->db=pc_base::load_model(content_model); } //评价 public function digg () { $modelid = intval($_GET[‘modelid‘]); $id = intval($_GET[‘id‘]); $digg = $_GET[‘digg‘]; $this->db->table_name = ‘v9_news‘; $data = $this->db->get_one(array(‘id‘=>$id),‘goodpost,badpost‘); if ($digg=="good"){ $this->db->update(array(‘goodpost‘=>‘+=1‘),array(‘id‘=>$id)); echo ++$data[‘goodpost‘]; }else { $this->db->update(array(‘badpost‘=>‘+=1‘),array(‘id‘=>$id)); echo ++$data[‘badpost‘]; } } } ?>
3、在/statics/js/加入digg.js,js代码如下:
function digg(cid,d,mid){ var saveid = GetCookie(‘diggid‘); if (saveid == cid) { alert("已经喜欢过了!"); return false; } else{ $.get("/index.php?m=content&c=index&a=digg", { modelid:mid, id: cid, digg:d}, function (r){ if(d=="good"){ $(‘#up‘).html(r); } else { $(‘#down‘).html(r); } }); SetCookie(‘diggid‘,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() }
4、在顶踩页面加入digg.js,然后在显示顶踩数标签处加入digg()函数就行了。
时间: 2024-10-10 11:13:12