EMLOG非插件实现一键点赞!

2017-9-25 0 PHP代码 蓝优

本文由蓝优网转载发布:用法很简单,在module.php里面加如以下代码
<?php //点赞
function addzan(){
$DB = MySql::getInstance();
if($DB->num_rows($DB->query("show columns from ".DB_PREFIX."blog like 'lemonzan'")) == 0){
$sql = "ALTER TABLE ".DB_PREFIX."blog ADD lemonzan int unsigned NOT NULL DEFAULT '0'";
$DB->query($sql);}}addzan();
function update($logid){
$logid = intval($_POST['id']);
$DB = Database::getInstance();
$DB->query("UPDATE " . DB_PREFIX . "blog SET lemonzan=lemonzan+1 WHERE gid=$logid");
setcookie('lemonzanpd_'. $logid, 'true', time() + 31536000);}
function lemoninit() {if( @$_POST['plugin'] == 'lemonzanpd' &&@$_POST['action'] == 'lemonzan' &&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 lemonzan FROM " . DB_PREFIX . "blog WHERE gid=$id";
$res = $DB->query($sql);
$row = $DB->fetch_array($res);
$arr[$id] = intval($row['lemonzan']);
return $arr[$id];}
?>
再在文章页加入以下代码,CSS自己搞定
<div class="nrzan"><a class="lemonzanpd" data-lemonzanpd="<?php echo $logData['logid'] ; ?>" title="喜欢这篇文章就赞一个吧!"><i class="fa fa-thumbs-up"></i>赞 <b><u><?php echo(isset($logData['lemonzan'])?$logData['lemonzan']:getnum($logData['logid']));?></u></b> 次</a></div>
再在你的JS文件里加上以下代码就OK了
$(document).on('click', '.lemonzanpd',
function() {
var a = $(this),
id = a.data('lemonzanpd');
if (lemonzanpd_check(id)) {
alert('您已赞过本文!');
} else {
$.post('', {
plugin: 'lemonzanpd',
action: 'lemonzan',
id: id
},
function(b) {
a.find('u').html(b);
lemonzanpd_(a);
});
}
});
function lemonzanpd_check(id) {
return new RegExp('lemonzanpd_' + id + '=true').test(document.cookie);
}
$('[data-lemonzanpd]').each(function() {
var a = $(this),
id = a.data('lemonzanpd');
if (lemonzanpd_check(id)) {
lemonzanpd_(a);
} else {
a.attr('title', '给本帅比来点动力吧!')
}
});
function lemonzanpd_(a) {
a.css('cursor', 'not-allowed').attr('title', '您已赞过本文!');
}
标签: EMLOG教程
版权声明:若无特殊注明,本文皆《蓝优》原创,转载请保留文章出处。
本文链接:EMLOG非插件实现一键点赞! - http://www.92mo.cn/daima/1.html

发表新评论

快捷功能: