thinkphp实例教程之数据分页

原创
2016-07-25 08:53:02744浏览
  1. create table `test` (
  2. `id` int(10) unsigned not null auto_increment,
  3. `name` char(100) not null,
  4. `content` varchar(300) not null,
  5. primary key (`id`)
  6. ) engine=myisam default charset=utf8 auto_increment=27 ;
  7. insert into `test` (`id`, `name`, `content`) values
  8. (19, '123', '123'),
  9. (20, '1231', '123123123'),
  10. (21, '123123', '123123123'),
  11. (26, '24', '123123'),
  12. (25, '321123', '321123'),
  13. (24, 'age', 'age'),
  14. (23, '123123', '123123'),
  15. (22, '213', '123');
复制代码

2,新建一个thinkphp项目。新版tp已经内置了项目自动生成目录功能。 在htdocs(也就是你的网站根目录)下新建一个test文件夹,把thinkphp核心文件夹放进test根目录,并在test根目录新建文件index.php,加入如下代码:

  1. // 定义thinkphp框架路径
  2. define('think_path', './thinkphp');
  3. //定义项目名称和路径。这2句是重点。
  4. define('app_name', 'test');
  5. define('app_path', './test');
  6. // 加载框架入口文件
  7. require(think_path."/thinkphp.php");
  8. //实例化一个网站应用实例
  9. $app = new app();
  10. //应用程序初始化
  11. $app->run();
复制代码

运行“http://localhost/test/index.php”.会看到thinkphp的欢迎页面。再打开你的test目录看看,发现在根目录下多了一个test文件夹,此时,你的项目目录已经生成了。

打开/test/test/conf/目录,新建“config.php” ,配置好数据库连接。

  1. return array(
  2. 'db_type'=>'mysql',
  3. 'db_host'=>'localhost',
  4. 'db_name'=>'test', //新建的数据库名test
  5. 'db_user'=>'root', //数据库用户名
  6. 'db_pwd'=>'', //数据库密码
  7. 'db_port'=>'3306',
  8. );
  9. ?>
复制代码

如果想打开调试模式,请在数组中加入: "debug_mode"=>true

3,基本页面输入与输出的实现。 1)打开/test/test/lib/action/indexaction.class.php,找到:

  1. // 本类由系统自动生成,仅供测试用途
  2. class indexaction extends action{
  3. public function index(){
  4. header("content-type:text/html; charset=utf-8");
  5. echo "
    ^_^ hello,欢迎使用thinkphp
    ";
  6. }
  7. }
  8. ?>
复制代码

由系统自动生成的indexaction类中的index()函数是默认的首页调用函数。你可以使用http://localhost/test/index.php或者http://localhost/test/index.php/index来访问他

2)暂时不管他。首先,需要一个表单提交的页面。打开“/test/test/tpl/default/index/”,新建一个文件add.html.

  1. 姓名:

  2. 内容:

  3. 提交:

复制代码

保存后,输入 http://localhost/test/index.php/index/add,你就能看到你新增的页面了。其中,__url__(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模板下的占位符会被替换成相应的数据。(脚本学堂 编辑整理 bbs.it-home.org)

3)从form的“action=__url__/insert”中可以看出,进行表单处理的动作是/test/index.php/index/insert,所以我们得新增insert动作来处理表单提交数据。在此之前,我们还有一件重要的事情要做,那就是新增model文件。通过model文件的建立,我们将能在insert动作中使用便捷的方法来操作数据库了 打开/test/test/lib/model/文件夹,新建文件testmodel.class.php.打开他,输入并保存以下代码

  1. class testmodel extends model {
  2. }
  3. ?>
复制代码

这是activerecord实现的基本文件。 命名规则是你数据库中的表后面加model。 例如将要使用到的表是test,文件命名必须是testmodel.class.php,而文件下的类命名必须是testmodel. 接着,回到indexaction.class.php文件,删除原来的代码,加入:

  1. class indexaction extends action{
  2. //表单数据添加到数据库
  3. public function insert() {
  4. //实例化我们刚才新建的testmodel.
  5. $test = d('test');
  6. if ($test->create()) {
  7. //保存表单数据就这一步。thinkphp已经全部做完了。
  8. $test->add();
  9. $this->redirect();
  10. }else{
  11. exit($test->geterror()。'[ 返 回 ]');
  12. }
  13. }
  14. }
复制代码

4)接下来,需要在indexaction类中增加一个首页默认显示动作index()来调用表单数据。

  1. public function index() {
  2. //依旧是实例化我们新建的对应相应表名的model.这是我们进行快捷表操作的重要关键。
  3. $test = d('test');
  4. //熟悉这段代码么?计算所有的行数
  5. $count = $test->count('','id');
  6. //每页显示的行数
  7. $listrows = '3';
  8. //需要查询哪些字段
  9. $fields = 'id,name,content';
  10. //导入分页类 /thinkphp/lib/org/util/page.class.php
  11. import("org.util.page");
  12. //通过类的构造函数来改变page的参数。$count为总数,$listrows为每一页的显示条目。
  13. $p = new page($count,$listrows);
  14. //设置查询参数。具体见“thinkphp/lib/think/core/model.class.php”1731行。
  15. $list = $test->findall('',$fields,'id desc',$p->firstrow.','.$p->listrows);
  16. //分页类做好了。
  17. $page = $p->show();
  18. //模板输出
  19. $this->assign('list',$list);
  20. $this->assign('page',$page);
  21. $this->display();
  22. }
复制代码

设置一个模板,在/test/test/tpl/default/index/下新建index.html(因为默认对应了index()。 程序中可以直接assign.而不用去指定模板文件。当然,这是可以配置的。)


  1. 填写
  2. //分页显示,这一行

  3. {$page}
  4. //数据显示。下面的参数很快会再进行详解。它很好理解。
  5. 姓名:{$vo.name}

  6. 内容:{$vo.content}


复制代码

保存,输入 http://localhost/test/

以上就是thinkphp制作分页的方法与实例,希望对大家有所帮助。



声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。
上一条:PHP模拟登录QQ邮箱获取QQ好友列表的方法 下一条:yii操作cookie方法示例

学习路径

查看更多

相关文章

查看更多