PHP+jQuery+MySql实现红蓝投票功能

 3428

这是一个非常实用的投票实例,应用在双方观点对抗投票场景。用户可以选择支持代表自己观点的一方进行投票,本文以红蓝双方投票为例,通过前后台交互,直观展示红蓝双方投票数和所占比例,应用非常广泛。


PHP+jQuery+MySql实现红蓝投票功能


HTML

我们需要在页面中展示红蓝双方的观点,以及对应的投票数和比例,以及用于投票交互的手型图片,本例以#red和#blue分别表示红蓝双方。.redhand和.bluehand用来做手型投票按钮,.redbar和.bluebar展示红蓝双方比例调,#red_num和#blue_num展示双方投票数。

  1. <div class="vote">
  2.     <div class="votetitle">您对Helloweba提供的文章的看法?</div>
  3.     <div class="votetxt">非常实用<span>完全看不懂</span></div>
  4.     <div class="red" id="red">
  5.         <div class="redhand"></div>
  6.         <div class="redbar" id="red_bar">
  7.             <span></span>
  8.             <p id="red_num"></p>
  9.         </div>
  10.     </div>
  11.     <div class="blue" id="blue">
  12.         <div class="bluehand"></div>
  13.         <div class="bluebar" id="blue_bar">
  14.             <span></span>
  15.             <p id="blue_num"></p>
  16.         </div>
  17.     </div>
  18. </div>

CSS

使用CSS将页面美化,加载背景图片,确定相对位置等等,你可以直接复制以下代码,在自己的项目中稍作修改即可。

  1. .vote{width:288px; height:220px; margin:60px auto 20px auto;position:relative}
  2. .votetitle{width:100%;height:62px; background:url(icon.png) no-repeat 0 30px; font-size:15px}
  3. .red{position:absolute; left:0; top:90px; height:80px;}
  4. .blue{position:absolute; right:0; top:90px; height:80px;}
  5. .votetxt{line-height:24px}
  6. .votetxt span{float:right}
  7. .redhand{position:absolute; left:0;width:36px; height:36px; background:url(icon.png) no-repeat -1px -38px;cursor:pointer}
  8. .bluehand{position:absolute; right:0;width:36px; height:36px; background:url(icon.png) no-repeat -41px -38px;cursor:pointer}
  9. .grayhand{width:34px; height:34px; background:url(icon.png) no-repeat -83px -38px;cursor:pointer}
  10. .redbar{position:absolute; left:42px; margin-top:8px;}
  11. .bluebar{position:absolute; right:42px; margin-top:8px; }
  12. .redbar span{display:block; height:6px; background:red; width:100%;border-radius:4px;}
  13. .bluebar span{display:block; height:6px; background:#09f; width:100%;border-radius:4px; position:absolute; right:0}
  14. .redbar p{line-height:20px; color:red;}
  15. .bluebar p{line-height:20px; color:#09f; text-align:right; margin-top:6px}

jQuery

当点击手型按钮时,利用jQuery的$.getJSON()向后台php发送Ajax请求,如果请求成功,将会得到后台返回的json数据,jQuery再将json数据进行处理。以下函数:getdata(url,sid),传递了两个参数,url是请求的后台php地址,sid表示当前投票主题ID,我们在该函数中,返回的json数据有红蓝双方的投票数,以及双方比例,根据比例计算比例条的宽度,异步交互展示投票效果。

  1. function getdata(url,sid){
  2.     $.getJSON(url,{id:sid},function(data){
  3.         if(data.success==1){
  4.             var w = 208; //定义比例条的总宽度
  5.             //红方投票数
  6.             $("#red_num").html(data.red);
  7.             $("#red").css("width",data.red_percent*100+"%");
  8.             var red_bar_w = w*data.red_percent-10;
  9.             //红方比例条宽度
  10.             $("#red_bar").css("width",red_bar_w);
  11.             //蓝方投票数
  12.             $("#blue_num").html(data.blue);
  13.             $("#blue").css("width",data.blue_percent*100+"%");
  14.             var blue_bar_w = w*data.blue_percent;
  15.             //蓝方比例条宽度
  16.             $("#blue_bar").css("width",blue_bar_w);
  17.         }else{
  18.             alert(data.msg);
  19.         }
  20.     });
  21. }

当页面初次加载时,即调用getdata(),然后点击给红方投票或给蓝方投票同样调用getdata(),只是传递的参数不一样。注意本例中的参数sid我们设置为1,是根据数据表中的id设定的,开发者可以根据实际项目读取准确的id。

  1. $(function(){
  2.     //获取初始数据
  3.     getdata("vote.php",1);
  4.     //红方投票
  5.     $(".redhand").click(function(){
  6.         getdata("vote.php?action=red",1);
  7.     });
  8.     //蓝方投票
  9.     $(".bluehand").click(function(){
  10.         getdata("vote.php?action=blue",1);
  11.     });
  12. });

PHP

前端请求了后台的vote.php,vote.php将根据接收的参数,连接数据库,调用相关函数。

  1. include_once("connect.php");
  2. $action = $_GET['action'];
  3. $id = intval($_GET['id']);
  4. $ip = get_client_ip();//获取ip
  5. if($action=='red'){//红方投票
  6.     vote(1,$id,$ip);
  7. }elseif($action=='blue'){//蓝方投票
  8.     vote(0,$id,$ip);
  9. }else{//默认返回初始数据
  10.     echo jsons($id);
  11. }

函数vote($type,$id,$ip)用来做出投票动作,$type表示投票方,$id表示投票主题的id,$ip表示用户当前ip。首先根据用户当前IP,查询投票记录表votes_ip中是否已经存在当前ip记录,如果存在,则说明用户已投票,否则更新红方或蓝方的投票数,并将当前用户投票记录写入到votes_ip表中以防重复投票。

  1. function vote($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.         
  12.         $sql_in = "insert into votes_ip (vid,ip) values ('$id','$ip')";
  13.         mysql_query($sql_in);
  14.         if(mysql_insert_id()>0){
  15.             echo jsons($id);
  16.         }else{
  17.             $arr['success'] = 0;
  18.             $arr['msg'] = '操作失败,请重试';
  19.             echo json_encode($arr);
  20.         }
  21.     }else{
  22.         $arr['success'] = 0;
  23.         $arr['msg'] = '已经投票过了';
  24.         echo json_encode($arr);
  25.     }
  26. }

函数jsons($id)通过查询当前id的投票数,计算比例并返回json数据格式供前端调用。

  1. function jsons($id){
  2.     $query = mysql_query("select * from votes where id=".$id);
  3.     $row = mysql_fetch_array($query);
  4.     $red = $row['likes'];
  5.     $blue = $row['unlikes'];
  6.     $arr['success']=1;
  7.     $arr['red'] = $red;
  8.     $arr['blue'] = $blue;
  9.     $red_percent = round($red/($red+$blue),3);
  10.     $arr['red_percent'] = $red_percent;
  11.     $arr['blue_percent'] = 1-$red_percent;
  12.     
  13.     return json_encode($arr);
  14. }

文中还涉及到获取用户真实IP的函数,这个大家自行百度一下。

MySQL

最后,贴上Mysql数据表,votes表用来记录红蓝双方的投票总数,votes_ip表则用来存放用户的投票IP记录。

  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.  
  8.  
  9. INSERT INTO `votes` (`id`, `likes`, `unlikes`) VALUES 
  10. (1, 30, 10); 
  11.  
  12. CREATE TABLE IF NOT EXISTS `votes_ip` ( 
  13.   `id` int(10) NOT NULL, 
  14.   `vid` int(10) NOT NULL, 
  15.   `ip` varchar(40) NOT NULL 
  16. ) ENGINE=MyISAM DEFAULT CHARSET=utf8;

再次提醒,下载的demo如果运行不了,请先检查数据库连接配置是否正确。

好了,少说两句,具体大家操作一下。



本文网址:https://www.zztuku.com/detail-9159.html
站长图库 - PHP+jQuery+MySql实现红蓝投票功能
申明:如有侵犯,请 联系我们 删除。

评论(0)条

您还没有登录,请 登录 后发表评论!

提示:请勿发布广告垃圾评论,否则封号处理!!

    编辑推荐