本文实例为大家分享了基于PHP+jQuery+MysqL实现红蓝(顶踩)投票代码,供大家参考,具体内容如下
数据库操作:
INSERT INTO <a href="https://www.jb51.cc/tag/Vote/" target="_blank" class="keywords">Vote</a>s
(id
,likes
,unlikes
) VALUES
(1,30,10);
CREATE TABLE IF NOT EXISTS <a href="https://www.jb51.cc/tag/Vote/" target="_blank" class="keywords">Vote</a>s_ip
(
id
int(10) NOT NULL,vid
int(10) NOT NULL,ip
varchar(40) NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
div:
很好,很强大!
太差劲了!
css:
jquery:
//初始化数据
getdata("do.PHP",1);
//单击“顶”时
$("#dig_up").click(function(){
getdata("do.PHP?action=like",1);
});
//单击“踩”时
$("#dig_down").click(function(){
getdata("do.PHP?action=unlike",1);
});
});
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{//投票失败
$("#msg").html(data.msg).show().css({'opacity':1,'top':'40px'})
.animate({top:'-50px',opacity:0},"slow");
}
});
}
PHP:
$id = 1;
$ip = get_client_ip();//获取ip
if($action=='like'){//顶
likes(1,$id,$ip);
}elseif($action=='unlike'){//踩
likes(0,$ip);
}else{
echo jsons($id);
}
function likes($type,$ip){
$ip_sql=mysql_query("select ip from votes_ip where vid='$id' and ip='$ip'");
$count=mysql_num_rows($ip_sql);
if($count==0){//还没有顶过
if($type==1){//顶
$sql = "update votes set likes=likes+1 where id=".$id;
}else{//踩
$sql = "update votes set unlikes=unlikes+1 where id=".$id;
}
mysql_query($sql);
$sql_in = "insert into votes_ip (vid,ip) values ('$id','$ip')";
mysql_query($sql_in);
if(mysql_insert_id()>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);
}
}
-----------PHP-------------------------
function jsons($id){
$query = MysqL_query("select from Votes where id=".$id);
$row = MysqL_fetch_array($query);
$like = $row['likes'];
$unlike = $row['unlikes'];
$arr['success']=1;
$arr['like'] = $like;
$arr['unlike'] = $unlike;
$like_percent = round($like/($like+$unlike),3)100;
$arr['like_percent'] = $like_percent.'%';
$arr['unlike_percent'] = (100-$like_percent).'%';
return json_encode($arr);
}
以上就是本文的全部内容,希望对大家学习PHP程序设计有所帮助。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。