織夢如何做點贊功能開發(fā),首先備份數(shù)據(jù)庫
系統(tǒng)》數(shù)據(jù)庫命令
ALTER TABLE dede_archives ADD zan int(11) NOT NULL DEFAULT '1200' AFTER weight;
繼續(xù)
CREATE TABLE IF NOT EXISTS `dede_zan` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`aid` int(11) NOT NULL,
`ip` varchar(40) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8
打開文章模板
在自己需要的位置加入
<div class="zan-num m-doc-qq-xx-zan" dir="{dede:field.id/}"><i class="iconfont icon-dianzan"></i><br />
<span data-role="like" class="zan-size">{dede:field.id function="zan(@me)"/}</span> </div>
核心代碼為最大層加class為zan-num 控制點擊,dir 控制文章ID ,zan-size控制點贊成功的數(shù)字
底部加入腳本,需要引入jq
<script language="javascript" type="text/javascript"> $(function(){ $(".zan-num").click(function(){ var zan = $(this); var id = zan.attr("dir"); //對應(yīng)id www.lol9.cn // zan.find("i").fadeOut(300); //漸隱效果 $.ajax({ type:"POST", url:"/zan.php", data:"id="+id, cache:false, //不緩存此頁面 success:function(data){ zan.find(".zan-size").html(data); zan.css("background-color","#f88120").fadeIn(300); //漸顯效果 } }); return false; }); }); </script>
在/include/extend.func.php 最后加入函數(shù)zan
function zan($aid) { global $dsql; $row = $dsql->GetOne("Select id,zan From dede_archives where id='".$aid."'"); return $row['zan']; }
最后在根目錄加入加入zan.php
<?php require_once (dirname(__FILE__) . "/include/common.inc.php"); $ip =getip(); //獲取用戶IP www.lol9.cn $id = $_POST['id']; if(!isset($id) || empty($id)) exit; //查詢已贊過的IP $dsql->SetQuery("SELECT ip FROM dede_zan WHERE aid='".$id."' and ip='$ip'"); $dsql->Execute(); $count = $dsql->GetTotalRow(); if($count==0){ //如果沒有記錄 $dsql->ExecuteNoneQuery("update dede_archives set zan=zan+1 where id='$id'; ");//寫入贊數(shù) www.lol9.cn $dsql->ExecuteNoneQuery("insert into dede_zan (aid,ip) values ('$id','$ip'); ");//寫入IP,及被贊的AID $rows = $dsql->GetOne("Select zan from dede_archives where id='".$id."'");//獲取被贊的數(shù)量 $zan = $rows['zan']; //獲取贊數(shù)值 echo $zan; }else{ echo "贊過了.."; }