1:前台展示
2:后台处理
<?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>";
}