给文章点赞是目前比较流行的功能,最常见的就是微信朋友圈、微博点赞了,那么emlog的文章可不可以实现点赞功能?答案是肯定的,下面博客吧分享一下emlog文章实现点赞功能的代码。
具体操作步骤:
1、在当前主题的module.php文件添加以下代码:
<?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];}?>
2、在主题的header.php文件的前面添加以下代码:
<script type="text/javascript">$(document).on('click', '.slzanpd', function() s),
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() s),
id = a.data('slzanpd');if (slzanpd_check(id)) {
slzanpd_(a);} else {
a.attr('title', '给舍力来点动力吧!')}});function slzanpd_(a) {
a.css('cursor', 'not-allowed').attr('title', '您已赞过本文!');}</script>
3、在主题的echo_log.php文件的<?php echo $log_content; ?>下面添加以下代码:
<div class="ding">
<a class="slzanpd" data-slzanpd="<?php echo $logData['logid'];?>" title="喜欢这篇文章就赞一个吧!">赞(<?php echo(isset($logData['slzan'])?$logData['slzan']:getnum($logData['logid']));?>次)</a>
</div>
4、在主题的main.css文件添加以下代码:
.ding {overflow:hidden; font-size:14px;}.ding a {display:block; float:left; background-color:#1F90DE; color:#fff; border-radius:3px; padding:5px 30px; cursor:pointer;}.ding a:hover {color:#fff;}
保存文件后,打开文章页面,在内容底部即会出现点赞按钮。
THE END