Say the experience that oneself studies ~ first

It was a little difficult to learn at first, because it was the first time to learn the MVC framework. In simple terms, each Controller corresponds to a folder with the same name under View, and then you customize the methods in Controller, which correspond to the HTML file under View. The role of the Model is to manage the logic of the controller, make operations easier, and put in some useful function methods.

Say again experience

1. Query
  • The key to the query is a single wordwhereIs the condition of the query. In TP, there are two methods that are used a lot, one is string query, the other is array query (objects are used less, not necessary) 1. Let’s start with string queries
// Suppose we have a user table
$user = M('User');

// Query a record given id = 3
$result = $user->where('id = 3')->find();   // Note that a single record is queried using find, which returns a one-dimensional array.

$id = $id
$result = $user->where("id = '$id'")->find();  // Enclose the variable in single quotes (in case it is a string)

$id = [1, 2, 3];
$id = join(', ',$id);       // Change the array to string mode
$result = $user->where("id in (".$id.")")->select();    // Select ()

// Query id is array and status is 1.
$result = $user->where("id in (".$id.") AND status = 1")->select(); // it is similar except for "=
// Query statistics
$user->where("status = 1")->count(); // Count the number of records where status = 1

$id = $id; $id = $id
$user->where("id = $id")->setInc('view'.1); // Make the view field value +1 plus or minus "1" can default to the second argument
$user->where("id = $id")->setDec('view'.1);// Set view to -1
Copy the code
  1. Let’s talk about array queries
$user = M('User');
// Array query usually uses the corresponding field condition as the array key.
$map['id'] = ['lt'.100];
$user->where($map)->select(); // Query data whose ID is less than 100

// If you want to query multiple fields, you need to query multiple fields.
$map['status'] = 1;
$map['name'] = ['like'['%javascript%']];
$user->where($map)->select(); // Remember that the default is 'and' both conditions must be met

// Select * from 'or'
$map['status'] = 1;
$map['name'] = ['like'['%javascript%']];
$map['_logic'] = 'or';
$user->where($map)->select(); // If either of the two conditions is satisfied, or if either of the two conditions is satisfied, then the following is true

$id = [1, 2, 3]; $id = [1, 2, 3];
$map['id'] = ['in', $id];  /* or */ $map['id'] = ['in'.' '.join(",",$id).' ']; 
Copy the code

3. As long as the “where” condition is well understood, other delete, modify, add is the same. Here’s a nice little example.

So this is a fuzzy query, and it looks like it's up here, so let's just use arrays. So first of all, if you look at this picture, this is a form, method is get, and if you fill in both of them, then you have two conditionsANDIf only one is entered, then query separately, if not, query all.public function index(a) {
    $name = I('get.name');
    $phone = I('get.phone');  // Use the I method to get the passed data
// Then judge
    if($name && trim($name) ! =' ') {
        $map['name'] = ['like'.The '%'.$name.The '%'];  // Array fuzzy query
    }    
    if($phone && trim($phone) ! =' ') {
        $map['phone'] = ['like'.The '%'.$phone.The '%'];    
    } 
    Status = 1;
    $map['status'] = 1;
    $result = $user->where($map)->select();
}
Copy the code
  1. Another common mistake Thinkphp makes is to write <> (tags) in templates such as:
'Compare tag'
<select name="group_id" id="group">
                            <volist name="groups" id="group">
                                <option value="{{$group.id}}" <eq name="group.id"value="$customer.group_id">selected</eq> >
                                {{$group.name}} </option> </volist> </select> Note that in these built-in judgment tags name="group.id" || name="group['id']"These are the only two that you don't need'$', conversely, in value value =$customer.group_id || value = $customer['group_id'Don't forget'$'

'if tags'<select name="category_id" id="parentCategory" class="form-control">
                                <volist name="categories" id="category">
                                    <option value="{{$category.id}}"
                                    <if condition="$category['id'] eq $article['category_id']">selected</if> > First class classification: {{$category.name}}</option>
                                    <volist name="category.children" id="child">
                                        <option value="{{$child.id}}"
                                        <if condition="$child['id'] eq $article['category_id']">selected</if> > Secondary classification: {{$child.name}}</option> </volist> </volist> </select'$'Should be$X['xxx'] and$XThe problem is that the last one cannot be replaced. Try to use all of the results obtained above in the comparison label$X['xxx'], don't use$XNote also that name=""This one must not be added'$'
Copy the code

Write this much and update it later