分页
要对列表页进行分页,需要知道:
①用户总数 $count
② 页大小 $pageSize:用户自定义
③ 当前页:$page:GET 方式获取
④ 总页数:$pageCount = ceil($count / $pageSize)
关键是用户总数 $count 的获取:
可以采取的方案是,在用户注册时,把 uid 存入链表,统计链表中用户 uid 的个数,即为用户总数 $count。
【操作】
reg.php:(line 14)
<?phprequire 'redis.php';$username = $_POST['username'];
$password = md5($_POST['password']);
$age =$_POST['age'];
//uid 自加
//当有 userid 这个键时,执行 incr时该键的值加1;不存在该键时,创建一个 userid,值为0,执行incr,值为1$uid = $redis->incr('userid');//向 hash 表中批量添加数据:hMset $redis->hMset('user:'.$uid, array('uid'=>$uid, 'name'=>$username, 'password'=>$password, 'age'=>$age));//把用户 id 存入链表$redis->rpush('uid', $uid);//跳转header('location:list.php');
在 redis 客户端 flushdb 清除一下数据库,重新注册用户;
注册了 9 个用户:
127.0.0.1:6379> keys *
1) "user:1"
2) "user:4"
3) "user:7"
4) "user:3"
5) "user:6"
6) "uid"
7) "user:8"
8) "user:5"
9) "user:9"
10) "user:2"
11) "userid"
key 为 uid 的键即是保存用户 uid 的链表:
127.0.0.1:6379> type uid
list
127.0.0.1:6379> lrange uid 0 -1
1) "1"
2) "2"
3) "3"
4) "4"
5) "5"
6) "6"
7) "7"
8) "8"
9) "9"
前台:
删除用户时,同时删除链表结构中的对应的用户的 uid :
del.php(line:7):
del('user:'.$uid);
//删除链表中的用户 uid
$redis->lrem('uid', $uid);
header('location:list.php');
删除其中一个用户(例如秦明),在 redis 客户端获取 list uid:
127.0.0.1:6379> lrange uid 0 -1
1) "1"
2) "2"
3) "3"
4) "4"
5) "5"
6) "6"
7) "7"
8) "9"
uid 为 8 的值已经被删掉了。
在 list.php 中可以通过 $resid->lsize('uid'); 来获取 key 为 uid 的链表元素的个数(line:15),同时修改 for 循环为 foreach 循环(line:36),分页(line:13):
用户信息列表返回注册页面
登录<?phprequire 'redis.php';/*分页*///用户总数$count = $redis->lsize('uid');//页大小 $pageSize = 5;//当前页码$page = !empty($_GET['page'])?$_GET['page']:1;//页总数$pageCount = ceil($count / $pageSize);/*第 1 页:lrange 0 4
第 2 页:lrange 5 9
第 3 页:lrange 10 14
第 n 页:lrange ($page - 1) * $pageSize ($page - 1) * $pageSize + ($pageSize - 1)*/$ids = $redis->lrange('uid', (($page - 1) * $pageSize), (($page - 1) * $pageSize + ($pageSize - 1)));/*var_dump($ids);
$page = 1
array(5) { [0]=> string(1) "1" [1]=> string(1) "2" [2]=> string(1) "3" [3]=> string(1) "4" [4]=> string(1) "5" }*/foreach($ids as $v){ $data[] = $redis->hgetall('user:'.$v);
}?>uidnameage操作<?php foreach($data as $v){ ?><?php echo $v['uid']?><?php echo $v['name']?><?php echo $v['age']?>删除 | 编辑上一页
下一页
首页
尾页
当前<?php echo $page;?>页
总共<?php echo $pageCount;?>页
共<?php echo $count;?>用户
完成
登录
需要判断用户输入的用户名是否已经注册,实现的方法是:在用户注册的时候把用户名存入字符串,修改 reg.php(注册):
reg.php(line:15):
<?phprequire 'redis.php';$username = $_POST['username'];
$password = md5($_POST['password']);
$age =$_POST['age'];
//uid 自加
//当有 userid 这个键时,执行 incr时该键的值加1;不存在该键时,创建一个 userid,值为0,执行incr,值为1$uid = $redis->incr('userid');//向 hash 表中批量添加数据:hMset $redis->hMset('user:'.$uid, array('uid'=>$uid, 'name'=>$username, 'password'=>$password, 'age'=>$age));//把用户 id 存入链表$redis->rpush('uid', $uid);//把用户名 name 存入字符串$redis->set('username:'.$username, $uid);//跳转header('location:list.php');
在 redis 客户端 flushdb 清空数据库,重新注册;
增加 login.php:
<?php
header("content-type:text/html;charset=utf-8"); require 'redis.php'; if(isset($_POST) && !empty($_POST)){ $username = $_POST['username']; $pass = $_POST['password']; //判断用户民是否存在
$id = $redis->get('username:'.$username); if(!empty($id)){ //用户存在,对比密码
$password = $redis->hget('user:'.$id, 'password'); if(md5($pass) == $password){ $auth = md5(time().$username.rand()); $redis->set('auth:'.$auth, $id); setcookie('auth', $auth, time()+86400); header('location:list.php');
}
}
}else{?>
Document用户名:密码:
修改 list.php,增加 “退出”,“欢迎+用户名”:
用户信息列表返回注册页面
get('auth:'.$_COOKIE['auth']);
//获取用户名
$name = $redis->hget('user:'.$id, 'name');
echo ' 欢迎你, ',$name;
echo ' 退出';
}else{
?> 登录
lsize('uid');
//页大小
$pageSize = 5;
//当前页码
$page = !empty($_GET['page'])?$_GET['page']:1;
//页总数
$pageCount = ceil($count / $pageSize);
/*
第 1 页:lrange 0 4
第 2 页:lrange 5 9
第 3 页:lrange 10 14
第 n 页:lrange ($page - 1) * $pageSize ($page - 1) * $pageSize + ($pageSize - 1)
*/
$ids = $redis->lrange('uid', (($page - 1) * $pageSize), (($page - 1) * $pageSize + ($pageSize - 1)));
/*
var_dump($ids);
$page = 1
array(5) { [0]=> string(1) "1" [1]=> string(1) "2" [2]=> string(1) "3" [3]=> string(1) "4" [4]=> string(1) "5" }
*/
foreach($ids as $v){
$data[] = $redis->hgetall('user:'.$v);
}
?>uidnameage操作" _href="del.php?uid=" style="color:#3194d0;">删除 | " _href="mod.php?uid=" style="color:#3194d0;">编辑" _href="?page=" style="color:#3194d0;">上一页
$pageCount?$pageCount:($page+1);?>" _href="?page=$pageCount?$pageCount:($page+1);?>" style="color:#3194d0;">下一页
首页
" _href="?page=" style="color:#3194d0;">尾页
当前页
总共页
共用户
此时在 redis 客户端产看所有的 key:
127.0.0.1:6379> keys *
1) "user:4"
2) "user:1"
3) "user:6"
4) "user:3"
5) "uid"
6) "username:\xe5\x90\x89\xe5\xb8\x83\xe6\x96\xaf"
7) "username:\xe5\x88\x87\xe8\xb5\xab"
8) "auth:d3716d28fd2491c1983fc6bb4087c05b"
9) "user:5"
10) "username:\xe9\xbb\x98\xe5\xbe\xb7\xe8\x90\xa8\xe5\x85\x8b"
11) "userid"
12) "user:2"
13) "username:\xe5\xbe\xb7\xe5\xb8\x83\xe8\xa5\xbf"
14) "username:\xe5\x85\x8b\xe6\x96\xaf\xe5\x88\x87\xe5\xb0\x94\xe5\xb0\xbc"
15) "username:\xe5\x8a\xa0\xe5\xb8\x83\xe9\x87\x8c\xe5\x9f\x83\xe5\xb0\x94"
用户列表前台:
退出
logout.php:
1 <?php
2 setcookie('auth', '', time()-1);
3 header('location:list.php');
加关注
修改 list.php(列表界面 line:64)
1
2345用户信息列表678 返回注册页面
9 get('auth:'.$_COOKIE['auth']);
14 //获取用户名
15 $name = $redis->hget('user:'.$id, 'name');
16 echo ' 欢迎你, ',$name;
17 echo ' 退出';
18 }else{
19 ?>
20 登录21
22 lsize('uid');
26 //页大小
27 $pageSize = 5;
28 //当前页码
29 $page = !empty($_GET['page'])?$_GET['page']:1;
30 //页总数
31 $pageCount = ceil($count / $pageSize);
32
33 /*
34 第 1 页:lrange 0 4
35 第 2 页:lrange 5 9
36 第 3 页:lrange 10 14
37 第 n 页:lrange ($page - 1) * $pageSize ($page - 1) * $pageSize + ($pageSize - 1)
38 */
39 $ids = $redis->lrange('uid', (($page - 1) * $pageSize), (($page - 1) * $pageSize + ($pageSize - 1)));
40 /*
41 var_dump($ids);
42 $page = 1
43 array(5) { [0]=> string(1) "1" [1]=> string(1) "2" [2]=> string(1) "3" [3]=> string(1) "4" [4]=> string(1) "5" }
44 */
45
46 foreach($ids as $v){
47 $data[] = $redis->hgetall('user:'.$v);
48 }
49 ?>
505152uid53name54age55操作5657
5859606162" _href="del.php?uid=" style="color:#3194d0;">删除 63 | " _href="mod.php?uid=" style="color:#3194d0;">编辑64
65 | &uid=" _href="addAttention.php?id=&uid=" style="color:#3194d0;">加关注6667
6869 " _href="?page=" style="color:#3194d0;">上一页70 $pageCount?$pageCount:($page+1);?>" _href="?page=$pageCount?$pageCount:($page+1);?>" style="color:#3194d0;">下一页 71 首页72 " _href="?page=" style="color:#3194d0;">尾页73 当前页
74 总共页
75 共用户
7677787980
加关注用集合(set,有并集、交集、差集——可以实现共同关注、好友推荐(a 关注了,但是 b 没有关注,a 给 b 的好友推荐))来存储
addAttenation.php:
1 <?php
2 require 'redis.php';
3
4 //待加关注的用户id
5 $id = $_GET['id'];
6 //用户id
7 $uid = $_GET['uid'];
8
9 //当前用户 uid 正在following(关注)id
10 $redis->sadd('user:'.$uid.':following', $id);11 //用户id的followers(粉丝):uid
12 $redis->sadd('user:'.$id.':followers', $uid);13
14 header('location:list.php');
添加完关注以后,在 redis 客户端:
127.0.0.1:6379> keys *
1) "user:4"
2) "user:1:followers"
3) "user:3"
4) "user:6"
5) "auth:a6ce443729f3d524f81c2bdcdba60278"
6) "uid"
7) "auth:556734b6f86b074b921349d616393c2e"
8) "username:\xe5\x88\x87\xe8\xb5\xab"
9) "user:2:followers"
10) "user:2"
11) "userid"
12) "user:1:following"
13) "auth:28e59ffccd991262bfb1b92c027a69ea"
14) "username:\xe5\xbe\xb7\xe5\xb8\x83\xe8\xa5\xbf"
15) "user:1"
16) "user:5:followers"
17) "username:\xe5\x90\x89\xe5\xb8\x83\xe6\x96\xaf"
18) "user:3:followers"
19) "user:5"
20) "auth:d3716d28fd2491c1983fc6bb4087c05b"
21) "username:\xe9\xbb\x98\xe5\xbe\xb7\xe8\x90\xa8\xe5\x85\x8b"
22) "auth:a2a03ba9c65d1716d7f19726372f83f7"
23) "user:2:following"
24) "user:4:followers"
25) "auth:8ae6684aecf41ee7259a288aaecca964"
26) "username:\xe5\x8a\xa0\xe5\xb8\x83\xe9\x87\x8c\xe5\x9f\x83\xe5\xb0\x94"
27) "username:\xe5\x85\x8b\xe6\x96\xaf\xe5\x88\x87\xe5\xb0\x94\xe5\xb0\xbc"
127.0.0.1:6379> smembers user:1:following
1) "2"
2) "4"
3) "5"
127.0.0.1:6379> smembers user:1:followers
1) "2"
127.0.0.1:6379> smembers user:2:following
1) "1"
2) "3"
3) "4"
127.0.0.1:6379> smembers user:2:followers
1) "1"
127.0.0.1:6379> #user:1 和 user:2 关注的差集,可以user:1 可以向 user:2 推荐好友
1) "2"
2) "5"
我的关注
修改 list.php(line:79):
1
2345用户信息列表678 返回注册页面
9 get('auth:'.$_COOKIE['auth']);
14 //获取用户名
15 $name = $redis->hget('user:'.$id, 'name');
16 echo ' 欢迎你, ',$name;
17 echo ' 退出';
18 }else{
19 ?>
20 登录21
22 lsize('uid');
26 //页大小
27 $pageSize = 5;
28 //当前页码
29 $page = !empty($_GET['page'])?$_GET['page']:1;
30 //页总数
31 $pageCount = ceil($count / $pageSize);
32
33 /*
34 第 1 页:lrange 0 4
35 第 2 页:lrange 5 9
36 第 3 页:lrange 10 14
37 第 n 页:lrange ($page - 1) * $pageSize ($page - 1) * $pageSize + ($pageSize - 1)
38 */
39 $ids = $redis->lrange('uid', (($page - 1) * $pageSize), (($page - 1) * $pageSize + ($pageSize - 1)));
40 /*
41 var_dump($ids);
42 $page = 1
43 array(5) { [0]=> string(1) "1" [1]=> string(1) "2" [2]=> string(1) "3" [3]=> string(1) "4" [4]=> string(1) "5" }
44 */
45
46 foreach($ids as $v){
47 $data[] = $redis->hgetall('user:'.$v);
48 }
49 ?>
505152uid53name54age55操作5657
5859606162" _href="del.php?uid=" style="color:#3194d0;">删除 63 | " _href="mod.php?uid=" style="color:#3194d0;">编辑64
65 | &uid=" _href="addAttention.php?id=&uid=" style="color:#3194d0;">加关注6667
6869 " _href="?page=" style="color:#3194d0;">上一页70 $pageCount?$pageCount:($page+1);?>" _href="?page=$pageCount?$pageCount:($page+1);?>" style="color:#3194d0;">下一页 71 首页72 " _href="?page=" style="color:#3194d0;">尾页73 当前页
74 总共页
75 共用户
7677787980我的关注81 <?php
82 $data = $redis->smembers('user:'.$id.':following');83 foreach ($data as $value) {
84 $row = $redis->hgetall('user:'.$value);85 ?>
868788899091
949596
前台显示:
粉丝同理。
总结:
统计用户总数可以把用户 uid 以链表(list)结构存储;判断用户是否已经注册可以把用户名以字符串(string)类型存储;统计用户的关注、粉丝以及推荐好友可以使用集(set)数据类型。