Thinkphp FAQ and usage summary & nbsp; & lt; spanstyle = "color: # ff0000;" & gt; ================================ ?? Conclusion ?? =========================& Lt;/span thinkphp FAQs and usage summary
================================ ?? Conclusion ?? ========================
?
Thinkphp bug and tips:
1. tags not available in the template
{$ Content} {$ I}
2. if Tag
For example:
There are always unexpected errors after the test, so it is better to use php directly.
If (...) {……?> Here we are.
Conventions:
1. all class library files must use. class. php as the file suffix, and the class name and file name must be consistent.
2. the class name of the controller is suffixed with action.
3. the model class name is suffixed with model. The first letter of the class name must be capitalized.
4. all database table names are in lower case,
For example:
Data table name: Prefix _ table name
Model class name: table name model note: The first letter of the table name must be capitalized.
Create object: d ('Table name') Note: The first letter of the table name must be capitalized.
Define controller class
Class indexaction extends action {
Public function show (){
Echo 'This is the new show operation ';
}
}
Enter
Http: // localhost/myapp/index. php/index/show/
Define model class:
Class table name model extends model {
[// Manually define a field [optional]
Protected $ fields = array (
'Id ',
'Username ',
'Email ',
'Age ',
'_ PK' => 'id', // primary key
'_ Autoinc' => true // whether to auto-increment
)
]
}
Record modification:
$ User = d ("user") // instantiate the user object
$ User-> find (1) // search for records with id 1
$ User-> name = 'thinkphp' // modify the name field of the queried record to thinkphp.
$ User-> save () // save the modified data
Update the value of a specific field
$ User-> setfield ('name', 'topthink', 'id = 1 ′)
Field operations are also supported.
$ User-> setfield ('score ',' (score + 1) ', 'id = 1 ′)
Create record, Method 1:
$ User = new usermodel () // instantiate the user object
$ User-> Field name = field value // assign a value to the field
$ User-> add () // add record
Create record, Method 2:
$ Data ['Field name'] = field value; // assign a value to the field
$ User = d ('user'); // instantiate the user object
$ User-> add ($ data); // $ insertid. The return value of the add method is the primary key value of the latest operator, which can be directly retrieved.
Add multiple records:
$ User = new usermodel ()
$ Data [0] ['name'] = 'thinkphp'
$ Data [0] ['email '] =
$ Data [1] ['name'] = 'current year'
$ Data [1] ['email '] =
$ User> addall ($ data)
Delete record
$ User-> find (2)
$ User-> delete () // delete the found record
$ User-> delete ('5, 6') // delete data with the primary key of 5 and 6
$ User-> deleteall () // delete all queried data
Record query
$ User-> getdbfields () // Obtain the current data field
$ User-> findall (); // query all records
$ User-> findall ('1, 3, 8 ') // query record sets whose primary key is 1, 3, and 8.
$ User-> count () // number of retrieved records
$ User-> max ('score ') // obtain the maximum credits of a user
$ User-> min ('score ', 'Score> 0') // obtain the minimum credits of a user whose points are greater than 0.
$ User-> avg ('field name') // Obtain the average value of the field values of all records
$ User-> sum ('field name') // calculates the field value.
$ User-> getn (2, 'Score> 80', 'Score desc') // returns the 2nd matching records.
$ User-> getn (2, 'Score> 80', 'Score desc') // you can obtain the last second record.
$ User-> first ('score> 80', 'Score desc') // to query the first record, you can also use
$ User-> last ('score> 80', 'Score desc') // Obtain the last record
$ User-> top (5, ", 'Score desc') // top 5 records with the highest points
$ User-> getby ('name', 'liu21st') // query the field value of the data field
$ Model = new model () // instantiate a model object without any data tables
$ Model-> query ("select * from think_user where status = 1 ″)
$ Objrs = $ model-> query ("select * from think_user where status = 1") // custom query
$ Model-> execute ("update think_user set name = 'thinkphp' where status = 1") // SQL operation used to update and write data, returns the number of affected records
$ User-> starttrans () // start the transaction
$ User-> commit () // submit the transaction
$ User-> rollback () // transaction rollback
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
$ Array ['name'] = 'thinkphp'
$ Array ['email '] =
$ Array ['phone'] ="
$ This-> assign ($ array)
$ This-> display () // call the read operation template of the user module
$ This-> display ('Edit') // call the edit operation template of the user module.
$ This-> display ('Member: read') // call the read operation template of the member module.
$ This-> display () // call the edit operation template of the user module of the xp topic
$ This-> display ('../member/read.html') // specify the full name of the template file.
Template tag:
{} Or {// comment content} // template comment
{$ User ['name']} // output array variable
{$ User: name} // attributes of the output object
To facilitate template definition, you can use the following methods to output Template variables in an array or an object:
{$ User. name}
For multi-dimensional array or multi-layer object attribute output, use the following definition method:
{$ User ['sub'] ['name']}
{$ User: sub: name}
Function:
Format: {$ varname | function1 | function2 = arg1, arg2 ,###}
Note:
{There cannot be spaces between the symbol and the $ symbol, so there is no problem with the space of the following parameter.
### Parameter location of template variables
Fine-grained variable
{$ Think. server. script_name} // get the $ _ server variable.
{$ Think. session. session_id | md5} // hunt for the $ _ session variable
{$ Think. get. pagenumber} // returns the $ _ get variable.
{$ Think. cookie. name} // retrieves the $ _ cookie variable
System constant
{$ Think. const. _ file __}
{$ Think. const. module_name}
Special variables, constants defined by the thinkphp system
{$ Think. version} // version
{$ Think. now} // current time
Blockchain output
{: Function (...)} // Execute the method and output the returned value
{~ Function} // the execution method is not output
} // Output the session variable
{& Var} // output configuration parameters
{% Var} // output language variable
{. Var} // output get variable
{^ Var} // output the post variable
{* Var} // output constant
Contains External files
// Use variables to control the template to be imported
// Use a final file name to include
Loop output
Iterate also has other aliases, including volist, resultset, and sublist.
Template assignment:
$ User = d ('user ')
$ List = $ user-> findall ()
$ This-> assign ('list', $ list)
Template definition:
{$ Vo. name}
Note the meaning of name and id
// Output 5th ~ of list ~ 15 records
{$ Vo. name}
// Output an even number of records
{$ Vo. name}
// Output key
{$ K}. {$ vo. name}
// Subloop output
{$ Sub. name}
Switch label
Value1
Value2
Default
The name attribute can use functions and fine-grained variables, for example:
Admin
Default
You can also use variables for the value attribute of case, for example:
Admin
Member
Default
In thinkphp, the use of the fckeditor editor has been plagued by tp's search for a useful online editor some time ago. after several attempts and modifications, the editor was finally successfully integrated into thinkphp. In addition, you can use both direct upload and ajax to process the content. To give some inspiration and help to colleagues who have not successfully integrated fckeditor, I would like to describe my experience and experience as follows:
Purpose: to integrate the fckeditor into thinkphp so that users can process the text and images to be published online, just like editing a word.
Application software and environment: apache Server Version 2.0 or later, php version 5.0 or later, mysql5.0 or later, thinkphp version 1.5 or later, and fckeditor version 2.x.
Procedure:
1. download fckeditor2.x and copy the decompressed folder fckeditor to the vender directory under the thinkphp folder to comply with the thinkphp third-party class library import rules.
2. modify parameters:
First, use editplus and other software to open the fckeditor_php5.php file under the fckeditor directory and find row 130th. The content is as follows:
Public function _ construct ($ instancename)
{$ This-> instancename
= $ Instancename;
$ This-> basepath = '?? ';
$ This-> width
= '000000 ′;
$ This-> height
= '000000 ′;
$ This-> toolbarset
= 'Default ';
$ This-> value
= ?? '? ';
$ This-> config
= Array ();
}
Public function _ construct ($ instancename)
{
$ This-> instancename
= $ Instancename;
$ This-> basepath
= '/Bm/thinkphp/vendor/fckeditor /';
$ This-> width
= '000000 ′;
$ This-> height
= '000000 ′;
$ This-> toolbarset
= 'Default ';
$ This-> value
= ";
The most important thing is to set the basepath. The path of the fckeditor_php5.php file to the document root directory of the website. In fact, this is the path used to characterize the relative root directory of the fckeditor editor. For example, the root directory of the file on the server is htdocs/or www/, the project name is project, and the project folder contains the thinkphp system file package and the project file package myapp. If the server does not set a virtual host for the project, the root directory of the file is still www/or htdocs /, it should be $ this-> basepath = '/project/thinphp/vendor/fckeditor /';
If a virtual host is set, the project is changed to the virtual document root directory, and a website project can be directly accessed through a domain name, at this time,
$ This-> basepath = '/thinphp/vendor/fckeditor /';
Other parameters, such as width and height, can be filled or left blank. If this parameter is specified, it is the default height and width of the editor. Instancename is the id and name of the tag of the editor. Ignore other parameters.
Next, find the config. php file under fckeditor \ editor \ filemanager \ connectors \ php \, open it, and find lines 30 and 34. The parameters to be rewritten are as follows: $ config ['enabled'] = true;
$ Config ['userfilespath'] = '??? ';
The first parameter should be set to true. the default value is true. The second parameter is the path of the uploaded file, such as the image to be displayed. Create an uploads folder under the project Directory. then $ config ['userfilespath'] = '/project/uploads /?? '; The path rule is the same as the previous basepath. If the project is the root directory of the virtual file, $ config ['userfilespath'] = '/uploads /?? ';
3. applications:
For example, indexaction in the lib Directory of the myapp project. class. in the index method of the php controller class, when accessing the program, the output template webpage contains a form, requiring the user to enter an article, then the editor can be used. the code example is as follows. only the code related to fckeditor is displayed. other codes are omitted.
The first is the server program:
Public function index ()
{...... // Other code
Vendor ("fckeditor. fckeditor"); // contains the fckeditor class library. The system method for tp to introduce a third-party class library is relative to the vendor directory.
$ Editor = new fckeditor ();?? // Instantiate the fckeditor object
$ Editor-> width = '000000'; // you can specify the actual width of the editor. If this option is omitted, the default width is used.
$ Editor-> height = '000000'; // you can specify the height required by the editor. If this parameter is omitted, the default height is used.
$ This-> value = "; // you can specify the initial value of the editor. It can also be the set value when the data is modified. You can leave it empty.
$ Editor-> instancename = 'comment'; // Set the id and name of the input tag in the form in which the editor is located, that is $ Html = $ editor-> createhtml (); // create an online editor html code string and assign it to the string variable $ html.
$ This-> assign ('html', $ html); // assign the $ html value to the template variable $ html. you can directly reference it in the template through {$ html.
....... // Other code, including the output template.
}
The second is the corresponding html template, that is, the index file. You only need to insert the editor where necessary. other code and general
Now, you can use it. In a form processing program, you can do as you normally process form elements. However, sometimes, after the project is transplanted, the uploaded images and other link paths will be incorrectly compiled, so that they cannot be correctly displayed. It is usually a double quotation mark parsing error. I have not solved it yet. To avoid errors, you can use ajax to process form data. However, before ajax processing, you must first use a piece of js code to assign the value in the editor to the form element in the form where name is the value of instacename. For example, in this example, if ajax is used to process the form, the following javascript code must be run in the index template file before the form processing:
....... // Other js code
Var? Editor = fckeditorapi. getinstance ('Comment'); // comment is the set instancename value.
Document. commentform. comment. value = editor. editordocument. body. innerhtml; // the source code after the content in the editor is processed ?????????????????????????????? ???????????????????????????????????????? ???????????????????????????????????????? // Assign the value to the comment attribute element of the commentform form.
...... // Other js code
Note: I personally think that xajax is quite good. I only need to focus on the background program. I also use xajax for data processing. The front-end code is very simple.
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