织梦分类信息模型如何添加文章的顶踩
首先打开
织梦
默认模板 default/article_article.htm, 找到这样一段代码:<div class="newdigg" id="newdigg">
<div class="diggbox digg_good" onmousemove="this.style.backgroundPosition='left bottom';" onmouseout="this.style.backgroundPosition='left top';" onclick="Javascript:postDigg('good',{dede:field.id/})">
<div class="digg_act">顶一下</div>
<div class="digg_num">({dede:field.goodpost/})</div>
<div class="digg_percent">
<div class="digg_percent_bar"><span style="width:{dede:field.goodper/}%"></span></div>
<div class="digg_percent_num">{dede:field.goodper/}%</div>
</div>
</div>
<div class="diggbox digg_bad" onmousemove="this.style.backgroundPosition='right bottom';" onmouseout="this.style.backgroundPosition='right top';" onclick="Javascript:postDigg('bad',{dede:field.id/})">
<div class="digg_act">踩一下</div>
<div class="digg_num">({dede:field.badpost/})</div>
<div class="digg_percent">
<div class="digg_percent_bar"><span style="width:{dede:field.badper/}%"></span></div>
<div class="digg_percent_num">{dede:field.badper/}%</div>
</div>
</div>
</div>
<script language="Javascript" type="text/Javascript">getDigg({dede:field.id/});</script>
不用说这段代码就是文章模型的顶踩代码, 代码调用了js函数postDigg(),找到这段js代码,要特别注意用红色标注的部分,一定要加上,因为织梦顶踩功能是ajax实现的:
<script language="Javascript" type="text/Javascript" src="{dede:global.cfg_cmsurl/}/include/dedeajax2.js"></script>
<script language="Javascript" type="text/Javascript"
function postBadGood(ftype,fid)
{
var taget_obj = document.getElementById(ftype+fid);
var saveid = GetCookie('badgoodid');
if(saveid != null)
{
var saveids = saveid.split(',');
var hasid = false;
saveid = '';
j = 1;
for(i=saveids.length-1;i>=0;i--)
{
if(saveids[i]==fid && hasid) continue;
else {
if(saveids[i]==fid && !hasid) hasid = true;
saveid += (saveid=='' ? saveids[i] : ','+saveids[i]);
j++;
if(j==10 && hasid) break;
if(j==9 && !hasid) break;
}
}
if(hasid) { alert('您刚才已表决过了喔!'); return false;}
else saveid += ','+fid;
SetCookie('badgoodid',saveid,1);
}
else
{
SetCookie('badgoodid',fid,1);
}
myajax = new DedeAjax(taget_obj,false,false,'','','');
myajax.SendGet2("{dede:field name='phpurl'/}/feedback.php?aid="+fid+"&action="+ftype+"&fid="+fid);
}
function postDigg(ftype,aid)
{
var taget_obj = document.getElementById('newdigg');
var saveid = GetCookie('diggid');
if(saveid != null)
{
var saveids = saveid.split(',');
var hasid = false;
saveid = '';
j = 1;
for(i=saveids.length-1;i>=0;i--)
{
if(saveids[i]==aid && hasid) continue;
else {
if(saveids[i]==aid && !hasid) hasid = true;
saveid += (saveid=='' ? saveids[i] : ','+saveids[i]);
j++;
if(j==20 && hasid) break;
if(j==19 && !hasid) break;
}
}
if(hasid) { alert("您已经顶过该帖,请不要重复顶帖 !"); return; }
else saveid += ','+aid;
SetCookie('diggid',saveid,1);
}
else
{
SetCookie('diggid',aid,1);
}
myajax = new DedeAjax(taget_obj,false,false,'','','');
var url = "{dede:global.cfg_phpurl/}/digg_ajax.php?action="+ftype+"&id="+aid;
myajax.SendGet2(url);
}
function getDigg(aid)
{
var taget_obj = document.getElementById('newdigg');
myajax = new DedeAjax(taget_obj,false,false,'','','');
myajax.SendGet2("{dede:global.cfg_phpurl/}/digg_ajax.php?id="+aid);
DedeXHTTP = null;
}
-->
</script>
上面这两段代码就是织梦文章顶踩功能的全部代码了, 我将这两段代码放在default/article_infos.htm 文章里面 ,在后台添加了分类信息栏目,满怀激动的添加了一条数据,果然在分类信息文章页出现了顶踩按钮!情不自禁点了顶一下, 数字加1 , 心里暗暗窃喜这么容易就是实现了
但是当我刷新页面的时候发现,顶一下数字竟然归0了。 意识到没这么简单,继续看代码!
代码中有这么一条: var url = "{dede:global.cfg_phpurl/}/digg_ajax.php?action="+ftype+"&id="+aid ,这是调用织梦插件里的plus/digg_ajax.php类, 打开这个文件,仔细看了下代码才发现问题所在!
这段php代码大致的意思是:当用户点击 顶一下或 踩一下 先检查该文章id是否存在,如果存在就select主表dede_archives, 大家看出问题了吗? 分类信息模型是独立模型, 他的内容是不在dede_archives表中 ,而是放在表:dede_addoninfos中,知道问题所在就好办了,我新建了一个php文件:digg_ajax_infos.php,将$maintable = 'dede_archives'; 改成$maintable = 'dede_addoninfos'; 同时将上面js代码中的digg_ajax.php改成/digg_ajax_infos.php,再试了一下就OK了,(digg_ajax_infos.php里的sql语句还有点问题,不和大家细说,直接贴代码自己看吧)
<?php
/**
*
* 文档digg处理ajax文件 牛人织梦修改!
*
* @version $Id: digg_ajax.php 2 13:00 2011/11/25 tianya $
* @package DedeCMS.Plus
* @copyright Copyright (c) 2007 - 2010, DesDev, Inc.
* @license http://help.dedecms.com/usersguide/license.html
* @link http://www.dedecms.com
*/
require_once(dirname(__FILE__)."/../include/common.inc.php");
$action = isset($action) ? trim($action) : '';
$id = empty($id)? 0 : intval(preg_replace("/[^\d]/",'', $id));
helper('cache');
if($id < 1)
{
exit();
}
$maintable = 'dede_addoninfos';
$prefix = 'diggCache';
$key = 'aid-'.$id;
$row = GetCache($prefix, $key);
if(!is_array($row) || $cfg_digg_update==0)
{
$row = $dsql->GetOne("SELECT goodpost,badpost,scores FROM `$maintable` WHERE aid='$id' ");
if($cfg_digg_update == 0)
{
if($action == 'good')
{
$row['goodpost'] = $row['goodpost'] + 1;
$dsql->ExecuteNoneQuery("UPDATE `$maintable` SET scores = scores + {$cfg_caicai_add},goodpost=goodpost+1,lastpost=".time()." WHERE aid='$id'");
}
共2页: 上一页