ThinkPHP data paging

不言
Release: 2023-03-30 19:56:01
Original
1585 people have browsed it

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.

'mysql', 'DB_HOST'=>'localhost', 'DB_NAME'=>'test', //新建的数据库名test 'DB_USER'=>'root', //数据库用户名 'DB_PWD'=>'', //数据库密码 'DB_PORT'=>'3306', ); ?>
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

^_^ Hello,欢迎使用ThinkPHP

"; } } ?>
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.

姓名:

内容:

提交:

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

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('Test'); if ($test->create()) { //保存表单数据就这一步。thinkphp已经全部做完了。 $test->add(); $this->redirect(); }else{ exit($test->getError()。'[ 返 回 ]'); } } }
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('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(); }
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.)


填写 //分页显示,这一行
{$page}
//数据显示。下面的参数很快会再进行详解。它很好理解。

姓名:{$vo.name}

内容:{$vo.content}


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!

source:php.cn
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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!