数据分页可能是 web 编程里最常用到的功能之一 ThinkPHP 实现分页功能十分简洁只需要定义几个参数就可以实现并且扩展也十分方便
下面让我们从零开始实现 ThinkPHP 的分页程序吧
1. 首先, 我们得创建一个用于分页测试的数据库 test.sql 代码如下
- CREATE TABLE `test` (`id` int(10) unsigned NOT NULL auto_increment,
- `name` char(100) NOT NULL,
- `content` varchar(300) NOT NULL,
- PRIMARY KEY (`id`)
- ) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=27 ;
- INSERT INTO `test` (`id`, `name`, `content`) VALUES
- (19, '123', '123'),
- (20, '1231', '123123123'),
- (21, '123123', '123123123'),
- (26, '24', '123123'),
- (25, '321123', '321123'),
- (24, 'age', 'age'),
- (23, '123123', '123123'),
- (22, '213', '123');
2. 接着, 我们得新建一个 ThinkPHP 项目新版 tp 已经内置了项目自动生成目录功能
在 htdocs(也就是你的网站根目录)下新建一个 test 文件夹, 把 THINKPHP 核心文件夹放进 test 根目录, 并在 test 根目录新建文件 index.php, 加入如下代码:
- // 定义 ThinkPHP 框架路径
- define('THINK_PATH', './Thinkphp');
- // 定义项目名称和路径这 2 句是重点
- define('APP_NAME', 'test');
- define('APP_PATH', './test');
- // 加载框架入口文件
- require(THINK_PATH."/ThinkPHP.php");
- // 实例化一个网站应用实例
- $App = new App();
- // 应用程序初始化
- $App->run();
运行 http://localhost/test/index.php. 会看到 ThinkPHP 的欢迎页面再打开你的 test 目录看看, 发现在根目录下多了一个 test 文件夹, 此时, 你的项目目录已经生成了
打开 / test/test/conf / 目录, 新建 config.php , 配置好你的数据库连接
- <?php
- return array(
- 'DB_TYPE'=>'mysql',
- 'DB_HOST'=>'localhost',
- 'DB_NAME'=>'test', // 新建的数据库名 test
- 'DB_USER'=>'root', // 数据库用户名
- 'DB_PWD'=>'', // 数据库密码'DB_PORT'=>'3306',
- );
- ?>
如果你想打开调试模式, 请在数组中加入
"debug_mode"=>true
3. 基本页面输入与输出的实现
(1)打开 / test/test/lib/action/IndexAction.class.php, 会发现以下代码
- <?php
- // 本类由系统自动生成, 仅供测试用途
- class IndexAction extends Action{
- public function index(){
- header("Content-Type:text/html; charset=utf-8");
- echo "^_^ Hello, 欢迎使用 ThinkPHP";
- }
- }
- ?>
由系统自动生成的 indexaction 类中的 index()函数是默认的首页调用函数你可以使用 http://localhost/test/index.php 或者 http://localhost/test/index.php/index 来访问他
(2)我们暂时不管他首先我们需要一个表单提交的页面打开 / test/test/tpl/default/index/, 新建一个文件 add.html.
姓名:
内容:
提交:
保存后, 输入 http://localhost/test/index.php/index/add, 你就能看到你新增的页面了其中,/index.php/Article(url 要大写)被转换为相应地址 / test/index.php/Index/.
这里简单说一下模板和 action 之间的关系每一个 action, 对应的模板是与之名字相同的 html 文件例如 index 类下的 index(), 对应 default/index/index.html, 而 add.html, 则显然对应的是 index 类下的 add()
我们甚至可以在只有 add.html 而没有相应的 add()动作情况下, 用访问 add()的形式 (http://localhost/test/index.php/index/add) 来访问 add.html 模板 add.html 模板下的占位符会被替换成相应的数据效果如下
(3)从 form 的 action=/index.php/Article/insert 中可以看出, 进行表单处理的动作是 / test/index.php/index/insert, 所以我们得新增 insert 动作来处理表单提交数据在此之前, 我们还有一件重要的事情要做, 那就是新增 model 文件通过 model 文件的建立, 我们将能在 insert 动作中使用便捷的方法来操作数据库了
打开 / test/test/lib/model / 文件夹, 新建文件 TestModel.class.php. 打开他, 输入并保存以下代码
- <?php
- class TestModel extends Model {
- }
- ?>
简单的说, 这是 ActiveRecord 实现的基本文件命名规则是你数据库中的表后面加 Model. 例如我们将要使用到的表是 test, 我的文件命名必须是 TestModel.class.php, 而文件下的类命名必须是 TestModel.
接着, 我们回到 indexaction.class.php 文件, 删除原来的代码, 加入如下代码
- class IndexAction extends Action {
- // 表单数据添加到数据库
- public
- function insert() {
- // 实例化我们刚才新建的 testmodel.
- $test = D('Test');
- if ($test - >create()) {
- // 保存表单数据就这一步 thinkphp 已经全部做完了
- $test - >add();
- $this - >redirect();
- } else {
- exit($test - >getError()'[ 返 回 ]');
- }
- }
- }
(4)接下来, 我们需要在 IndexAction 类中增加一个首页默认显示动作 index()来调用表单数据
- public
- function index() {
- // 依旧是实例化我们新建的对应相应表名的 model. 这是我们进行快捷表操作的重要关键
- $test = D('Test');
- // 熟悉这段代码么? 计算所有的行数
- $count = $test - >count('','id');
- // 每页显示的行数
- $listRows = '3';
- // 需要查询哪些字段
- $fields = 'id,name,content';
- // 导入分页类 /ThinkPHP/lib/ORG/Util/Page.class.php
- import("ORG.Util.Page");
- // 通过类的构造函数来改变 page 的参数 $count 为总数,$listrows 为每一页的显示条目
- $p = new Page($count, $listRows);
- // 设置查询参数具体见 ThinkPHP/Lib/Think/Core/Model.class.php1731 行
- $list = $test - >findall('', $fields,'id desc', $p - >firstRow.','.$p - >listRows);
- // 分页类做好了
- $page = $p - >show();
- // 模板输出
- $this - >assign('list', $list);
- $this - >assign('page', $page);
- $this - >display();
- }
我们该设置一个模板了在 / test/test/tpl/default/index / 下新建 index.html(因为默认对应了 index()所以程序中可以直接 assign. 而不用去指定模板文件当然, 这是可以配置的)
填写
- // 分页显示, 这一行
- {$page}
- // 数据显示下面的参数很快会再进行详解它很好理解
姓名:{$vo.name}
内容:{$vo.content}
保存他接着输入 http://localhost/test/
恭喜你你已经学会了如何利用 thinkphp 制作分页了!
来源: https://www.php1.cn/detail/php-a015ea55fc.html