先给大家展示效果图:
这是一个非常实用的投票实例,应用在双方观点对抗投票场景。用户可以选择支持代表自己观点的一方进行投票,本文以红蓝双方投票为例,通过前后台交互,直观展示红蓝双方投票数和所占比例,应用非常广泛。
本文是一篇综合知识应用类文章,需要您具备PHP、jQuery、MysqL以及html和css方面的基本知识。
HTML
我们需要在页面中展示红蓝双方的观点,以及对应的投票数和比例,以及用于投票交互的手型图片,本例以#red和#blue分别表示红蓝双方。.
redhand和.bluehand
用来做手型投票按钮,.redbar和.bluebar展示红蓝双方比例调,#red_num和#blue_num展示双方投票数。
rush:xhtml;">
Vote">
Votetitle">您对Helloweba提供的
文章的看法?
Votetxt">非常实用完全看不懂
CSS
使用CSS将页面美化,加载背景图片,确定相对位置等等,你可以直接复制以下代码,在自己的项目中稍作修改即可。
rush:css;">
.
Vote{width:288px; height:220px; margin:60px auto 20px auto;position:relative}
.
Votetitle{width:100%;height:62px; background:url(icon.png) no-repeat 0 30px; font-size:15px}
.red{position:absolute; left:0; top:90px; height:80px;}
.blue{position:absolute; right:0; top:90px; height:80px;}
.
Votetxt{line-height:24px}
.
Votetxt span{float:right}
.redhand{position:absolute; left:0;width:36px; height:36px; background:url(icon.png) no-repeat -1px -38px;cursor:pointer}
.bluehand{position:absolute; right:0;width:36px; height:36px; background:url(icon.png) no-repeat -41px -38px;cursor:pointer}
.grayhand{width:34px; height:34px; background:url(icon.png) no-repeat -83px -38px;cursor:pointer}
.redbar{position:absolute; left:42px; margin-top:8px;}
.bluebar{position:absolute; right:42px; margin-top:8px; }
.redbar span{
display:block; height:6px; background:red; width:100%;border-radius:4px;}
.bluebar span{
display:block; height:6px; background:#09f; width:100%;border-radius:4px; position:absolute; right:0}
.redbar p{line-height:20px; color:red;}
.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数据有红蓝双方的投票数,以及双方比例,根据比例计算比例条的宽度,异步交互展示投票效果。
rush:js;">
function getdata(url,sid){
$.getJSON(url,{id:sid},function(data){
if(data.success==1){
var w = 208; //定义比例条的总宽度
//红方投票数
$("#red_num").html(data.red);
$("#red").css("width",data.red_percent*100+"%");
var red_bar_w = w*data.red_percent-10;
//红方比例条宽度
$("#red_bar").css("width",red_bar_w);
//蓝方投票数
$("#blue_num").html(data.blue);
$("#blue").css("width",data.blue_percent*100+"%");
var blue_bar_w = w*data.blue_percent;
//蓝方比例条宽度
$("#blue_bar").css("width",blue_bar_w);
}else{
alert(data.msg);
}
});
}
当页面初次加载时,即调用getdata(),然后点击给红方投票或给蓝方投票同样调用getdata(),只是传递的参数不一样。注意本例中的参数sid我们设置为1,是根据数据表中的id设定的,开发者可以根据实际项目读取准确的id。
rush:js;">
$(function(){
//
获取初始数据
getdata("
Vote.
PHP",1);
//红方投票
$(".redhand").click(function(){
getdata("
Vote.
PHP?action=red",1);
});
//蓝方投票
$(".bluehand").click(function(){
getdata("
Vote.
PHP?action=blue",1);
});
});
前端请求了后台的Vote.PHP,Vote.PHP将根据接收的参数,连接数据库,调用相关函数。
rush:
PHP;">
include_once("connect.
PHP");
$action = $_GET['action'];
$id = intval($_GET['id']);
$ip = get_client_ip();//获取ip
if($action=='red'){//红方投票
Vote(1,$id,$ip);
}elseif($action=='blue'){//蓝方投票
Vote(0,$ip);
}else{//默认返回初始数据
echo jsons($id);
}
函数Vote($type,$ip)用来做出投票动作,$type表示投票方,$id表示投票主题的id,$ip表示用户当前ip。首先根据用户当前IP,查询投票记录表Votes_ip中是否已经存在当前ip记录,如果存在,则说明用户已投票,否则更新红方或蓝方的投票数,并将当前用户投票记录写入到Votes_ip表中以防重复投票。
mysql_query($sql_in);
if(mysql_insert_id()>0){
echo jsons($id);
}else{
$arr['success'] = 0;
$arr['msg'] = '操作失败,请重试';
echo json_encode($arr);
}
}else{
$arr['success'] = 0;
$arr['msg'] = '已经投票过了';
echo json_encode($arr);
}
}
最后,贴上MysqL数据表,Votes表用来记录红蓝双方的投票总数,Votes_ip表则用来存放用户的投票IP记录。
rush:
sql;">
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(10) NOT NULL,`vid` int(10) NOT NULL,`ip` varchar(40) NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
PHP+MysqL+jQuery实现顶和踩投票功能
本文结合实例,讲解使用PHP+MysqL+jQuery实现的“顶”和“踩”投票功能,通过记录用户IP,判断用户的投票行为是否有效,该实例也可以扩展到投票系统中。
首先我们在页面上放置“顶”和“踩”的按钮,即#dig_up和#dig_down,按钮上分别记录了投票的票数以及所占的百分比。
rush:js;">
$(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);
});
});
以上内容实现了基于PHP+jQuery+MysqL实现红蓝(顶踩)投票代码,希望大家喜欢。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。