Data paging may be one of the most common features in web programming. thinkphp implementation of paging function is very concise. You just need to define a few parameters to implement it. And the expansion is also very convenient.
Let's start by implementing Thinkphp's paging program from scratch.
1. First, we have to create a database Test.sql code for paging tests as follows.
CREATE TABLE ' test ' (
' id ' int (a) unsigned NOT null auto_increment,
' name ' char (MB) not NULL,
' content ' Varc Har () not NULL,
PRIMARY KEY (' id ')
) Engine=myisam DEFAULT Charset=utf8 auto_increment=27;
INSERT into ' test ' (' id ', ' name ', ' content ') VALUES
(' 123 ', ' 123 '), ('
1231 ', ' 123123123 '),
(21, ' 1231 ", ' 123123123 '),
(num, ', ' 123123 '), ((),
' 321123 ', ' 321123 '), (" Age
", ' Age '), (
23, ' 123123 ', ' 123123 '),
(22, ' 213 ', ' 123 ');
2. Next, we have to create a new thinkphp project. The new version of TP has a built-in project to automatically generate directory functionality.
Create a new test folder under Htdocs (that is, your site root), put the thinkphp Core folder in the test root directory, and create a new file index.php in the test root directory, add the following code:
Defines the thinkphp framework path
define (' Think_path ', './thinkphp ');
Define the project name and path. These 2 sentences are the focus.
define (' app_name ', ' test ');
Define (' App_path ', './test ');
Load frame Entry file
require (Think_path.) /thinkphp.php ");
Instantiate an instance of a Web site application
$App = new app ();
Application initialization
$App->run ();
Run "http://localhost/test/index.php". You will see the thinkphp Welcome page. Open your test directory again and see that there is a test folder in the root directory, at which point 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 username
' db_pwd ' => ',//Database password ' db_port ' => '
3306 ',
);
? >
If you want to turn on debug mode, please add in the array
3. Basic page input and output implementation.
(1) Open/test/test/lib/action/indexaction.class.php, you will find the following code
<?php
//This class is automatically generated by the system, only for test purposes
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;p adding:8px;font-size:14px;font-family:tahoma ' > ^_^ Hello, Welcome to use <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 calling function. You can visit him using http://localhost/test/index.php or Http://localhost/test/index.php/index.
(2) We don't care about him for the time being. First we need a form to submit the page. 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> Submit: <input type= "Submit" value= "Submit" ></p>
</form>
After you save it, enter Http://localhost/test/index.php/index/add and you'll see the new page. Where the __url__ (the URL is to be capitalized) is converted to the corresponding address/test/index.php/index/.
Here's a quick look at the relationship between the template and the action. Each action, the corresponding template is the same as the name of the HTML file. For example, the index () under the Index class, corresponding to the default/index/index.html, and add.html, is obviously corresponding to the index class add ().
We can even access the add.html template in the form of access to add () (Http://localhost/test/index.php/index/add) without the corresponding add () action in add.html. Placeholders under the add.html template are replaced with the corresponding data. The effect is as follows.
(3) From the form of "Action=__url__/insert" can be seen in the form of the action is/test/index.php/index/insert, so we need to add insert action to process the form submission data. Before that, we have another important thing to do, and that is to add the model file. By building the model file, we will be able to use the easy way to manipulate the database in the Insert action
Open the/test/test/lib/model/folder and create a new file TestModel.class.php. Open him, enter and save the following code
<?php
class Testmodel extends Model {
}
?>
Simply put, this is the basic file for ActiveRecord implementation. The naming convention is behind the tables in your database plus model. 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 is added to the database public
function insert () {
//instantiate the Testmodel we just created.
$test = D (' test ');
if ($test->create ()) {
//Save the form data this step. The thinkphp have all been finished.
$test->add ();
$this->redirect ();
} else{
exit ($test->geterror (). ' [<a href= ' Javascript:history.back () ' > Return </A>] ');}}
(4) Next, we need to add a homepage in the Indexaction class default display action index () to invoke the form data.
The Public Function index () {
//is still the model that instantiates our new corresponding table name. This is an important key to our shortcut table operation.
$test = D (' test ');
Are you familiar with this piece of code? Count all rows
$count = $test->count (', ' id ');
The number of rows displayed per page
$listRows = ' 3 ';
What fields need to be queried
$fields = ' id,name,content ';
Import a paging class/thinkphp/lib/org/util/page.class.php
import ("ORG.") Util.page ");
Change the parameters of the page by using the constructor of the class. $count is the total, $listrows is the display entry for each page.
$p = new Page ($count, $listRows);
Set query parameters. See "thinkphp/lib/think/core/model.class.php" 1731 lines in detail.
$list = $test->findall (', $fields, ' id desc ', $p->firstrow. ', '. $p->listrows);
The paging class is done.
$page = $p->show ();
Template Output
$this->assign (' list ', $list);
$this->assign (' page ', $page);
$this->display ();
}
We should set up a template. Create a new index.html under/test/test/tpl/default/index/(because the default corresponds to index (). So the program can be directly assign without having to specify the template file. Of course, this can be configured. )
Save him. then enter http://localhost/test/
Congratulations to you. You've learned how to use thinkphp to make pagination!