Data Paging for ThinkPHP Quick Start Example Tutorial

  • 2021-07-06 10:21:26
  • OfStack

Data paging is probably one of the most commonly used functions in web programming. ThinkPHP realizes paging function 10 points concise. You only need to define a few parameters to achieve it. And the expansion is 10 points convenient.

Let's implement the paging program of ThinkPHP from scratch.

1. First, we have to create a database test. sql for paging testing with the following code.


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. Next, we have to create a new ThinkPHP project. The new version of tp has built-in the function of automatically generating directories for projects.
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, adding the following code:


//  Definition ThinkPHP Frame path 
define('THINK_PATH', './Thinkphp');
// Define the project name and path. This 2 Sentence is the point. 
define('APP_NAME', 'test');
define('APP_PATH', './test');
//  Load the framework entry file 
require(THINK_PATH."/ThinkPHP.php");
// Instantiation 1 Web site application examples 
$App = new App();
// Application initialization 
$App->run();

Run "http://localhost/test/index. php". You will see the welcome page of ThinkPHP. Open your test directory again, and find that there is one more test folder under 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(
'DB_TYPE'=>'mysql',
'DB_HOST'=>'localhost',
'DB_NAME'=>'test', // New database name test
'DB_USER'=>'root', // Database user name 
'DB_PWD'=>'', // Database password 
'DB_PORT'=>'3306',
);
?>

If you want to turn on debug mode, add the


"debug_mode"=>true

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


<?php
//  This class is automatically generated by the system and is for testing purposes only 
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, Welcome to <span style='font-weight:bold;color:red'>ThinkPHP</span></div>";
}
}
?>

The index () function in the indexaction class, which is automatically generated by the system, is the default home page calling function. You can access him using http://localhost/test/index.php or http://localhost/test/index.php/index

(2) We leave him alone for the time being. First, we need a page for form submission. Open "/test/test/tpl/default/index/" and create a new file add. html.


<form method="post" action="__URL__/insert">
<p> Name: <input name="name" type="text" ></p>
<p> Content: <input name="content" type="text"></p>
<p> Submission: <input type="submit" value="submit"></p>
</form>

After saving, enter http://localhost/test/index.php/index/add, and you will see your new page. Where __URL__ (url to be capitalized) is translated to the corresponding address/test/index. php/Index/.
Here, I briefly talk about the relationship between template and action. For every action, the corresponding template is an html file with the same name. For example, index () under index corresponds to default/index/index. html, while add. html obviously corresponds to add () under index.
We can even access the add.html template in the form of accessing add () (http://localhost/test/index.php/index/add) with only add.html and no corresponding add () action. The placeholders under the add. html template are replaced with the corresponding data. The effect is as follows.

(3) As can be seen from "action=__URL__/insert" in form, the action to process the form is/test/index. php/index/insert, so we have to add an insert action to process the form submission data. Before that, we have one more important thing to do, which is to add model file. Through the establishment of model file, we will be able to use a convenient method to operate the database in 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 {
}
?>

Simply put, this is the basic file for ActiveRecord implementation. The naming rule is to add Model to 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 go back to the indexaction. class. php file, delete the original code, and add the following code.


class IndexAction extends Action{
// Form data added to the database 
public function insert() {
// Instantiate the new testmodel.
$test = D('Test');
if ($test->create()) {
// Save the form data here 1 Step. thinkphp It's all done. 
$test->add();
$this->redirect();
}else{
exit($test->getError() . '[ <A HREF="javascript:history.back()"> Return   Gback </A> ]');
}
}
}

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


public function index() {
// Is still instantiating the new one we created corresponding to the corresponding table name model. This is an important key for us to operate the shortcut table. 
$test = D('Test');
// Are you familiar with this code? Count all rows 
$count = $test->count('','id');
// Number of rows displayed per page 
$listRows = '3';
// Which fields need to be queried 
$fields = 'id,name,content';
// Import paging classes  /ThinkPHP/lib/ORG/Util/Page.class.php
import("ORG.Util.Page");
// Through the constructor of the class to change page The parameters of the. $count Is the total, $listrows For each 1 Display entries for the page. 
$p = new Page($count,$listRows);
// Set query parameters. See " ThinkPHP/Lib/Think/Core/Model.class.php " 1731 OK. 
$list = $test->findall('',$fields,'id desc',$p->firstRow.','.$p->listRows);
// The paging class is ready. 
$page = $p->show();
// Template output 
$this->assign('list',$list);
$this->assign('page',$page);
$this->display();
}

It's time for us to set up a template. Create a new index. html under/test/test/tpl/default/index/(because it corresponds to index () by default. Therefore, assign can be directly used in the program. Without specifying the template file. Of course, this is configurable. )


<hr><a href="__URL__/add"> Fill in </a>
// Paging shows that this 1 Row 
<hr>{$page}<hr>
// The data shows. The following parameters will be explained in detail soon. It's easy to understand. 
<volist name="list" id="vo">
<p> Name: {$vo.name}</p>
<p> Content: {$vo.content}</p>
<hr>
</volist>

Save him. Then enter http://localhost/test/
Congratulations. You have learned how to use thinkphp to make paging!


Related articles: