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;
复制代码
2,html页面部分在页面中,有两个分别表示“顶”和“踩”的按钮,即#dig_up和#dig_down,按钮上记录了投票的票数以及分别所占的百分比,非常直观的对比投票结果。
很好,很强大!
太差劲了!
复制代码
3,css代码部分必须使用css来美化页面,使用一张图diggs.png来定位不同的按钮背景,通过设置position来定位各元素之间的位置关系。
.digg{width:420px; height:120px; margin:80px auto 20px auto; position:relative} #dig_up,#dig_down{width:200px; height:48px; margin:10px; position:relative; border:1px solid #d3d3d3; padding-left:42px; cursor:pointer} .digup{background:url(diggs.png) no-repeat 4px 2px;} .digup_on{background:url(diggs.png) no-repeat 4px -49px;} .digdown{background:url(diggs.png) no-repeat 4px -102px;} .digdown_on{background:url(diggs.png) no-repeat 4px -154px;} #num_up,#num_down{position:absolute; right:6px; top:18px; font-size:20px;} #dig_up p{height:24px; line-height:24px; color:#360} #dig_down p{height:24px; line-height:24px; color:#f30} .bar{width:100px; height:12px; line-height:12px; border:1px solid #f0f0f0; position:relative; text-align:center} .bar span{display:block; height:12px; } .bar i{position:absolute; top:0; left:104px;} #bar_up span{background:#360} #bar_down span{background:#f60} #msg{position:absolute; right:20px; top:40px; font-size:18px; color:#f00}
复制代码
2,jquery部分本示例还依赖于jquery,因此一定不能忘了在页面中先载入jquery库文件。首先,jquery要处理的是鼠标分别滑向两个投票按钮时变换的背景图片,通过addclass()和removeclass()来实现。
$(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(do.php,1); //单击“顶”时 $(#dig_up).click(function(){ getdata(do.php?action=like,1); }); //单击“踩”时 $(#dig_down).click(function(){ getdata(do.php?action=unlike,1); }); });
复制代码
然后,初始化数据,就是页面加载后,要显示初始的已经投票的结果,包括各投票数和所占百分比。将获取数据的操作写在一个自定义函数getdata()中,通过传递不同的请求地址和id参数来完成数据的载入。函数getdata()中,向url发送一个ajax请求,根据后台处理的返回结果,如果投票成功则更改页面中相应的元素内容,包括投票数和所占百分比。
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{//投票失败 bbs.it-home.org $(#msg).html(data.msg).show().css({'opacity':1,'top':'40px'}) .animate({top:'-50px',opacity:0}, slow); } }); }
复制代码
4,php代码部分数据的获取都是通过do.php来完成,do.php根据前端页面传递的参数,连接数据库,根据条件判断分别进入“顶”、“踩”和初始数据处理模块,以下是do.php模块代码结构:
复制代码
函数likes()用来处理“顶”和“踩”投票模块,首先是判断用户ip是否已经投票过了,如果已经投票则直接返回相应提示,如果用户ip没有投票记录则更新votes表,将对应的投票数加1,然后向votes_ip表中插入该用户的ip记录,如果操作成功,则调用jsons()输出投票后的投票数和所占百分比等数据,否则输入操作失败的提示信息。
0){ echo jsons($id); }else{ $arr['success'] = 0; $arr['msg'] = '操作失败,请重试'; echo json_encode($arr); } }else{ $msg = $type==1?'您已经顶过了':'您已经踩过了'; $arr['success'] = 0; $arr['msg'] = $msg; echo json_encode($arr); } }
复制代码
函数jsons()用来读取votes表中相应id的投票数,并计算百分比,最后将这些信息以json格式输出,供前端页面使用。
复制代码
该实例可以应用到常见的赞、同意和反对评论、投票系统等场景中,运用了ajax原理实现的前后端交互功能。do.php中还有个函数get_client_ip()用来获取用户真实ip,在本文提供的源码包中,大家可以下载研究下。