Home Backend Development PHP Tutorial ThinkPHP data paging

ThinkPHP data paging

Jun 08, 2018 pm 03:28 PM
thinkphp Example tutorial Quick start Data paging

This article mainly introduces the data paging implementation process of the ThinkPHP quick start tutorial. Friends who need it can refer to it

Data paging may be one of the most commonly used functions in web programming. ThinkPHP implements paging function very simply. This can be achieved by just defining a few parameters. And it is also very easy to expand.

Let’s implement ThinkPHP’s paging program from scratch.

1. First, we have to create a database for paging testing. The test.sql code is as follows.

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');
Copy after login

2. Next, we have to create a new ThinkPHP project. The new version of tp has built-in automatic project directory generation function.
Create a new test folder under htdocs (that is, the root directory of your website), put the THINKPHP core folder into the test root directory, and create a new file index.php in the test root directory, and add the following code:

// 定义ThinkPHP框架路径
define('THINK_PATH', './Thinkphp');
//定义项目名称和路径。这2句是重点。
define('APP_NAME', 'test');
define('APP_PATH', './test');
// 加载框架入口文件
require(THINK_PATH."/ThinkPHP.php");
//实例化一个网站应用实例
$App = new App();
//应用程序初始化
$App->run();
Copy after login

Run "http://localhost/test/index.php". You will see the ThinkPHP welcome page. Open your test directory again and find that there is an additional test folder in the root directory. At this time, your project directory has been generated.
Open the /test/test/conf/ directory, create a new "config.php", and configure your database connection.

<?php
return array(
&#39;DB_TYPE&#39;=>&#39;mysql&#39;,
&#39;DB_HOST&#39;=>&#39;localhost&#39;,
&#39;DB_NAME&#39;=>&#39;test&#39;, //新建的数据库名test
&#39;DB_USER&#39;=>&#39;root&#39;, //数据库用户名
&#39;DB_PWD&#39;=>&#39;&#39;, //数据库密码
&#39;DB_PORT&#39;=>&#39;3306&#39;,
);
?>
Copy after login

If you want to turn on debug mode, please add

"debug_mode"=>true
Copy after login

to the array 3. Implementation of basic page input and output.
(1) Open /test/test/lib/action/IndexAction.class.php, you will find the following code

<?php
// 本类由系统自动生成,仅供测试用途
class IndexAction extends Action{
public function index(){
header("Content-Type:text/html; charset=utf-8");
echo "<p style=&#39;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&#39;>^_^ Hello,欢迎使用<span style=&#39;font-weight:bold;color:red&#39;>ThinkPHP</span></p>";
}
}
?>
Copy after login

is automatically generated by the system The index() function in the indexaction class is the default home page call function. You can use http://localhost/test/index.php or http://localhost/test/index.php/index to access it

(2) We will ignore it for now. First we need a form submission page. Open "/test/test/tpl/default/index/", create a new file 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>
Copy after login

After saving, enter http://localhost /test/index.php/index/add, you will be able to see the page you added. Among them, __URL__ (url should be capitalized) is converted to the corresponding address /test/index.php/Index/.
Here is a brief introduction to the relationship between templates and actions. For each action, the corresponding template is an html file with the same name. For example, index() under the index class corresponds to default/index/index.html, and add.html obviously corresponds to add() under the index class.
We can even access add in the form of accessing add() (http://localhost/test/index.php/index/add) when there is only add.html and no corresponding add() action. html template. Placeholders under the add.html template will be replaced with corresponding data. The effect is as follows.

(3) It can be seen from the "action=__URL__/insert" of the form that the action for form processing is /test/index.php/index/insert, so we have to add the insert action to process it. Form submission data. Before that, we still have one important thing to do, which is to add a new model file. Through the creation of the model file, we will be able to use convenient methods to operate the database in the insert action.
Open the /test/test/lib/model/ folder and create a new file TestModel.class.php. Open it, enter and Save the following code

<?php
class TestModel extends Model {
}
?>
Copy after login

Simply put, this is the basic file for ActiveRecord implementation. The naming rule is to add Model after the table in your database. For example, the table we are going to use is test, my file name must be TestModel.class.php, and the class name under the file must be TestModel.

Next, we return to the indexaction.class.php file, delete the original code, and add the following code.

class IndexAction extends Action{
//表单数据添加到数据库
public function insert() {
//实例化我们刚才新建的testmodel.
$test = D(&#39;Test&#39;);
if ($test->create()) {
//保存表单数据就这一步。thinkphp已经全部做完了。
$test->add();
$this->redirect();
}else{
exit($test->getError()。&#39;[ <A HREF="javascript:history.back()">返 回</A> ]&#39;);
}
}
}
Copy after login

(4) Next, we need to add a homepage default display action index() to the IndexAction class to call the form data.

public function index() {
//依旧是实例化我们新建的对应相应表名的model.这是我们进行快捷表操作的重要关键。
$test = D(&#39;Test&#39;);
//熟悉这段代码么?计算所有的行数
$count = $test->count(&#39;&#39;,&#39;id&#39;);
//每页显示的行数
$listRows = &#39;3&#39;;
//需要查询哪些字段
$fields = &#39;id,name,content&#39;;
//导入分页类 /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(&#39;&#39;,$fields,&#39;id desc&#39;,$p->firstRow.&#39;,&#39;.$p->listRows);
//分页类做好了。
$page = $p->show();
//模板输出
$this->assign(&#39;list&#39;,$list);
$this->assign(&#39;page&#39;,$page);
$this->display();
}
Copy after login

It’s time to set up a template. Create a new index.html under /test/test/tpl/default/index/ (because it corresponds to index() by default. Therefore, you can directly assign in the program without specifying the template file. Of course, this can be configured.)

<hr><a href="__URL__/add">填写</a>
//分页显示,这一行
<hr>{$page}<hr>
//数据显示。下面的参数很快会再进行详解。它很好理解。
<volist name="list" id="vo">
<p>姓名:{$vo.name}</p>
<p>内容:{$vo.content}</p>
<hr>
</volist>
Copy after login

Save him. Then enter http://localhost/test/
Congratulations. You have learned how to use thinkphp to create pagination

The above is the entire content of this article. I hope it will be helpful to everyone's learning. For more related content, please pay attention to the PHP Chinese website!

Related recommendations:

PHP backend method to implement paging subscript generation code for web pages

About thinkPHP3.2 implementation How to customize paging styles

Analysis on adding js event paging class customPage.class.php to thinkPHP framework

The above is the detailed content of ThinkPHP data paging. For more information, please follow other related articles on the PHP Chinese website!

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 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)

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 to use Hyperf framework for data paging How to use Hyperf framework for data paging Oct 20, 2023 am 11:25 AM

How to use the Hyperf framework for data paging Introduction: Data paging is very common in actual Web development. Paging can make it easier for users to browse large amounts of data. Hyperf is a high-performance PHP framework that provides a powerful set of features and components. This article will introduce how to use the Hyperf framework for data paging and give detailed code examples. 1. Preparation: Before starting, you need to ensure that the Hyperf framework has been correctly installed and configured. Can be done via Composer

See all articles