学习笔记2(投票)

ajax.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);
}

?>

配置文件

<?php
//数据库配置
$host="localhost";
$db_user="root";//用户名
$db_pass="123456";//密码
$db_name="xjy";//数据库名
$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");
?>

index.php

<?php
//bao han jiao ben wen jian
include_once("connect.php");
//cong mysql qu data bing qie chu li
$query=mysql_query("select likes,unlikes from votes where id = 1");
$row = mysql_fetch_array($query);

$like = $row['likes'];
$unlike = $row['unlikes'];

$percent_like = round($like / ($like + $unlike), 3) * 100;
$percent_unlike = 100 - $percent_like;

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head> 
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
        <title>演示:PHP+MySql+jQuery实现顶和踩投票功能</title>
        <meta name="keywords" content="PHP投票,jquery投票" />
        <meta name="description" content="本文结合实例,讲解使用PHP+MySql+jQuery实现的“顶”和“踩”投票功能,通过记录用户IP,判断用户的投票行为是否有效,该实例也可以扩展到投票系统中。" />
        <link rel="stylesheet" type="text/css" href="http://www.sucaihuo.com/jquery/css/common.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(images/diggs.png) no-repeat 4px 2px;}
            .digup_on{background:url(images/diggs.png) no-repeat 4px -49px;}
            .digdown{background:url(images/diggs.png) no-repeat 4px -102px;}
            .digdown_on{background:url(images/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 class="head">
            <div class="head_inner clearfix">
                <ul id="nav">
                    <li><a href="http://www.sucaihuo.com">首 页</a></li>
                    <li><a href="http://www.sucaihuo.com/templates">网站模板</a></li>
                    <li><a href="http://www.sucaihuo.com/js">网页特效</a></li>
                    <li><a href="http://www.sucaihuo.com/php">PHP</a></li>
                    <li><a href="http://www.sucaihuo.com/site">精选网址</a></li>
                </ul>
                <a class="logo" href="http://www.sucaihuo.com"><img src="http://www.sucaihuo.com/Public/images/logo.jpg" alt="素材火logo" /></a>
            </div>
        </div>
        <div class="container">
            <div class="demo">
                <h2 class="title"><a href="http://www.sucaihuo.com/js/105.html">教程:PHP+MySql+jQuery实现顶和踩投票功能</a></h2>
                <div class="digg"> 
                    <div id="dig_up" class="digup">
                        <span id="num_up"><?php echo $like;?></span>
                        <p>很好,很强大!</p>
                        <div id="bar_up" class="bar"><span style="width:<?php echo $percent_like;?>%"></span><i><?php echo $percent_like;?>%</i></div>
                    </div>
                    <div id="dig_down" class="digdown">
                        <span id="num_down"><?php echo $unlike;?></span>
                        <p>太差劲了!</p>
                        <div id="bar_down" class="bar"><span style="width:<?php echo $percent_unlike;?>%"></span><i><?php echo $percent_unlike;?>%</i></div>
                    </div>
                    <div id="msg"></div>
                </div>
            </div>
        </div>
        <div class="foot">
            Powered by sucaihuo.com  本站皆为作者原创,转载请注明原文链接:<a href="http://www.sucaihuo.com" target="_blank">www.sucaihuo.com</a>
        </div>
        <script type="text/javascript" src="http://www.sucaihuo.com/Public/js/other/jquery.js"></script> 
        <script type="text/javascript">
            $(function() {//??i cant understand
                $("#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);
                });
            });

            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>
    </body>
</html>

图片:未上传
1需要自己建表否则功能不可用
2link链接一个外部CSS文件

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值