ThinkPHP quick start instance tutorial-data paging _ php instance

Source: Internet
Author: User
This article mainly introduces the data paging implementation process of the ThinkPHP quick start instance tutorial. if you need it, you can refer to the data paging function, which may be one of the most commonly used functions in web programming. ThinkPHP provides simple paging functions. You only need to define several parameters. It is also very convenient to scale.

Let's implement the ThinkPHP paging program from scratch.

1. First, we need to create a database test. SQL code for paging testing 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');

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

// Define the ThinkPHP framework PATH define ('think _ path', './thinkphp'); // define the project name and PATH. These two statements are important. Define ('app _ name', 'test'); define ('app _ path ','. /test'); // load the framework entry file require (THINK_PATH. "/ThinkPHP. php "); // instantiate a website application instance $ App = new App (); // initialize the application $ App-> run ();

Run "http: // localhost/test/index. php". the welcome page of ThinkPHP is displayed. Open your test directory and check that a test folder is added to the root directory. at this time, your project directory has been generated.
Open the/test/conf/directory, create "config. php", and configure your database connection.

<? Phpreturn array ('Db _ type' => 'mysql', 'DB _ host' => 'localhost', 'DB _ name' => 'test ', // test 'DB _ user' => 'root', // database username 'DB _ pwd' => '', // database password 'DB _ port' => '123',);?>

If you want to enable the debugging mode, add it to the array.

"debug_mode"=>true

3. basic page input and output implementation.
(1) open/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 test purposes only. class IndexAction extends Action {public function index () {header ("Content-Type: text/html; charset = utf-8 "); echo"

^_^ Hello, welcome to ThinkPHP

";}}?>

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

(2) We do not care about him for the moment. First, we need a form submission page. Open "/test/tpl/default/index/folder, and create a new file named add.html.

 

After saving, enter http: // localhost/test/index. php/index/add to view the new page. Here,/index. php/Article (url should be capitalized) is converted to the corresponding address/test/index. php/Index /.
The relationship between the template and action is briefly described here. For each action, the corresponding template is an html file with the same name. For example, if the index () under the index class corresponds to default/index/index.html, and add.html, it obviously corresponds to add () under the index class ().
We can access the add.html template in the form of "add ()" (http: // localhost/test/index. php/index/add.html) without the corresponding add () action. The placeholders in the add.html template are replaced with the corresponding data. The effect is as follows.

(3) "action =/index. php/Article/insert, we can see that the action for form processing is/test/index. php/index/insert, so we need to add the insert action to process the data submitted by the form. Before that, we have another important thing to do: to add a model file. Through the creation of the model file, we will be able to use convenient methods in the insert action to operate the database.
Open the/test/lib/model/folder, create the file TestModel. class. php, open it, enter and save the following code.

<?phpclass TestModel extends Model {}?>

Simply put, this is the basic file implemented by ActiveRecord. The naming rule is to add Model to the table in your database. for example, the table we want to use is test, and my file name must be TestModel. class. php, and the class name in the file must be TestModel.

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

Class IndexAction extends Action {// add form data to database public function insert () {// instantiate the testmodel we just created. $ test = D ('test'); if ($ Test-> create () {// Save the form data in this step. Thinkphp is all done. $ Test-> add (); $ this-> redirect ();} else {exit ($ test-> getError (). '[Back to]') ;}}

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

Public function index () {// still instantiate the model of the corresponding table name that we created. this is an important key for quick table operations. $ Test = D ('test'); // Are you familiar with this code? Calculate all rows $ count = $ test-> count ('', 'id'); // The number of rows displayed on each page $ listRows = '3 '; // query the fields $ fields = 'id, name, content'; // import the paging class/ThinkPHP/lib/ORG/Util/Page. class. phpimport ("ORG. util. page "); // use the constructor of the class to change the page parameter. $ Count is the total number, and $ listrows is the display entry for each page. $ P = new Page ($ count, $ listRows); // you can specify the query parameters. For details, see "ThinkPHP/Lib/Think/Core/Model. class. php" row 1731. $ 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 ();}

We should set a template. Create index.html under/test/tpl/default/index/(because index () is used by default (). Therefore, you can directly use assign in the program without specifying the template file. Of course, this can be configured .)

Enter // display by page. this row is displayed on {$ page} // data. The following parameters will be explained soon. It is easy to understand.
 
  

Name: {$ vo. name}

Content: {$ vo. content}

Save it. Enter http: // localhost/test/
Congratulations. You have learned how to use thinkphp to create pages!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.