Some common practical skills in thinkphp programming

Source: Internet
Author: User
Tags add time

There are many convenient ways to integrate in thinkphp programming. For the programmer just contact, as long as the master and flexible use, then you can achieve a multiplier effect, the following for everyone to explain in detail.

1. GetField

The convenience of GetField is that you can get a value, an array, or a set of key-value pairs.

The next three examples are explained briefly.

A. Gets a value.

In the method of changing the password, verify that the old password matches. The user ID is now known as $uid.

Then $old_pass = $Model->where ("uid= ' $uid '")->getfield (' Password ');

You can obtain the old password to verify it.

B. Get an array

If there is a class two classification under the first level category, a first class classification is now given, which requires searching for all products under the first class classification.

The solution is to first obtain all the two level classification ID, this time with GetField is very convenient.

$arr _types = $Model->where ("pid= ' $type _id '")->getfield (' id ', true);

$map [' type_id '] = Array (' In ', $arr _types);

$products = M (' product ')->where ($map)->select ();

This will get all the products under this level classification, this method is limited to level two classification, if there are three levels of classification, please use other methods to solve.

C. Obtaining a set of key-value pairs

In view of the limited level, currently mainly used in the information display of related queries, the database can be reduced to a certain extent.

For example, to show a user's shopping cart, but only the product ID can be found from the cart table, but I want to show the name of the product. This can be solved by getting a set of key-value pairs from the product table.

$arr _product = $Model->getfield (' Id,product_name ', true);

When the output, the name of the corresponding commodity is $arr_product[$product _id];

Of course, the use of joins can also solve this problem.

2. Page

As the name implies, this is a thinkphp package good paging class, it is very convenient to use. Different versions use different, detailed usage can be viewed in manual. Here is the ultimate solution. No matter which version, even the native PHP, can solve the paging problem.

Suppose the result to be paged is $product_list (data type array).

Current page: $page = I (' page ')? I (' page '): 1;

Number of messages per page: $per _page = 10;

Get paged results: $product _list = Array_slice ($product _list, ($page-1) * $per _page, $per _page);

Array_slice parameter: array name, starting position (starting at 0), number of splits.

3. SetField

Sometimes you need to change a field in a set of data or a class of data, which is a bit overqualified for the Save method.

For example, I just need to modify a state value to change the status value of one or a class to 1, indicating that it is available.

$Model->where ($map)->setfield (' status ', 1);

What if I want to keep a record of the change time as well.

$data [' status '] = 1;

$data [' edittime '] = time ();

$Model->where ($map)->setfield ($data);

SetField can modify a field, or you can modify multiple fields.

4. Setinc, Setdec

Setinc, commonly used to count reading volume hits. Each time you request an article, you only need to:

$Model->where ("id= ' $id '")->setinc (' Readnum ');

The default is increased by 1 and can also be customized. Detailed usage can be seen in the manual.

Setdec uses the same.

5. Background with tips on adding and editing a class of controllers

Do background development know, add a piece of data need two controller, one to display the page, another to operate, in fact, two controllers can be merged. Now added as an example:

General wording:

Public Function addproduct () {

$this->display ();

}

Public Function do_addproduct () {

$Model = M (' product ');

$data = Array (

' name ' + = I (' name '),

' Price ' = I (' price '),

......

);

$result = $Model->add ($data);

If ($result) {

Success (' Add Success ', U (' product_list '));

}else{

Error (' Add failed ');

}

}

The writing of a merge:

Public Function addproduct () {

If (is_post) {

$Model = M (' product ');

$data = Array (

' name ' + = I (' name '),

' Price ' = I (' price '),

......

);

$result = $Model->add ($data);

If ($result) {

Success (' Add Success ', U (' product_list '));

}else{

Error (' Add failed ');

}

}else{

$this->display ();

}

}

The above is the addition of a class of controller, followed by the same way of thinking, showing the modification of the controller's wording.

Public Function editproduct () {

$id = I (' id ');

$Model = M (' product ');

If (is_post) {

$data = Array (

' id ' = $id,

' name ' + = I (' name '),

' Price ' = I (' price '),

......

' Addtime ' =>time ()

);

$result = $Model->save (' data ');

If ($result) {

Success (' modified success ', U (' product_list '));

}else{

Error (' Add failed ', U (' editproduct ', array (' id ' = = $id));

}

}else{

$this->info = $Model->find ($id);

$this->display ();

}

}

Finally, you need to explain some notes to programmers:

A. When the page is displayed, the ID is presented in Input[hidden] form, and it will be easy to write when submitted.

B. If there is a primary key in the $data array, the direct Save method is available.

C. In order to avoid any changes that result in the failure of the returned result, add time to resolve.

One of the most important benefits of a one-way approach is to write in the template

Forms, action= "/admin/technology/edit/id/507.html" can be, very convenient. Well, to this place for everyone to finish, if there is still no understanding of the place, then you can ask for help.

This article by the Professional Zhengzhou app development company Brigitte Xuan Science and Technology finishing release, original is not easy, if need reprint please indicate source!

Some common practical skills in thinkphp programming

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.