ThinkPHP 分页实现

注意:要先在配置文件里配置如下代码:

<?php
return array(
    //'配置项'=>'配置值'
     /* 数据库设置 */
    'APP_DEBUG'=> true,
    'SHOW_PAGE_TRACE'=>true,
    'DB_TYPE'               => 'mysql',     // 数据库类型
    'DB_HOST'               => 'localhost', // 服务器地址
    'DB_NAME'               => '7799520',          // 数据库名
    'DB_USER'               => 'root',      // 用户名
    'DB_PWD'                => '222222',          // 密码
    'DB_PORT'               => '3306',        // 端口
    'DB_PREFIX'             => '',    // 数据库表前缀
    'DB_FIELDTYPE_CHECK'    => false,       // 是否进行字段类型检查
    'DB_FIELDS_CACHE'       => true,        // 启用字段缓存
    'DB_CHARSET'            => 'utf8',      // 数据库编码默认采用utf8
    'DB_DEPLOY_TYPE'        => 0, // 数据库部署方式:0 集中式(单一服务器),1 分布式(主从服务器)
    'DB_RW_SEPARATE'        => false,       // 数据库读写是否分离 主从式有效
    'DB_MASTER_NUM'         => 1, // 读写分离后 主服务器数量
    'DB_SQL_BUILD_CACHE'    => false, // 数据库查询的SQL创建缓存
    'DB_SQL_BUILD_QUEUE'    => 'file',   // SQL缓存队列的缓存方式 支持 file xcache和apc
    'DB_SQL_BUILD_LENGTH'   => 20, // SQL缓存的队列长度
    'VAR_PAGE'=>'p',       //分页参数,这个一定要配置
);
?>

 

 

先在控制器里实现这样的代码:

 

 

<?php
// 本类由系统自动生成,仅供测试用途
class IndexAction extends Action {
    public function index(){

        $moden=M(' customers');
        /*分页*/
        import("ORG.Util.Page");// 导入分页类
        $count= $moden->count();// 查询满足要求的总记录数
        $Page= new Page($count,15);// 实例化分页类 传入总记录数和每页显示的记录数
        $show= $Page->show();// 分页显示输出
        $rs=$moden->field("id,account,qq,sex_id,nick")->limit($Page->firstRow.','.$Page->listRows)->select();
        $this->assign('page',$show);// 赋值分页输出
        /*分页结束*/
        $this->assign('list',$rs);
        $this->display();
    }
}

然后在模板里调用,具体模板里代码如下:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<title>Examples</title>
<meta name="description" content="">
<meta name="keywords" content="">
<link href="" rel="stylesheet">
</head>
<body>
    <table align="center" width="800" border="1">
            <tr>
                    <td>ID</td>
                    <td>账号</td>
                    <td>QQ</td>
                    <td>性别</td>
                    <td>名字</td>
            </tr>
        <foreach name="list" item="vo">
            <tr>
                <td>{$vo.id}</td>
                <td>{$vo.account}</td>
                <td>{$vo.qq}</td>
                <td>{$vo.sex_id}</td>
                <td>{$vo.nick}</td>
            
            </tr>
        </foreach>
    </table>
    <div>{$page}</div>
</body>
</html>

 效果如下:

 

posted @ 2015-06-03 16:57  天涯alone  阅读(194)  评论(0)    收藏  举报