ThinkPHP Quick Start Example Tutorial Data Pagination_PHP Tutorial

WBOY
Release: 2016-07-13 10:26:29
Original
654 people have browsed 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.

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

If you want to turn on debug mode, please add

to the array
"debug_mode"=>true
Copy after login

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

<&#63;php
// 本类由系统自动生成,仅供测试用途
class IndexAction extends Action{
public function index(){
header("Content-Type:text/html; charset=utf-8");
echo "<div 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></div>";
}
}
&#63;>
Copy after login

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

(2) Let’s ignore him for now. First we need a form submission page. Open "/test/test/tpl/default/index/" and 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, and you will be able to see your new page. Among them, __URL__ (url should be capitalized) is converted to the corresponding address /test/index.php/Index/.
Here is a brief talk about 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 the add.html template in the form of accessing add() (http://localhost/test/index.php/index/add) when there is only add.html but no corresponding add() action. Placeholders under the add.html template will be replaced with corresponding data. The effect is as follows.

(3) From the "action=__URL__/insert" of the form, we can see that the action for form processing is /test/index.php/index/insert, so we have to add a new insert action to process the 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

<&#63;php
class TestModel extends Model {
}
&#63;>
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.

Then, 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()。'[ <A HREF="javascript:history.back()">返 回</A> ]');
}
}
}
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.)

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

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/824680.htmlTechArticleData 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. Next...
Related labels:
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
Popular Tutorials
More>
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!