In-depth analysis of PHP Infinitus classification case study, analysis of php case study _ PHP Tutorial

Source: Internet
Author: User
In-depth analysis of PHP Infinitus classification case tutorial, analysis of php case tutorial. In-depth analysis of the case study of PHP Infinitus classification, analysis of php case study in normal development is more or less inevitable will encounter the problem of Infinitus classification, because of efficiency, logic and other issues, we have been deeply analyzing the case study of PHP Infinitus classification, and analyzing the case study of php.

In normal development, the problem of Infinitus classification is more or less inevitable, because efficiency, logic and other problems have always made these problems more acute. Today, we will take the yii2 framework as the basis and the topic Infinitus as an example to give a simple solution to this problem.

First, we have a column data table tree.

Table structure, for example, (the original text has a diagram)

It seems that the table structure is very simple.

Insert several test data records

INSERT INTO `tree` (`id`, `parent_id`, `name`) VALUES (1, 0, 'A'), (2, 0, 'B'), (3, 1, 'a'), (4, 3, 'aa'), (5, 2, 'b'), (6, 4, 'aaa');

The tree structure is roughly as follows:

|
| --
| ---- Aa
| ------ Aaa
| B
| -- B

This is exactly the data structure we need. let's take a look at how to handle it to get the expected results.

We have also mentioned that yii2 is the basis, so our writing is also based on object-oriented rules.

Class tree {// Access index to view the tree structure public function actionIndex () {$ data = self: getTree (); // to facilitate testing, here we output \ Yii: $ app-> response-> format = \ yii \ web \ Response: FORMAT_JSON; return $ data;} in json format ;} // Retrieve The Tree public static function getTree () {// Here we get all the data directly, then, the most taboo in the Infinitus classification is to perform layer-by-layer operations on the database, which can easily cause memory overflow. // Finally, the computer crashes. result $ data = static :: find ()-> all (); return self: _ generateTree ($ data);} // Generate tree private static function _ generateTree ($ data, $ pid = 0) {$ tree = []; if ($ data & is_array ($ data) {foreach ($ data as $ v) {if ($ v ['parent _ id'] = $ pid) {$ tree [] = ['id' => $ v ['id'], 'name' => $ v ['name'], 'parent _ id' => $ v ['parent _ id'], 'Children '=> self :: _ generateTree ($ data, $ v ['id']),] ;}}return $ tree ;}}

Let's look at the tree/index below, as shown below:

In this way, we can see a clear tree structure, which is what we ultimately need.

The case study on PHP Infinitus classification will introduce so much to you and hope to help you!

In normal development, NLP will inevitably encounter the problem of Infinitus classification, because efficiency, logic, and other problems have always been...

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.