PHP、MySql、jQuery 实现“顶”与“踩”投票功能

WBOY
Lepaskan: 2016-07-25 08:55:30
asal
829 orang telah melayarinya
  1. CREATE TABLE IF NOT EXISTS `votes` (
  2. `id` int(10) NOT NULL AUTO_INCREMENT,
  3. `likes` int(10) NOT NULL DEFAULT '0',
  4. `unlikes` int(10) NOT NULL DEFAULT '0',
  5. PRIMARY KEY (`id`)
  6. ) ENGINE=MyISAM DEFAULT CHARSET=utf8;
  7. INSERT INTO `votes` (`id`, `likes`, `unlikes`) VALUES
  8. (1, 30, 10);
  9. CREATE TABLE IF NOT EXISTS `votes_ip` (
  10. `id` int(10) NOT NULL,
  11. `vid` int(10) NOT NULL,
  12. `ip` varchar(40) NOT NULL
  13. ) ENGINE=MyISAM DEFAULT CHARSET=utf8;
复制代码

2,html页面部分 在页面中,有两个分别表示“顶”和“踩”的按钮,即#dig_up和#dig_down,按钮上记录了投票的票数以及分别所占的百分比,非常直观的对比投票结果。

  1. 很好,很强大!

  • 太差劲了!

  • 复制代码

    3,css代码部分 必须使用CSS来美化页面,使用一张图diggs.png来定位不同的按钮背景,通过设置position来定位各元素之间的位置关系。

    1. .digg{width:420px; height:120px; margin:80px auto 20px auto; position:relative}
    2. #dig_up,#dig_down{width:200px; height:48px; margin:10px; position:relative;
    3. border:1px solid #d3d3d3; padding-left:42px; cursor:pointer}
    4. .digup{background:url(diggs.png) no-repeat 4px 2px;}
    5. .digup_on{background:url(diggs.png) no-repeat 4px -49px;}
    6. .digdown{background:url(diggs.png) no-repeat 4px -102px;}
    7. .digdown_on{background:url(diggs.png) no-repeat 4px -154px;}
    8. #num_up,#num_down{position:absolute; right:6px; top:18px; font-size:20px;}
    9. #dig_up p{height:24px; line-height:24px; color:#360}
    10. #dig_down p{height:24px; line-height:24px; color:#f30}
    11. .bar{width:100px; height:12px; line-height:12px; border:1px solid #f0f0f0;
    12. position:relative; text-align:center}
    13. .bar span{display:block; height:12px; }
    14. .bar i{position:absolute; top:0; left:104px;}
    15. #bar_up span{background:#360}
    16. #bar_down span{background:#f60}
    17. #msg{position:absolute; right:20px; top:40px; font-size:18px; color:#f00}
    复制代码

    2,jquery部分 本示例还依赖于jQuery,因此一定不能忘了在页面中先载入jquery库文件。 首先,jQuery要处理的是鼠标分别滑向两个投票按钮时变换的背景图片,通过addClass()和removeClass()来实现。

    1. $(function(){
    2. //鼠标滑向和离开投票按钮时,变换背景样式
    3. $("#dig_up").hover(function(){
    4. $(this).addClass("digup_on");
    5. },function(){
    6. $(this).removeClass("digup_on");
    7. });
    8. $("#dig_down").hover(function(){
    9. $(this).addClass("digdown_on");
    10. },function(){
    11. $(this).removeClass("digdown_on");
    12. });
    13. //初始化数据
    14. getdata("do.php",1);
    15. //单击“顶”时
    16. $("#dig_up").click(function(){
    17. getdata("do.php?action=like",1);
    18. });
    19. //单击“踩”时
    20. $("#dig_down").click(function(){
    21. getdata("do.php?action=unlike",1);
    22. });
    23. });
    复制代码

    然后,初始化数据,就是页面加载后,要显示初始的已经投票的结果,包括各投票数和所占百分比。 将获取数据的操作写在一个自定义函数getdata()中,通过传递不同的请求地址和id参数来完成数据的载入。 函数getdata()中,向URL发送一个ajax请求,根据后台处理的返回结果,如果投票成功则更改页面中相应的元素内容,包括投票数和所占百分比。

    1. function getdata(url,sid){
    2. $.getJSON(url,{id:sid},function(data){
    3. if(data.success==1){//投票成功
    4. $("#num_up").html(data.like);
    5. //通过控制宽度来显示百分比进度条效果
    6. $("#bar_up span").css("width",data.like_percent);
    7. $("#bar_up i").html(data.like_percent);
    8. $("#num_down").html(data.unlike);
    9. $("#bar_down span").css("width",data.unlike_percent);
    10. $("#bar_down i").html(data.unlike_percent);
    11. }else{//投票失败 bbs.it-home.org
    12. $("#msg").html(data.msg).show().css({'opacity':1,'top':'40px'})
    13. .animate({top:'-50px',opacity:0}, "slow");
    14. }
    15. });
    16. }
    复制代码

    4,php代码部分 数据的获取都是通过do.php来完成,do.php根据前端页面传递的参数,连接数据库,根据条件判断分别进入“顶”、“踩”和初始数据处理模块,以下是do.php模块代码结构:

    1. include_once("connect.php");//连接数据库
    2. $action = $_GET['action'];
    3. $id = 1;
    4. $ip = get_client_ip();//获取ip
    5. if($action=='like'){//顶
    6. likes(1,$id,$ip);
    7. }elseif($action=='unlike'){//踩
    8. likes(0,$id,$ip);
    9. }else{
    10. echo jsons($id);
    11. }
    复制代码

    函数likes()用来处理“顶”和“踩”投票模块,首先是判断用户IP是否已经投票过了,如果已经投票则直接返回相应提示,如果用户IP没有投票记录则更新votes表,将对应的投票数加1,然后向votes_ip表中插入该用户的IP记录,如果操作成功,则调用jsons()输出投票后的投票数和所占百分比等数据,否则输入操作失败的提示信息。

    1. function likes($type,$id,$ip){
    2. $ip_sql=mysql_query("select ip from votes_ip where vid='$id' and ip='$ip'");
    3. $count=mysql_num_rows($ip_sql);
    4. if($count==0){//还没有顶过
    5. if($type==1){//顶
    6. $sql = "update votes set likes=likes+1 where id=".$id;
    7. }else{//踩
    8. $sql = "update votes set unlikes=unlikes+1 where id=".$id;
    9. }
    10. mysql_query($sql);
    11. $sql_in = "insert into votes_ip (vid,ip) values ('$id','$ip')";
    12. mysql_query($sql_in);
    13. if(mysql_insert_id()>0){
    14. echo jsons($id);
    15. }else{
    16. $arr['success'] = 0;
    17. $arr['msg'] = '操作失败,请重试';
    18. echo json_encode($arr);
    19. }
    20. }else{
    21. $msg = $type==1?'您已经顶过了':'您已经踩过了';
    22. $arr['success'] = 0;
    23. $arr['msg'] = $msg;
    24. echo json_encode($arr);
    25. }
    26. }
    复制代码

    函数jsons()用来读取votes表中相应id的投票数,并计算百分比,最后将这些信息以json格式输出,供前端页面使用。

    1. function jsons($id){
    2. $query = mysql_query("select * from votes where id=".$id);
    3. $row = mysql_fetch_array($query);
    4. $like = $row['likes'];
    5. $unlike = $row['unlikes'];
    6. $arr['success']=1;
    7. $arr['like'] = $like;
    8. $arr['unlike'] = $unlike;
    9. $like_percent = round($like/($like+$unlike),3)*100;
    10. $arr['like_percent'] = $like_percent.'%';
    11. $arr['unlike_percent'] = (100-$like_percent).'%';
    12. return json_encode($arr);
    13. }
    复制代码

    该实例可以应用到常见的"赞"、同意和反对评论、投票系统等场景中,运用了Ajax原理实现的前后端交互功能。 do.php中还有个函数get_client_ip()用来获取用户真实IP,在本文提供的源码包中,大家可以下载研究下。



    sumber:php.cn
    Kenyataan Laman Web ini
    Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
    Tutorial Popular
    Lagi>
    Muat turun terkini
    Lagi>
    kesan web
    Kod sumber laman web
    Bahan laman web
    Templat hujung hadapan
    Tentang kita Penafian Sitemap
    Laman web PHP Cina:Latihan PHP dalam talian kebajikan awam,Bantu pelajar PHP berkembang dengan cepat!