资讯

展开

使用PHP+Mysql实现顶和踩投票功能

作者:快盘下载 人气:

PHP+mysql实现网站顶和踩投票功能实例,通过记录用户IP,判断用户的投票行为是否有效,该实例也可以扩展到投票系统中。

使用PHP+Mysql实现顶和踩投票功能

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

123          
4很好,很强大!5678          
9太差劲了!1011121314
15 $(function(){
16     //当鼠标悬浮和离开两个按钮时,切换按钮背景样式
17     $("#dig_up").hover(function(){
18         $(this).addClass("digup_on");
19     },function(){
20         $(this).removeClass("digup_on");
21     });
22     $("#dig_down").hover(function(){
23         $(this).addClass("digdown_on");
24     },function(){
25         $(this).removeClass("digdown_on");
26     });
27      
28     //初始化数据
29     getdata("ajax.php",1);
30      
31     //单击“顶”时
32     $("#dig_up").click(function(){
33         getdata("ajax.php?action=like",1);
34     });
35     //单击“踩”时
36     $("#dig_down").click(function(){
37         getdata("ajax.php?action=unlike",1);
38     });
39 });


函数getdata()

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{//投票失败
12             $("#msg").html(data.msg).show().css({'opacity':1,'top':'40px'})
13             .animate({top:'-50px',opacity:0}, "slow");
14         }
15     });
16 }


ajax.php

1 $action = $_GET['action'];
2 $id = 1;
3 $ip = get_client_ip();//获取当前ip
4  
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 }


投票的表结构

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(11) NOT NULL AUTO_INCREMENT,
14   `vid` int(11) NOT NULL,
15   `ip` varchar(20) NOT NULL,
16   PRIMARY KEY (`id`)
17 ) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=3 ;


本文转自:https://www.sucaihuo.com/php/105.html 转载请注明出处!

加载全部内容

相关教程
猜你喜欢
用户评论
快盘暂不提供评论功能!