结合pdo 和 redis 做一个简单的点赞页面

1:前台展示
<?php
header('content-type:text/html;charset=utf-8');
$pdo=new PDO('mysql:host=localhost;dbname=yii9_1','root','root');
$pdo->exec("set names utf8");
$data=$pdo->query("select * from goods")->fetchall(PDO::FETCH_ASSOC);
//print_r($data);
?>
<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>商品点赞页面</title>
    <script src="../jquery.js"></script>
</head>
<body>

<table>
    <tr>
        <td>id</td>
        <td>商品名称</td>
        <td>点赞</td>
    </tr>
    <?php foreach($data as $k=>$v):?>
    <tr>
        <td><?=$v['gid']?></td>
        <td><?=$v['gname']?></td>
        <td>
            <img src="./dianzan.png" class="dianzan" gid=<?=$v['gid']?>  >
        <span></span>
        </td>
    </tr>
    <?php endforeach; ?>
</table>
<h2><a href="#"  id="paihang">查看点赞排行榜</a></h2>
<div id="paihang2">

</div>
</body>
<script>
    $(function(){
        $(document).on('click','.dianzan',function(){
            var gid=$(this).attr('gid');
            console.log(gid);
            var _this=$(this);
            $.ajax({
                type: 'POST',
                url: './paihangbang.php',
                data:{gid:gid},
                success:function(msg){
                    _this.next().html(msg);
                    //alert(msg);
                }
            })
        });
        $(document).on('click','#paihang',function(){
            $.ajax({
                type:"POST",
                data:{type:'chakan'},
                url:"paihangbang.php",
                success:function(msg){
                    $('#paihang2').html(msg);
                }
            })
        })
    })
</script>
</html>

2:后台处理
<?php
header('content-type:text/html;charset=utf-8');
//redis-server.exe  redis.windows.conf
@$gid=$_POST['gid']?$_POST['gid']:0;

//$num=$redis->get($gid);
if($gid!=0){
    $redis=new Redis();
    $redis->connect("127.0.0.1","6379");
    //第一个参数为key值  为第三个参数 添加一个 设定的自增项 为1
    $res=$redis->zIncrBy('dianzanshu',1,$gid);
    if($res){
        //zscore 为取值
        $num=$redis->zScore('dianzanshu',$gid);
        echo $num;
        //echo "66";
    }else{
        echo  "存值错误";
    }
    exit;
}
@$type=$_POST['type']?$_POST['type']:0;
if($type=="chakan"){
    $redis=new Redis();
    $redis->connect("127.0.0.1","6379");
    $pdo=new PDO("mysql:host=localhost;dbname=yii9_1",'root','root');
    $pdo->exec('set names utf8');
    $res=$redis->zRevRange("dianzanshu",0,4,true);
    $result=array();
    foreach($res as $id=>$num){
       $sql="select * from goods where gid=$id";
        $goods=$pdo->query($sql)->fetch(PDO::FETCH_ASSOC);
        $result[]=array('gid'=>$id,'gname'=>$goods['gname'],'num'=>$num);
    }
    echo "<table>";
        foreach($result as $re) {
            echo "<tr><td>id" . $re['gid'] . "</td>";
            echo "<td>商品名称" . $re['gname'] . "</td>";
            echo "<td>点赞数量" . $re['num'] . "</td></tr>";
        }
     echo "</table>";
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值