thinkphp Example Tutorial Data paging

Source: Internet
Author: User
    1. CREATE TABLE ' Test ' (
    2. ' id ' int (ten) unsigned NOT NULL auto_increment,
    3. ' Name ' char (+) is not NULL,
    4. ' content ' varchar (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, ' 1231 ', ' 123123123 '),
    10. (21, ' 123123 ', ' 123123123 '),
    11. (26, ' 24 ', ' 123123 '),
    12. (25, ' 321123 ', ' 321123 '),
    13. ("Age", "age"),
    14. (23, ' 123123 ', ' 123123 '),
    15. (22, ' 213 ', ' 123 ');
Copy Code

2, create a new thinkphp project. The new TP has built-in project auto-generated directory functionality. Create a new test folder under Htdocs (that is, your Web site root), put the thinkphp Core folder in the test root directory, and create a new file index.php in the test root directory, adding the following code:

    1. Defining thinkphp Frame paths
    2. Define (' Think_path ', './thinkphp ');
    3. Define the project name and path. These 2 sentences are the focus.
    4. Define (' app_name ', ' test ');
    5. Define (' App_path ', './test ');
    6. Loading Frame Entry File
    7. Require (Think_path. " /thinkphp.php ");
    8. Instantiate an instance of a Web site application
    9. $app = new app ();
    10. Application initialization
    11. $app->run ();
Copy Code

Run "http://localhost/test/index.php". You will see the Welcome page of thinkphp. Then open your test directory to see, found in the root of a test folder, at this time, your project directory has been generated.

Open the/test/test/conf/directory, create a new "config.php", and configure the database connection.

    1. Return Array (
    2. ' Db_type ' = ' mysql ',
    3. ' db_host ' = ' localhost ',
    4. ' db_name ' = ' test ',//New database name test
    5. ' Db_user ' = ' root ',//database user name
    6. ' Db_pwd ' = ', '//Database password
    7. ' Db_port ' = ' 3306 ',
    8. );
    9. ?>
Copy Code

If you want to turn on debug mode, add: "Debug_mode" to the array =>true

3, the implementation of basic page input and output. 1) Open/test/test/lib/action/indexaction.class.php, find:

    1. This class is automatically generated by the system and is intended 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 use thinkphp";
    6. }
    7. }
    8. ?>
Copy Code

The index () function in the Indexaction class that is automatically generated by the system is the default first page call function. You can use http://localhost/test/index.php or Http://localhost/test/index.php/index to visit him.

2) temporarily regardless of him. First, a form submission page is required. Open "/test/test/tpl/default/index/" and create a new file add.html.

Copy Code

Once saved, enter Http://localhost/test/index.php/index/add and you will be able to see your new page. where __url__ (the URL to capitalize) is converted to the corresponding address/test/index.php/index/. Here's a brief look at the relationship between the template and the action. Each action, the corresponding template is the same name as the HTML file. For example, index () under the Index class, corresponding to default/index/index.html, and add.html, is obviously corresponding to the index class of Add ().

You can access the add.html template in the form of Access Add (Http://localhost/test/index.php/index/add) in the case of only add.html without the corresponding add () action. Placeholders under the add.html template are replaced with the corresponding data. (Script Academy editing and finishing bbs.it-home.org)

3) from the "Action=__url__/insert" in the form, we can see that the action of the form processing is/test/index.php/index/insert, so we have to add insert action to process the form submission data. Until then, we have one more important thing to do, which is to add the model file. Through the creation of the model file, we will be able to use the Insert action in a convenient way to manipulate the database open the/test/test/lib/model/folder, new file testmodel.class.php. Open him, enter and save the following code

    1. Class Testmodel extends Model {
    2. }
    3. ?>
Copy Code

This is the basic file that ActiveRecord implements. A naming convention is a table in your database that is appended to the model. For example, the table that will be used is test, the file name must be testmodel.class.php, and the class name under the file must be Testmodel. Then, go back to the indexaction.class.php file, delete the original code, and add:

    1. Class Indexaction extends action{
    2. Adding form data to a database
    3. Public Function Insert () {
    4. Instantiate the Testmodel we just built.
    5. $test = d (' Test ');
    6. if ($test->create ()) {
    7. Save the form data in this step. The thinkphp have all been done.
    8. $test->add ();
    9. $this->redirect ();
    10. }else{
    11. Exit ($test->geterror (). ' [Return] ');
    12. }
    13. }
    14. }
Copy Code

4) Next, you need to add a home page default display action index () in the Indexaction class to invoke the form data.

    1. Public Function index () {
    2. It is still the model that we created for the corresponding table name. This is the key to our quick table operation.
    3. $test = d (' Test ');
    4. Are you familiar with this piece of code? Count All Rows
    5. $count = $test->count (' ', ' id ');
    6. Number of lines displayed per page
    7. $listrows = ' 3 ';
    8. Which fields to query
    9. $fields = ' id,name,content ';
    10. Import a paging class/thinkphp/lib/org/util/page.class.php
    11. Import ("Org.util.page");
    12. The parameters of the page are changed by the constructor of the class. $count is the total, $listrows the display entry for each page.
    13. $p = new Page ($count, $listrows);
    14. Set the query parameters. See "thinkphp/lib/think/core/model.class.php" for more than 1731 lines.
    15. $list = $test->findall (', $fields, ' id desc ', $p->firstrow. ', '. $p->listrows);
    16. The paging class is done.
    17. $page = $p->show ();
    18. Template output
    19. $this->assign (' list ', $list);
    20. $this->assign (' page ', $page);
    21. $this->display ();
    22. }
Copy Code

Set up a template to create a new index.html under/test/test/tpl/default/index/(because the default corresponds to index (). The program can be directly assign. Instead of specifying a template file. Of course, this can be configured. )

    1. Fill
    2. Pagination shows that this line
    3. {$page}
    4. Data display. The following parameters will be explained shortly. It is well understood.
    5. Name: {$vo. Name}

    6. Contents: {$vo. Content}

Copy Code

Save, enter http://localhost/test/

The above is thinkphp to make pagination method and examples, I hope to help 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.