Data paging in thinkphp instance tutorial

Source: Internet
Author: User
Data paging in thinkphp instance tutorial

  1. Create table 'test '(
  2. 'Id' int (10) unsigned not null auto_increment,
  3. 'Name' char (100) not null,
  4. 'Content' varchar (300) not null,
  5. Primary key ('id ')
  6. ) Engine = myisam default charset = utf8 auto_increment = 27;
  7. Insert into 'test' ('id', 'name', 'Content') values
  8. (19, '123', '123 '),
  9. (20, '123', '123 '),
  10. (21, '123', '123 '),
  11. (26, '24', '123 '),
  12. (25, '123', '123 '),
  13. (24, 'age', 'age '),
  14. (23, '123', '123 '),
  15. (22, '123', '123 ');

2. create a 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:

  1. // Define the thinkphp framework path
  2. Define ('think _ path', './thinkphp ');
  3. // Define the project name and path. These two statements are important.
  4. Define ('app _ name', 'test ');
  5. Define ('app _ path', './test ');
  6. // Load the framework entry file
  7. Require (think_path. "/thinkphp. php ");
  8. // Instantiate a website application instance
  9. $ App = new app ();
  10. // Application Initialization
  11. $ 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 the database connection.

  1. Return array (
  2. 'Db _ type' => 'mysql ',
  3. 'Db _ host' => 'localhost ',
  4. 'Db _ name' => 'test', // name of the newly created database, test
  5. 'Db _ user' => 'root', // database username
  6. 'Db _ pwd' => '', // database password
  7. 'Db _ port' => '123 ',
  8. );
  9. ?>

To enable the debugging mode, add "debug_mode" => true to the array.

3. basic page input and output. 1) open/test/lib/action/indexaction. class. php and find:

  1. // This class is automatically generated by the system for testing purposes only
  2. Class indexaction extends action {
  3. Public function index (){
  4. Header ("content-type: text/html; charset = utf-8 ");
  5. Echo"

    ^_^ Hello, welcome to thinkphp

    ";
  6. }
  7. }
  8. ?>

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) temporarily ignore him. First, you need a page for form submission. 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, __url _ (url should be capitalized) is converted to the corresponding address/test/index. php/index/. The relationship between the template and action is briefly described. 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 ().

You 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. (Script school edit finishing bbs.it-home.org)

3) from form's "action =__ url _/insert", we can see that the form processing action 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 can use a convenient method in the insert action to operate the database. open the/test/lib/model/folder and create the file testmodel. class. php. open him, enter and save the following code

  1. Class testmodel extends model {
  2. }
  3. ?>

This is the basic file implemented by activerecord. The naming rule is to add model to the table in your database. For example, if the table to be used is test, the file name must be testmodel. class. php, and the class name in the file must be testmodel. then return to indexaction. class. php file, delete the original code, and add:

  1. Class indexaction extends action {
  2. // Add form data to the database
  3. Public function insert (){
  4. // Instantiate the testmodel we just created.
  5. $ Test = d ('test ');
  6. If ($ test-> create ()){
  7. // Save the form data. Thinkphp is all done.
  8. $ Test-> add ();
  9. $ This-> redirect ();
  10. } Else {
  11. Exit ($ test-> geterror (). '[Return]');
  12. }
  13. }
  14. }

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

  1. Public function index (){
  2. // The new model corresponding to the table name is still instantiated. this is an important key for quick table operations.
  3. $ Test = d ('test ');
  4. // Are you familiar with this code? Calculate all rows
  5. $ Count = $ test-> count ('', 'id ');
  6. // Number of lines displayed per page
  7. $ Listrows = '3 ';
  8. // Which fields need to be queried
  9. $ Fields = 'id, name, content ';
  10. // Import the paging class/thinkphp/lib/org/util/page. class. php
  11. Import ("org. util. page ");
  12. // Modify the page parameters through the constructor of the class. $ Count is the total number, and $ listrows is the display entry for each page.
  13. $ P = new page ($ count, $ listrows );
  14. // Set query parameters. For details, see "thinkphp/lib/think/core/model. class. php" row 1731.
  15. $ List = $ test-> findall ('', $ fields, 'id desc', $ p-> firstrow. ','. $ p-> listrows );
  16. // The paging class is ready.
  17. $ Page = $ p-> show ();
  18. // Template output
  19. $ This-> assign ('list', $ list );
  20. $ This-> assign ('page', $ page );
  21. $ This-> display ();
  22. }

Set a template and create index.html under/test/tpl/default/index/(because index () is used by default (). In the program, you can directly use assign instead of specifying the template file. Of course, this can be configured .)

  1. Enter
  2. // Display this line by page
  3. {$ Page}
  4. // Data display. The following parameters will be explained soon. It is easy to understand.
  5. Name: {$ vo. name}

  6. Content: {$ vo. content}

Save and enter http: // localhost/test/

The above is the method and example for making paging in thinkphp. I hope it will be helpful to you.

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.