夺命雷公狗---微信开发50----获取用户列表接口(2)

让用户列表通过遍历进行返回,说白了就是查寻数据库然后进行遍历即可实现效果,

我们先来创建一个show_users.php的文件,代码如下所示:

<?php
    //链接数据库
    $connect = mysql_connect('localhost','root','root');
    mysql_select_db('wxdb',$connect);
    mysql_query('set names utf8');
    //分页开工咯,先取个别名counts
    $sql = "select count(*) as counts from userinfo2";
    $res = mysql_query($sql);
    $row = mysql_fetch_assoc($res);
    //这里的$row_num是总的咨询条数
    $row_num = $row['counts'];
    //我们规定每页显示5条记录
    $page_size = 5;
    //计算总页数
    $page_count = ceil($row_num/$page_size);
    //接收当前用户点击的是第几页
    $page_num = $_GET['page_num'];
    //判断$page_num是否为空,如果是空的默认给他一个1
    if(empty($page_num)){
        $page_num = 1;
    }
    //计算从那条记录开始获取
    $begin = ($page_num-1)*$page_size;
    $sql = "select * from userinfo2 order by id desc limit {$begin},{$page_size}";
    $info_res = mysql_query($sql);
?>
<!doctype html> 
<html> 
    <head> 
        <meta charset="utf-8">
        <title>微信粉丝列表</title>
        <meta name="viewport" content="width=device-width,height=device-height,inital-scale=1.0,maximum-scale=1.0,user-scalable=no;">
        <meta name="apple-mobile-web-app-capable" content="yes">
        <meta name="apple-mobile-web-app-status-bar-style" content="black"> 
        <meta name="format-detection" content="telephone=no"> 
        <link href="./jquery.mobile-1.0.min.css" rel="stylesheet" type="text/css"> 
        <script src="./jquery-1.6.4.min.js" type="text/javascript"></script> 
        <script src="./jquery.mobile-1.0.min.js" type="text/javascript"></script> 
    </head> 
    <body>
        <div data-role='page' id='page3'>
            <div data-role='header'>
                <h1>微信粉丝列表</h1>
            </div>
            
            <div data-role='content'>
                <ul data-role='listview'>
                    <?php
                        $row = array();
                        while($row = mysql_fetch_assoc($info_res)){ 
                    ?>
                        <li height='200px'>
                            昵称:<?php echo $row['nickname']; ?><br />
                            地址:<?php echo $row['address']; ?>
                            <p class='ui-li-aside'><img width='40px' src='<?php echo $row['headurl']; ?>'></p>
                        </li>
                    <?php } ?>
                </ul>
            </div>
            
            <!--分页的按钮开始-->
            <div data-role="controlgroup" data-type="horizontal">
            <?php
                if($page_num!=1 && $page_num>1){ ?>
                    <a href="/show_users.php?page_num=<?php echo $page_num-1; ?>" data-role="button">上一页</a>
                <?php } ?>
            <?php
                if($page_count > $page_num){ ?>
                    <a href="/show_users.php?page_num=<?php echo $page_num+1; ?>" data-role="button">下一页</a>
                <?php } ?>
            </div>
            <!--分页的按钮结束-->
        </div>
    </body> 
</html>

 

 

最终访问效果如下图所示:

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