Home php教程 php手册 ThinkPHP快速入门实例教程之数据分页

ThinkPHP快速入门实例教程之数据分页

Jun 06, 2016 pm 08:20 PM
thinkphp Example tutorial Quick start Data paging

这篇文章主要介绍了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();

运行“”.会看到ThinkPHP的欢迎页面。再打开你的test目录看看,发现在根目录下多了一个test文件夹,此时,你的项目目录已经生成了。
打开/test/test/conf/目录,新建“config.php” ,配置好你的数据库连接。

'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,会发现以下代码

^_^ Hello,欢迎使用ThinkPHP"; } } ?>

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

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

姓名:

内容:

提交:

保存后,输入 ,你就能看到你新增的页面了。其中,__URL__(url要大写)被转换为相应地址/test/index.php/Index/.
这里简单说一下模板和action之间的关系。每一个action,对应的模板是与之名字相同的html文件。例如index类下的index(),对应default/index/index.html,而add.html,则显然对应的是index类下的add()。
我们甚至可以在只有add.html而没有相应的add()动作情况下,用访问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.打开他,输入并保存以下代码

简单的说,这是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.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.而不用去指定模板文件。当然,这是可以配置的。)

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1665
14
PHP Tutorial
1269
29
C# Tutorial
1249
24
How to run thinkphp project How to run thinkphp project Apr 09, 2024 pm 05:33 PM

To run the ThinkPHP project, you need to: install Composer; use Composer to create the project; enter the project directory and execute php bin/console serve; visit http://localhost:8000 to view the welcome page.

There are several versions of thinkphp There are several versions of thinkphp Apr 09, 2024 pm 06:09 PM

ThinkPHP has multiple versions designed for different PHP versions. Major versions include 3.2, 5.0, 5.1, and 6.0, while minor versions are used to fix bugs and provide new features. The latest stable version is ThinkPHP 6.0.16. When choosing a version, consider the PHP version, feature requirements, and community support. It is recommended to use the latest stable version for best performance and support.

How to run thinkphp How to run thinkphp Apr 09, 2024 pm 05:39 PM

Steps to run ThinkPHP Framework locally: Download and unzip ThinkPHP Framework to a local directory. Create a virtual host (optional) pointing to the ThinkPHP root directory. Configure database connection parameters. Start the web server. Initialize the ThinkPHP application. Access the ThinkPHP application URL and run it.

Python learning: How to install the pandas library in the system Python learning: How to install the pandas library in the system Jan 09, 2024 pm 04:42 PM

Quick Start: How to install the pandas library in Python requires specific code examples 1. Overview Python is a widely used programming language with a powerful development ecosystem that includes many practical libraries. Pandas is one of the most popular data analysis libraries. It provides efficient data structures and data analysis tools, making data processing and analysis easier. This article will introduce how to install the pandas library in Python and provide corresponding code examples. 2. Install Py

Which one is better, laravel or thinkphp? Which one is better, laravel or thinkphp? Apr 09, 2024 pm 03:18 PM

Performance comparison of Laravel and ThinkPHP frameworks: ThinkPHP generally performs better than Laravel, focusing on optimization and caching. Laravel performs well, but for complex applications, ThinkPHP may be a better fit.

Development suggestions: How to use the ThinkPHP framework to implement asynchronous tasks Development suggestions: How to use the ThinkPHP framework to implement asynchronous tasks Nov 22, 2023 pm 12:01 PM

"Development Suggestions: How to Use the ThinkPHP Framework to Implement Asynchronous Tasks" With the rapid development of Internet technology, Web applications have increasingly higher requirements for handling a large number of concurrent requests and complex business logic. In order to improve system performance and user experience, developers often consider using asynchronous tasks to perform some time-consuming operations, such as sending emails, processing file uploads, generating reports, etc. In the field of PHP, the ThinkPHP framework, as a popular development framework, provides some convenient ways to implement asynchronous tasks.

How to install thinkphp How to install thinkphp Apr 09, 2024 pm 05:42 PM

ThinkPHP installation steps: Prepare PHP, Composer, and MySQL environments. Create projects using Composer. Install the ThinkPHP framework and dependencies. Configure database connection. Generate application code. Launch the application and visit http://localhost:8000.

How is the performance of thinkphp? How is the performance of thinkphp? Apr 09, 2024 pm 05:24 PM

ThinkPHP is a high-performance PHP framework with advantages such as caching mechanism, code optimization, parallel processing and database optimization. Official performance tests show that it can handle more than 10,000 requests per second and is widely used in large-scale websites and enterprise systems such as JD.com and Ctrip in actual applications.

See all articles