这篇文章主要介绍了thinkphp快速入门实例教程的数据分页实现过程,需要的朋友可以参考下
数据分页可能是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” ,配置好你的数据库连接。
<?phpreturn 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 "<p style='font-weight:normal;color:blue;float:left;width:345px;text-align:center;border:1px solid silver;background:#e8efff;padding:8px;font-size:14px;font-family:tahoma'>^_^ hello,欢迎使用<span style='font-weight:bold;color:red'>thinkphp</span></p>";}}?>
由系统自动生成的indexaction类中的index()函数是默认的首页调用函数。你可以使用http://localhost/test/index.php或者http://localhost/test/index.php/index来访问他
(2)我们暂时不管他。首先我们需要一个表单提交的页面。打开“/test/test/tpl/default/index/”,新建一个文件add.html.
<form method="post" action="__url__/insert"><p>姓名:<input name="name" type="text" ></p><p>内容:<input name="content" type="text"></p><p>提交:<input type="submit" value="submit"></p></form>
保存后,输入 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模板下的占位符会被替换成相应的数据。效果如下。
(3)从form的“action=__url__/insert”中可以看出,进行表单处理的动作是/test/index.php/index/insert,所以我们得新增insert动作来处理表单提交数据。在此之前,我们还有一件重要的事情要做,那就是新增model文件。通过model文件的建立,我们将能在insert动作中使用便捷的方法来操作数据库了
打开/test/test/lib/model/文件夹,新建文件testmodel.class.php.打开他,输入并保存以下代码
<?phpclass 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()。'[ <a href="javascript:history.back()">返 回</a> ]');}}}
(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.phpimport("org.util.page");//通过类的构造函数来改变page的参数。$count为总数,$listrows为每一页的显示条目。$p = new page($count,$listrows);//设置查询参数。具体见“thinkphp/lib/think/core/model.class.php”1731行。$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.而不用去指定模板文件。当然,这是可以配置的。)
<hr><a href="__url__/add">填写</a>//分页显示,这一行<hr>{$page}<hr>//数据显示。下面的参数很快会再进行详解。它很好理解。<volist name="list" id="vo"><p>姓名:{$vo.name}</p><p>内容:{$vo.content}</p><hr></volist>
保存他。接着输入 http://localhost/test/
恭喜你。你已经学会了如何利用thinkphp制作分页了
以上就是本文的全部内容,希望对大家的学习有所帮助,更多相关内容请关注!
相关推荐:
php后端方法实现网页的分页下标生成代码
关于thinkphp3.2实现分页自定义样式的方法
关于thinkphp框架添加js事件分页类custompage.class.php的分析
以上就是thinkphp的数据分页的详细内容。