一.初始化redis对象
//连接本地的 Redis 服务
$redis = new Redis();
$link = $redis->connect('127.0.0.1', 6379);
if($link){
echo "Connection to server sucessfully <br/>";
}else{
echo "Connection to server unsucessfully";
exit();
}
redis.php
<?php
?>
redis.php
<?php
$redis = new Redis();
$redis->connect('127.0.0.1', 6379);
?>
二.增加的模板
<form action="reg.php" method="post">
用户名: <input type="text" name="username" /><br/>
密码: <input type="password" name="password" /><br/>
年龄: <input type="text" name="age" /><br/>
<input type="submit" value="注册" />
<input type="reset" value="重新填写" />
</form>
三.执行添加的php文件
require("redis.php");
$username = $_POST['username'];
$password = MD5($_POST['password']);
$age = $_POST['age'];
//自动增加函数incr()
$uid = $redis->incr("userid");
//添加用户
$res = $redis->hmset("user:".$uid,array("uid"=>$uid,"username"=>$username,"password"=>$password,"age"=>$age));
$redis->rpush("uid",$uid);
if($res){
header("location:list.php");
}
四.列表展示--删除
<?php
require("redis.php");
//获取分页--首先知道总数,每页条数,当前页数,页总数
//用户总数
$count = $redis->lsize("uid");
//每页条数
$page_size = 3;
//当前页数
$page_num = (!empty($_GET['page']))?$_GET['page']:1;
//页总数
$page_count = ceil($count/$page_size);
$ids = $redis->lrange("uid",($page_num-1)*$page_size,(($page_num-1)*$page_size+$page_size-1));
//取出当前的所有用户
/*for($i=1;$i<=($redis->get("userid"));$i++){
$data[] = $redis->hgetall("user:".$i);
}*/
foreach($ids as $val){
$data[] = $redis->hgetall("user:".$val);
}
$data = array_filter($data);//过滤数组中的空元素
?>
<table border=1>
<tr>
<th>uid</th>
<th>username</th>
<th>age</th>
<th>操作</th>
</tr>
<?php foreach($data as $val){ ?>
<tr>
<td><?php echo $val['uid']; ?></td>
<td><?php echo $val['username']; ?></td>
<td><?php echo $val['age']; ?></td>
<td><a href="del.php?id=<?php echo $val['uid']?>">删除</a>
<a href="edit.php?id=<?php echo $val['uid']?>">编辑</a></td>
</tr>
<?php } ?>
<tr>
<td colspan="4">
<a href="?page=<?php echo (($page_num-1)<=1)?1:($page_num-1); ?>">上一页</a>
<a href="?page=<?php echo (($page_num+1)>=$page_count)?$page_count:($page_num+1); ?>">下一页</a>
<a href="?page=1">首页</a>
<a href="?page=<?php echo $page_count; ?>">尾页</a>
当前<?php echo $page_num; ?>页
总共<?php echo $page_count; ?>页
总共<?php echo $count; ?>用户
</td>
</tr>
</table>
五.删除文件
require("redis.php");
$uid = $_GET['id'];
$res = $redis->del("user:".$uid);
$redis->lrem("uid",$uid);
if($res){
header("location:list.php");
}
六.修改文件
<?php
require("redis.php");
$uid = $_GET['id'];
$data = $redis->hgetall("user:".$uid);
?>
<form action="doedit.php" method="post">
<input type="hidden" value="<?php echo $data['uid']; ?>" name="uid" />
用户名: <input type="text" name="username" value="<?php echo $data['username']; ?>" /><br/>
年龄: <input type="text" name="age" value="<?php echo $data['age']; ?>" /><br/>
<input type="submit" value="修改" />
</form>
七.执行修改
require("redis.php");
$uid = $_POST['uid'];
$username = $_POST['username'];
$age = $_POST['age'];
$res = $redis->hmset("user:".$uid,array("username"=>$username,"age"=>$age));
if($res){
header("location:list.php");
}else{
header("location:edit.php");
}