夺命雷公狗---Redis---7-案例操作2(用户列表页,实现分页显示已经关注功能)完

redis.php(公共文件)

<?php
    $redis = new Redis();
    $redis -> connect('localhost',6379);
    $redis -> auth('123456');//因为我修改了配置文件的密码是123456

 

login.php(登录页面)

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title></title>
    </head>
    <body>
        <form action="action.php?act=login" method="post">
            <table>
                <caption><h1>用户登录</h1></caption>
                <tr>
                    <th>name</th>
                    <td><input type="text" name="username"></td>
                </tr>
                <tr>
                    <th>pass</th>
                    <td><input type="password" name="pass"></td>
                </tr>
                <tr>
                    <td><input type="submit" value="提交"></td>
                    <td><input type="reset" name="重置"></td>
                </tr>
            </table>
        </form>
    </body>
</html>

 

index.php(首页)

<?php
    require "redis.php";
    //取出用户列表,实现分页显示
    //1.计算总的用户数,
    $count = $redis -> lsize('userid');
    //echo $count;die;
    //2.定义每页显示的记录数
    $perpage = 2;
    //3.计算总的页数
    $pagecount = ceil($count/$perpage);
    //4.定义当前页
    $page = isset($_GET['page'])?(max(1,min($pagecount,$_GET['page']))):1;
    //echo $page;
    //5.取出当前页的数据,思路,取出链表里面的id,根据id拼接哈希,取出哈希里面的内容。
    //$redis -> lrange('userid',0,1); 这样样是第一页的数据  0代表偏移量 算法 ($page-1) * $perpage =2
    //$redis -> lrange('userid',2,3); 这样样是第一页的数据 算法 ($page-1) * $perpage =2
    //$redis -> lrange('userid',4,5); 这样样是第一页的数据 算法 ($page-1) * $perpage =4
    //($page-1)*$perpage-1=1
    //($page-1)*$perpage-1=3
    //如果每页显示5个
    //$redis -> lrange('userid',0,4);    ($page -1) *$perpage = 0   5
    //$redis -> lrange('userid',5,9);    5+5-1=9
    //$redis -> lrange('usesrid',10,14); 10+5-1
    $offset = ($page-1)*$perpage;
    $N=($page-1)*$perpage+$perpage-1;
    $ids = $redis -> lrange('userid',$offset,$N);//返回的是当前显示数据的id的值,是一个一维数组
    //echo "<pre>";
    //print_r($ids);
    //取出当前页面的数据
    $data = array();

    foreach($ids as $v){
        $data[] = $redis -> hgetall('user:id:'.$v);//返回的是一行数据,也就是一个一维数组
    }
    //var_dump($ids);die;
    session_start();


    //关注程序的开始
    $mid = $_SESSION['user_id'];  //取出登录用户的id
    $ids = $redis ->smembers('myguan:'.$mid);  //返回的是一维数组
    $guan =array();
    foreach($ids as $v){
        $guan[] = $redis -> hgetall('user:id:'.$v);//返回是一行数据,也就是一个一维数组
    }
    //关注程序结束
?>
<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title></title>
    </head>
    <body>
        <a href="edit.php?id=<?php echo $_SESSION['user_id']; ?>">尊敬的:<?php echo $_SESSION['username']; ?>欢迎您的归来!</a>
        <a href="action.php?act=sunll">退出</a><br />
        <table width="500" border='1'>
                <caption><h1>用户列表</h1></caption>
                <tr>
                    <th>id</th>
                    <th>name</th>
                    <th>age</th>
                    <th>操作</th>
                </tr>
                <?php foreach($data as $v){ ?>
                <tr>
                    <td><?php echo $v['id']; ?></td>
                    <td><?php echo $v['username']; ?></td>
                    <td><?php echo $v['age']; ?></td>
                    <td align="center"><a href="edit.php?id=<?php echo $v['id']; ?>">编辑</a>/
                    <a href="action.php?act=del&id=<?php echo $v['id']; ?>">删除</a>/
                    <a href="action.php?act=guan&id=<?php echo $v['id']; ?>">添加关注</a>
                    </td>
                </tr>
                <?php } ?>
                <tr align="right">
                    <td colspan="4">
                        <a href="?page=<?php echo max(1,$page-1); ?>">上一页</a><a href="?page=<?php echo min($page+1,$pagecount); ?>">下一页</a>
                    </td>
                </tr>
            </table>
            <hr />

            <b>我关注的人</b>
            <?php foreach($guan as $v){
                 echo $v['username'].'-----还有-----';
            }?>
    </body>
</html>

 

reg.php(注册页面)

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8" />
        <title></title>
    </head>
    <body>
        <form action="action.php?act=reg" method="post">
            <table>
            <caption><h1>用户注册</h1></caption>
                <tr>
                    <th>name</th>
                    <td><input type="text" name="username"></td>
                </tr>
                <tr>
                    <th>pass</th>
                    <td><input type="password" name="pass"></td>
                </tr>
                <tr>
                    <th>age</th>
                    <td><input type="text" name="age"></td>
                </tr>
                <tr>
                    <td align="rigth"><input type="submit" value="注册" /></td>
                    <td align="center"><input type="reset" value="重置"></td>
                </tr>
            </table>
        </form>
    </body>
</html>

 

edit.php(编辑页面)

 

<?php
    require "redis.php";
    $ids = $_GET['id'];
            $data = array();        
            $data[] = $redis -> hgetall('user:id:'.$ids);

?>
<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title></title>
    </head>
    <body>
        <form action="action.php?act=edit" method="post">
            <table width="500" border='1' >
                <caption><h1>修改中心</h1></caption>
                <tr>
                    <th>name</th>
                    <th>age</th>
                    <th>pass</th>
                </tr>
                <?php foreach($data as $data ){ ?>
                <tr>
                    <td><input type="text" name="username" disabled value="<?php echo $data['username']; ?>"></td>
                    <td><input type="text" name="age" value="<?php echo $data['age']; ?>"></td>
                    <td><input type="password" name="pass" value="<?php echo $data['password']; ?>"></td>
                    <input type="hidden" name="id"  value="<?php echo $ids; ?>">
                </tr>
                <?php } ?>
                </foreach>
                <tr align="center">
                    <td colspan="3"><input type="submit" value="修改">&nbsp;&nbsp;&nbsp;<input type="reset" value="重置"></td>
                    
                </tr>
            </table>
        </form>
    </body>
</html>

 

posted @ 2015-11-13 00:13  夺命雷公狗  阅读(253)  评论(0编辑  收藏  举报