欢迎访问 舍力博客(www.shuyong.net)
<?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自己搞定
<a class="slzanpd" data-slzanpd="<?php echo $logData['logid'];?>" title="喜欢这篇文章就赞一个吧!">赞 (<?php echo(isset($logData['slzan'])?$logData['slzan']:getnum($logData['logid']));?>次)</a>再在你的JS文件里加上以下代码就OK了
$(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', '您已赞过本文!');
}
留言/评论:◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。