本代码来源于网络,具体谁先写出来,舍力表示不知道,本站只做收集分享之用途,本人也不喜欢用插件,好了,废话不多说,下面舍力说说具体的操作方法
用法很简单,在module.php里面加如以下代码
代码语言:javascript复制<?php //点赞
function syzan(){
$DB = MySql::getInstance();
if($DB->num_rows($DB->query("show columns from ".DB_PREFIX."blog like 'slzan'")) == 0){
$sql = "ALTER TABLE ".DB_PREFIX."blog ADD slzan int unsigned NOT NULL DEFAULT '0'";
$DB->query($sql);}}syzan();
function update($logid){
$logid = intval($_POST['id']);
$DB = Database::getInstance();
$DB->query("UPDATE " . DB_PREFIX . "blog SET slzan=slzan 1 WHERE gid=$logid");
setcookie('slzanpd_'. $logid, 'true', time() 31536000);}
function lemoninit() {if( @$_POST['plugin'] == 'slzanpd' &&@$_POST['action'] == 'slzan' &&isset($_POST['id'])){
$id = intval($_POST['id']);
header("Access-Control-Allow-Origin: *");
update($id);echo getnum($id);die;}}lemoninit();
function getnum($id){
static $arr = array();
$DB = Database::getInstance();
if(isset($arr[$logid])) return $arr[$logid];
$sql = "SELECT slzan FROM " . DB_PREFIX . "blog WHERE gid=$id";
$res = $DB->query($sql);
$row = $DB->fetch_array($res);
$arr[$id] = intval($row['slzan']);
return $arr[$id];}
?>
再在文章页加入以下代码,CSS自己搞定
代码语言:javascript复制<a class="slzanpd" data-slzanpd="<?php echo $logData['logid'];?>" title="喜欢这篇文章就赞一个吧!">赞 (<?php echo(isset($logData['slzan'])?$logData['slzan']:getnum($logData['logid']));?>次)</a>
再在你的JS文件里加上以下代码就OK了
代码语言:javascript复制$(document).on('click', '.slzanpd',
function() {
var a = $(this),
id = a.data('slzanpd');
if (slzanpd_check(id)) {
alert('您已赞过本文!');
} else {
$.post('', {
plugin: 'slzanpd',
action: 'slzan',
id: id
},
function(b) {
a.find('u').html(b);
slzanpd_(a);
});
}
});
function slzanpd_check(id) {
return new RegExp('slzanpd_' id '=true').test(document.cookie);
}
$('[data-slzanpd]').each(function() {
var a = $(this),
id = a.data('slzanpd');
if (slzanpd_check(id)) {
slzanpd_(a);
} else {
a.attr('title', '给舍力来点动力吧!')
}
});
function slzanpd_(a) {
a.css('cursor', 'not-allowed').attr('title', '您已赞过本文!');
}