thinkphp5 Realizes Infinite Classification

  • 2021-11-29 06:25:44
  • OfStack

tp5 can read and display the information of the classified data table either by the controller or in the corresponding model. In this case, we are completely successful in the model. Moreover, the operation of adding, deleting, modifying and checking data tables is the job of the model.

First of all, under Statement 1, we use recursive method to realize infinite classification. However, the realization of infinite classification is not only a recursive way, but also a full-path way, which can also be realized. However, this way is usually used in the full-path navigation menu. Therefore, we still use the most common recursive function here: infinite classification.

First of all, let's create a static method: getCate. Why do you want a static method? Because static methods are efficient and do not need instantiation. Moreover, classified queries are very frequently used operations, and many queries depend on the results of classified queries.

The current getCate method has three parameters:

The first is $pid, which is the id of the parent class, and the default is 0, indicating that it starts from the top level.

The second parameter is the returned query result, which we return in an array. Note that this variable is passed in by introduction.

In recursive functions, there are three ways to return results:

The first one is familiar to everyone, using the global variable $_ GLOBALS []. This method has side effects, so we don't use it;

The second way is also good, which uses static keyword in the function to declare a static variable, which can accumulate the recursive execution results every time and finally return, which is very easy to understand, but it occupies too many resources and has low execution efficiency;

The third is to use the way of referencing parameters to return results, which is also the current mainstream technology. Although it is difficult to understand, the execution efficiency is the highest. If we want to play, we will play big, so here, we will directly use references to return the query results.

The third parameter, which can be absent, is used to set the hierarchical display between categories under 1, which can make the result look more intuitive and clear.

namespace app\admin\model;
use think\Collection;
use think\Model;
class Category extends Model
 // Create 1 Static methods getCate, To obtain classification information 
  * @param int $pid:  The parent of the current classification id
  * @param array $result: Reference return value 
  * @param int $blank: Set display prompts between categories 
 public static function getCate($pid=0, &$result=[], $blank=0)
  //1. Classification table query :$pid
  $res = self::all(['pid'=>$pid]);
  //2. Customize the prompt information before the category name 
  $blank += 2;
  //3. Traversal classification table 
  foreach ($res as $key => $value) {
   //3-1 Customize the display format of category names 
   $cate_name = '|--'.$value->cate_name;
   $value->cate_name = str_repeat('&nbsp;',$blank).$cate_name;
   //3-2 Save the queried current record to the result $result Medium 
   $result[] = $value;
   //3-3 Key : Set the currently recorded id As the following 1 Parent of level classification id,$pid Continuing to call this method recursively 
   self::getCate($value->id, $result, $blank);
  //4. Return query results , Call the result set class make Method to package the current results , Convert to 2 Dimensional array return 
  return Collection::make($result)->toArray();

Calling code in controller:

public function edit(Request $request)
 //1. Get 1 Subclassification id
 $cate_id = $request -> param('id');
 //2. Query the data to be updated 
 $cate_now = CategoryModel::get($cate_id);
 //3. Query all classification information recursively 
 $cate_level = CategoryModel::getCate();
 //4. Template assignment 
 $this -> view -> assign('cate_now', $cate_now);
 $this -> view -> assign('cate_level', $cate_level);
 //5. Render template 
 return $this -> view -> fetch('category_edit');

Related articles: