,首先后台执行SQL, ,ALTER TABLE dede_archives ADD zan int(11) NOT NULL DEFAULT '0' AFTER weight;, ,也就是在 dede_archives 表中增加 zan 这个字段,加到weight这个字段后边。, ,然后执行, ,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,这个表是保存被赞的文档aid 及IP的,为了检测是否赞过的IP, ,然后在include/extend.func.php 中增加以下函数,用来获取文档被赞次数的, , ,function zan($aid),{, global $dsql;, $row = $dsql->GetOne("Select id,zan From dede_archives where id='".$aid."'");, return $row['zan'];, }, , ,打开模板templets/default/article_article.htm, ,加入css, ,.zan p{,background:#000; opacity:.8;filter:alpha(opacity=80);} ,.zan p a{padding-left:30px; height:24px; background:url(/images/heart.png) no-repeat ,4px -1px;color:#fff; font-weight:bold; font-size:14px} ,.zan p a:hover{background-position:4px -25px;text-decoration:none}, ,加入JS,记得引入jquery.js,<script type="text/javascript" src="/jquery.js"></script>, ,$(function(){ , $("p a").click(function(){ , var zan = $(this); , var id = zan.attr("rel"); //对应id , zan.fadeOut(300); //渐隐效果 , $.ajax({ , type:"POST", , url:"/zan.php", , data:"id="+id, , cache:false, //不缓存此页面 , success:function(data){ , zan.html(data); , zan.fadeIn(300); //渐显效果 , } , }); , return false; , }); ,});, ,加入, ,<div class="zan"><p><a href="#" ,title="赞一个 "class="img_on" rel="{dede:field.id/}">{dede:field.id function="zan(@me)"/}</a></p></div>, , ,建立zan.php 放到根目录, ,<?php,require_once (dirname(__FILE__) . "/include/common.inc.php");,$ip =getip(); //获取用户IP ,$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'; ");//写入赞数, , $dsql->ExecuteNoneQuery("insert into dede_zan (aid,ip) values ('$id','$ip'); ");//写入IP,及被赞的AID , , $rows = $dsql->GetOne("Select zan from dede_archives where id='".$id."'");//获取被赞的数量, $zan = $rows['zan']; //获取赞数值 , echo $zan; ,}else{ , echo "赞过了.."; ,}, ,好了,点赞功能开发好了。, , , ,