Learn to thinkphp set up a small instance, from the Internet to find an article, itself thinkphp is an instance of the official website, but always error, the following this article is also good, is the most basic, from the configuration file to the connection database, then to the production template, read the template, there is a new MySQL database , are more detailed, thinkphp the underlying core file unchanged, if just learning can refer to the study.
Test system: WIN7, XP
Test tool: Zend studio9
Operating platform: WAMP
Database: MySQL
TP Beta version: 3.0 (emphasis: 3.0 Previous versions are very different on configuration)
1. Configure the Portal file
Portal file: Under the same level directory as thinkphp
Name: index.php
index.php
- <?php
- Define (' Think_path ', './thinkphp/');
- Define (' App_path ', './web3.0/');
- Define (' App_name ', ' web3.0 ');
- Require Think_path. ' Thinkphp.php ';
- ?>
Run Result: See Welcome interface ^_^ Hello, welcome to use thinkphp both to configure Ingress success.
2. How to configure database linking
File path: drive letter: \ Server Path \tp3.0web\web3.0\conf
Open the config.php file and modify it to the following:
- <?php
- Return Array (
- //' config item ' = ' config value '
- ' Db_type ' = ' mysql ',
- ' db_host ' = ' localhost ',
- ' Db_name ' = ' MyApp ', //database named MyApp
- ' Db_user ' = ' root ',
- ' Db_pwd ' = ',
- ' Db_port ' = ' 3306 ',
- ' Db_prefix ' = ' think_ ',
- //Due to the simplest way of linking, the function is abbreviated
- );
- ?>
The database name is myapp,mysql in the following ways:
<1> Build a database
CREATE DATABASE ' MyApp ';
<2> Build data table (Think_form is both the name of the datasheet)
- CREATE TABLE ' think_form ' (
- ' ID ' smallint (4) unsigned not NULL auto_increment,
- ' title ' varchar (255) not NULL,
- ' Content ' varchar (255) is not NULL,
- ' Create_time ' int (one) unsigned not NULL,
- ' Update_time ' int (one) unsigned not NULL,
- ' Status ' tinyint (1) unsigned not NULL,
- ' Email ' varchar (not NULL) ,
- PRIMARY KEY (' id ')
- ) Engine=myisam DEFAULT Charset=utf8;
<3>: Inserting data
INSERT into ' think_form ' (' id ', ' title ', ' content ', ' create_time ', ' update_time ', ' status ', ' email ') VALUES
(1, ' This is test data ', ' dfdf ', 1212724876, 0, 1, ' [email protected] ')
3. Configuring the module file
File path: Automatically generated files after activating the portal file, where the file name is web3.0
EG: drive letter: \ Server Path \tp3.0web\web3.0\lib\action\indexaction.class.php
Open the IndexAction.class.php file and modify it to read as follows:
- <?php
- This class is automatically generated by the system and is intended for testing purposes only
- Class Indexaction extends Action {
- Public Function Index ()
- {
- $form = D (' form ')->findall (); //Recommended not to use the encapsulated database query method, the details will be updated later
- Dump ( $form);
- //$this->display ();
- }
- }
- ?>
4. Configure database query statement files
File path: drive letter: \ Server Path \tp3.0web\web3.0\lib\model
Create a template file under the file (I don't know if that's the right thing to say) FormModel.class.php
Drive character: \ server path \tp3.0web\web3.0\lib\model\formmodel.class.php
- <?php
- Class Formmodel extends Model
- {
- Public function FindAll ()
- {
- $sql = ' SELECT ' title ' from Think_form ';
- return $this->query ( $sql);
- }
- }
- ?>
Summarize:
Create two files: Portal file index.php and database template file FormModel.class.php
Modified two files: config.php and IndexAction.class.php
Finally Http://localhost/TP3.0WEb/index.php/index/index view the results.
Note: The database connection method only describes the method of configuration and implementation, and does not explain the method. Updated later in detail.
Append: Implement the MVC method to extract the database data and display it in the HTML page.
5. Create a data display template (partial template substitution in HTML)
File path: drive letter: \ Server Path \TP3.0WEB\WEB3.0\TPL
Create an index (I-uppercase) folder under the TPL directory, and then build a index.html (i-lowercase) file under the folder
Drive character: \ server path \tp3.0web\web3.0\tpl\index\index.html
Emphasis: In 2.0, you need to set up the default folder in the TPL directory and then set up the Index folder, and then set up the index.html in the Index folder to calculate the configuration is complete
In index.html, add the following:
-
-
- <meta http-equiv="Content-type" content= "text/html; Charset=utf-8 ">
- <title>{$title}</title>
-
- <body>
- {$title}
- </body>
-
The contents of {$title} can be seen replaced after running
6. Modify the contents of step 4 without changing the path.
File path: drive letter: \ Server Path \tp3.0web\web3.0\lib\model
Change the contents of the FormModel.class.php template file to read as follows:
- <?php
- This class is automatically generated by the system and is intended for testing purposes only
- Class Indexaction extends Action {
- Public Function Index ()
- {
- //$form = D (' form ')->findall (); Can be used both up and down
- $form = M (' form ')->select ();
- Dump ( $form);
- //$this->assign (' title ', $form [0]["title"]); Both methods are the same
- $this->title = $form [0]["title"];
- $this->display (); //To be at index. Display content in HTML must open display, and then replace the template with the corresponding name and path
- }
- }
- ?>
At this point, only the TP database operations that can be queried have been completed.
The most detailed and simplest thinkphp link database-TripAdvisor blog