Thinkphp summary, thinkphp Summary
After using ThinkPHP for several projects, I feel that this framework is quite good and suitable for my logic habits. It is also quick to develop. I have summarized some of the frequently used projects, hope to help beginners TP!
Conventions:
For example:
Define controller class
Record modification:
Record Query
$ User-> getDbFields () // obtain the current data field
$ Model = new Model () // instantiate a model object without any data tables
$ Objrs = $ Model-> query ("select * from think_userwhere status = 1") // custom query
Template:
$ This-> assign ('name', $ value); // use the assign Method in the Action class to assign values to template variables. All variable types Use assign for assignment.
$ This-> display () // output Template File
Batch assignment
Template Tag:
{} Or {// comment content} // template comment
To facilitate template definition, you can use the following methods to output template variables in an array or an object:
Function:
System Variables
Quick output
Contains external files
Loop output
Template assignment:
Template definition:
Note the meaning of name and id
// Output an even number of records
// Output key
// Subloop output
Switch label
Compare tags
If tag
C operation
Operation
D operation
S operation
F Operation
(IN parameter1 INTEGER)
BEGIN
DECLARE variable1 CHAR (10 );
IF parameter1 = 17 THEN
SET variable1 = 'birds ';
ELSE
SET variable1 = 'beasts ';
End if;
Insert INTO table1 VALUES (variable1 );
END
ThinkPHP system constant
THINK_PATH // ThinkPHP system directory
CONFIG_PATH // project configuration file directory
MEMORY_LIMIT_ON // whether the memory usage limit exists
Predefined Constants
Upload Overview
Basic upload function
Batch upload
Note that the UploadFile upload class does not use Multifile upload.
Ajax File Upload
Automatically generate thumbnails
After setting, the system automatically generates thumbnails in the same format after uploading. The default thumbnail path is the directory where the uploaded file is located, and _ thumb is added to the file to identify the thumbnail file. You can configure the thumbnail path in the project configuration file.
Generate multiple thumbnails
In the preceding example, three thumbnails are generated, and the suffix added after the thumbnail file name and the width and height of the three thumbnails are specified.
More upload settings
ThinkPHP also provides an upload setting interface with the UploadFile class in the Action field to facilitate more parameter settings on the client for upload control.
The following lists the main parameters. For more parameters, refer to the _ upload method in the Action class of the framework.
// Sets the overwrite upload mode.
<Input type = "hidden" name = "_ uploadReplace" value = "1"> // sets the file TYPE that can be uploaded.
<Input type = "hidden" name = "_ uploadFileType" value = "jpg, gif, png, swf"> // upload the file storage directory and set the writable permission.
<Input type = "hidden" name = "_ uploadSavePath" value = "/Public/Images/user/"> // Upload File name naming rules. functions are supported, for example, the time uniqid com_create_guid system is set to uniqid by default to ensure that the uploaded file names are not duplicated. If no function is set, the rule string is used as the upload file name.
<Input type = "hidden" name = "_ uploadSaveRule" value = "time"> // set the size of the uploaded file
<Input type = "hidden" name = "_ uploadFileSize" value = "20480"> // sets the upload data table, <input type = "hidden" name = "_ uploadFileTable" value = "user"> // sets the data number of the uploaded file, you do not need to set it unless you need it.
<Input type = "hidden" name = "_ uploadRecordId" value = ""> // you do not need to set the upload user ID. The system automatically obtains the current logon user ID.
<Input type = "hidden" name = "_ uploadUserId" value = "{$ user. id}"> Source: http://blog.sina.com.cn/s/blog_813e149b01010o3z.html
How to redirect to other websites in thinkphp
5.15 redirection
The redirect method of the Action class can realize page redirection.
The parameter usage of the redirect method is the same as that of the U function (refer to the URL generation section above), for example:
The preceding usage is to jump to the News Module's category operation after 5 seconds, and display the page Jump text. After redirection, the current URL address is changed.
If you only want to redirect to a specified URL address, rather than the operation method to a module, you can directly use the redirect method to redirect, for example:
The first parameter of the Redirect method is a URL address.
5.14 page Jump
During application development, you will often encounter jump pages with prompts, such as pages with operation successes or operation errors, and automatically jump to another target page. The system Action class has two built-in jump Methods success and error, which are used for page Jump prompts and support ajax submission. The method is simple, for example:
Both the Success and error methods have corresponding templates and can be set. The default settings are as follows:
Template tags can be used in template files, and the following template variables can be used:
$ MsgTitle: Operation title
$ Message: Page prompt information
$ Status: Operation status 1 indicates success. 0 indicates failure. You can also define rules for the project.
$ WaitSecond: the unit of the jump wait time is seconds.
$ JumpUrl: Jump page address
The success and error Methods automatically determine whether the current request belongs to an Ajax request. If the request belongs to an Ajax request, the ajaxReturn method is called to return information. For details, refer to the AJAX return section.
These thinkphp3.0 manuals are available. You can download the manual to see them.
How to connect to the database in ThinkPHP [Switch]
Before connecting to the database to operate the database in ThinkPHP, we need to create a Model. Before talking about Model and Action, first explain the storage location of Model and Action. Model is saved in the lib/Model folder in the program directory, and Action is saved in the lib/Action folder in the program directory. The default Model rule of the ThinkPHP system is like this: the civilized name of the Model file is similar to "Model class name + Model. class. php, and the default Operating Database Table Name of the Model is in config. DB_PREFIX + Model class name defined in php. Model class name and file name must be capitalized. "In the Model file, define a class and extend the Model class, the general syntax is as follows: class name Model extends Model {}. Now let's define a Model. Because our database table name is cms_article, and the class ArticleModel extends Model {} file is saved as ArticleModel. class. php. No need to write anything. A Model has been defined. Now, let's continue with our knowledge about Action. Action and Model are very similar. The difference is that Action does not directly operate the database, but needs to operate the database through the Model. Now let's define an Action to complete the operation. Class IndexAction extends Action {function index () {$ Article = D ("Article") ;}} saves the file as IndexAction. class. php. OK. Now let's refresh the homepage. If there are no prompts, congratulations. The database connection Model and Action definitions are normal. In Action, the D method calls Model, and the Article method is the Model class in ArticleModel. class. php we just defined ~ That is to say, while defining the Model, we have completed the connection to the database and the quasi-standby operation to the database table ~