温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

PHP+Mysql怎么实现网站顶和踩投票功能

发布时间:2021-08-09 22:16:02 来源:亿速云 阅读:149 作者:chen 栏目:编程语言

本篇内容介绍了“PHP+Mysql怎么实现网站顶和踩投票功能”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!

首先我们在页面上放置“顶”和“踩”的按钮,即#dig_up和#dig_down,按钮上分别记录了投票的票数以及所占的百分比。

<div class="digg">  
    <div id="dig_up" class="digup"> 
        <span id="num_up"></span> 
        <p>很好,很强大!</p> 
        <div id="bar_up" class="bar"><span></span><i></i></div> 
    </div> 
       <div id="dig_down" class="digdown"> 
        <span id="num_down"></span> 
        <p>太差劲了!</p> 
        <div id="bar_down" class="bar"><span></span><i></i></div> 
    </div> 
    <div id="msg"></div> 
</div>
$(function(){ 
    //当鼠标悬浮和离开两个按钮时,切换按钮背景样式 
    $("#dig_up").hover(function(){ 
        $(this).addClass("digup_on"); 
    },function(){ 
        $(this).removeClass("digup_on"); 
    }); 
    $("#dig_down").hover(function(){ 
        $(this).addClass("digdown_on"); 
    },function(){ 
        $(this).removeClass("digdown_on"); 
    }); 
     
    //初始化数据 
    getdata("ajax.php",1); 
     
    //单击“顶”时 
    $("#dig_up").click(function(){ 
        getdata("ajax.php?action=like",1); 
    }); 
    //单击“踩”时 
    $("#dig_down").click(function(){ 
        getdata("ajax.php?action=unlike",1); 
    }); 
});


函数getdata()

function getdata(url,sid){ 
    $.getJSON(url,{id:sid},function(data){ 
        if(data.success==1){//投票成功 
            $("#num_up").html(data.like); 
            //通过控制宽度来显示百分比进度条效果 
            $("#bar_up span").css("width",data.like_percent); 
            $("#bar_up i").html(data.like_percent); 
            $("#num_down").html(data.unlike); 
            $("#bar_down span").css("width",data.unlike_percent); 
            $("#bar_down i").html(data.unlike_percent); 
        }else{//投票失败 
            $("#msg").html(data.msg).show().css({'opacity':1,'top':'40px'}) 
            .animate({top:'-50px',opacity:0}, "slow"); 
        } 
    }); 
}


ajax.php

$action = $_GET['action']; 
$id = 1; 
$ip = get_client_ip();//获取当前ip 
 
if ($action == 'like') { 
    likes(1, $id, $ip); 
} elseif ($action == 'unlike') { 
    likes(0, $id, $ip); 
} else { 
    echo jsons($id); 
}


投票的表结构

CREATE TABLE IF NOT EXISTS `votes` ( 
  `id` int(10) NOT NULL AUTO_INCREMENT, 
  `likes` int(10) NOT NULL DEFAULT '0', 
  `unlikes` int(10) NOT NULL DEFAULT '0', 
  PRIMARY KEY (`id`) 
) ENGINE=MyISAM  DEFAULT CHARSET=utf8; 
 
 
INSERT INTO `votes` (`id`, `likes`, `unlikes`) VALUES 
(1, 30, 10); 
 
CREATE TABLE IF NOT EXISTS `votes_ip` ( 
  `id` int(11) NOT NULL AUTO_INCREMENT, 
  `vid` int(11) NOT NULL, 
  `ip` varchar(20) NOT NULL, 
  PRIMARY KEY (`id`) 
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=3 ;

“PHP+Mysql怎么实现网站顶和踩投票功能”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注亿速云网站,小编将为大家输出更多高质量的实用文章!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI