发布网友 发布时间:2022-04-09 15:25
共3个回答
懂视网 时间:2022-04-09 19:47
index.html
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>演示:PHP+jQuery+MySql实现的“顶”和“踩”功能</title>
<meta name="keywords" content="PHP,mysql,jquery" />
<meta name="description" content="Helloweba演示平台,演示XHTML、CSS、jquery、PHP案例和示例" />
<link rel="stylesheet" type="text/css" href="main.css" />
<style type="text/css">
.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}
</style>
</head>
<body>
<div id="main">
<h2 class="top_title">PHP+jQuery+MySql实现的“顶”和“踩”功能</h2>
<div class="digg">
<div id="dig_up" class="digup">
<span id="num_up"></span>
<p>很好,很强大!</p>
<div id="bar_up" class="bar"><span style=""></span><i></i></div>
</div>
<div id="dig_down" class="digdown">
<span id="num_down"></span>
<p>太差劲了!</p>
<div id="bar_down" class="bar"><span style=""></span><i></i></div>
</div>
<div id="msg"></div>
</div>
<!--<div class="ad_76090"><script src="bd_76090.js" type="text/javascript"></script></div><br/>-->
</div>
</body>
<script type="text/javascript" src="jquery.min.js"></script>
<script type="text/javascript">
$(function(){
//初始化数据
getdata("do.php",1);
$.getJSON(‘do.php‘,function(data){
//显示数字和百分比进度条效果
$("#bar_up span").css(‘width‘,data.like_percent);
$("#bar_up i").html(data.like_percent);
$("#num_down").html(data.unlike);
$("#bar_down span").html(‘width‘,data.unlike_percent);
$("#bar_down i").html(data.unlike_percent);
});
$("#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");
});
$("#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");
}
});
}
</script>
</html>
do.php
<?php
include_once("connect.php");
$action = $_GET[‘action‘];
$id = 1;
$ip = get_client_ip();
if($action==‘like‘){
likes(1,$id,$ip);
}elseif($action==‘unlike‘){
likes(0,$id,$ip);
}else{
echo jsons($id);
}
function likes($type,$id,$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);
echo jsons($id);
}else{
$msg = $type==1?‘您已经顶过了‘:‘您已经踩过了‘;
$arr[‘success‘] = 0;
$arr[‘msg‘] = $msg;
echo json_encode($arr);
}
}
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);
}
//获取用户真实IP
function get_client_ip() {
if (getenv("HTTP_CLIENT_IP") && strcasecmp(getenv("HTTP_CLIENT_IP"), "unknown"))
$ip = getenv("HTTP_CLIENT_IP");
else
if (getenv("HTTP_X_FORWARDED_FOR") && strcasecmp(getenv("HTTP_X_FORWARDED_FOR"), "unknown"))
$ip = getenv("HTTP_X_FORWARDED_FOR");
else
if (getenv("REMOTE_ADDR") && strcasecmp(getenv("REMOTE_ADDR"), "unknown"))
$ip = getenv("REMOTE_ADDR");
else
if (isset ($_SERVER[‘REMOTE_ADDR‘]) && $_SERVER[‘REMOTE_ADDR‘] && strcasecmp($_SERVER[‘REMOTE_ADDR‘], "unknown"))
$ip = $_SERVER[‘REMOTE_ADDR‘];
else
$ip = "unknown";
return ($ip);
}
?>
connect.php
<?php
//数据库配置
$host="localhost";
$db_user="root";//用户名
$db_pass="1234567890";//密码
$db_name="vote";//数据库名
$timezone="Asia/Shanghai";
$link=mysql_connect($host,$db_user,$db_pass);
mysql_select_db($db_name,$link);
mysql_query("SET names UTF8");
header("Content-Type: text/html; charset=utf-8");
?>
main.css,images/ 略
详见 http://www.helloweba.com/view-blog-237.html
PHP+MySql+jQuery实现的“顶”和“踩”投票功能
标签:数字 char bar select ret like case 配置 roo
热心网友 时间:2022-04-09 16:55
实现字段3个,次数、日期、ip地址热心网友 时间:2022-04-09 18:13
用过dede么? 我本来是想用iframe做的,发现dede的顶一下,踩一下非常方便。 我直接偷了dede的js代码。 他直接用的cookie记录,防止重复投票。省的建表的麻烦。 如果你要求精确,当然可以记录IP时间。