Php unlimited classification learning reference: detailed explanation of ecshop unlimited Classification

Source: Internet
Author: User
After carefully analyzing the infinite classification of ecshop, we found that it is not difficult to understand its algorithms. There are examples to help you understand them.

After carefully analyzing the infinite classification of ecshop, we found that it is not difficult to understand its algorithms. There are examples to help you understand them.

The Code is as follows:
Function cat_options ($ spec_cat_id, $ arr)
{
Static $ cat_options = array ();
If (isset ($ cat_options [$ spec_cat_id])
{
Return $ cat_options [$ spec_cat_id];
}
/*
Initialize key parameters:
$ Level: depth of the current subnode
$ Last_cat_id: ID of the current parent node
$ Options: array with indentation level
$ Cat_id_array: enters the parent node along the same path in sequence
$ Level_array: depth of the subnode of the node.
*/
If (! Isset ($ cat_options [0])
{
$ Level = $ last_cat_id = 0;
$ Options = $ cat_id_array = $ level_array = array ();
While (! Empty ($ arr) // continue traversal if the node to be constructed exists
{
Foreach ($ arr AS $ key => $ value)
{
$ Cat_id = $ value ['cat _ id'];
// Level 1 Classification Node
If ($ level = 0 & $ last_cat_id = 0)
{
If ($ value ['parent _ id']> 0)
{
Break;
}
$ Options [$ cat_id] = $ value;
$ Options [$ cat_id] ['level'] = $ level;
$ Options [$ cat_id] ['id'] = $ cat_id;
$ Options [$ cat_id] ['name'] = $ value ['cat _ name'];
// If the traversal is completed, the traversal is no longer performed.
Unset ($ arr [$ key]);
If ($ value ['has _ children '] = 0)
{
Continue;
}
$ Last_cat_id = $ cat_id; // parent node of the lower Node
$ Cat_id_array = array ($ cat_id );
$ Level_array [$ last_cat_id] = ++ $ level;
Continue;
}
// The parent node ID of the current node is equal to its parent node ID.
If ($ value ['parent _ id'] = $ last_cat_id)
{
$ Options [$ cat_id] = $ value;
$ Options [$ cat_id] ['level'] = $ level;
$ Options [$ cat_id] ['id'] = $ cat_id;
$ Options [$ cat_id] ['name'] = $ value ['cat _ name'];
Unset ($ arr [$ key]); // After traversal, The traversal is no longer performed.
// If the current node has children, the current node will be stationed, but will not be traversed; otherwise, it will not be traversed
If ($ value ['has _ children ']> 0)
{
If (end ($ cat_id_array )! = $ Last_cat_id)
{
$ Cat_id_array [] = $ last_cat_id;
}
$ Last_cat_id = $ cat_id; // when the current node is used as the new parent node of the next level node
$ Cat_id_array [] = $ cat_id; // enter

$ Level_array [$ last_cat_id] = ++ $ level; // depth of the next level of the current node
}

}
Elseif ($ value ['parent _ id']> $ last_cat_id)
{// If the parent depth of the current node is greater than the depth of the current parent node, perform the next cycle
Break;
}
} // Endforeach
$ Count = count ($ cat_id_array );
If ($ count> 1)
{
// Retrieve the last Father's Day as the current Father's Day
$ Last_cat_id = array_pop ($ cat_id_array );
}
Elseif ($ count = 1)
{
If ($ last_cat_id! = End ($ cat_id_array ))
{
// When the parent node has only one entry, it is not taken out as the current Father's Day.
$ Last_cat_id = end ($ cat_id_array );
}
Else
{// Otherwise, the last parent node retrieved must be a level-1 Classification node.
$ Level = 0;
$ Last_cat_id = 0;
$ Cat_id_array = array ();
Continue;
}
}

If ($ last_cat_id & isset ($ level_array [$ last_cat_id])
{
// Retrieve the depth of the current node
$ Level = $ level_array [$ last_cat_id];
}
Else
{
$ Level = 0;
}
} // End while, now the non-recursive pre-order traversal of the constructor tree has been completed, where $ options has saved all nodes starting from the root node with an array of layered nature
$ Cat_options [0] = $ options;
}
Else
{
$ Options = $ cat_options [0];
}
// If the entire tree is retrieved from 0, it will be returned and no longer processed.
If (! $ Spec_cat_id)
{
Return $ options;
}
// Otherwise, it will start to intercept from the specified node. I 'd like to explain it a little bit, just say a few parameter meanings.
/*
$ Spec_cat_id_level: depth of the truncated Node
$ Spec_cat_id_array: A product classification tree with this node as the root node returned
The final returned array is sorted in this order: the size of the parent node, the size of the parent node, and the traversal of the first root node like the same parent node. For example:
Level 1 nodes have Level 2 nodes have and 7 Level 3 nodes have. If the direct child of 1 is and the direct child of 2 is
If the number of children in 5 is 7, the final array is 1-> 2-> 8-> 9-> 6-> 5-> 7.
*/
Else
{
If (empty ($ options [$ spec_cat_id])
{
Return array ();
}
$ Spec_cat_id_level = $ options [$ spec_cat_id] ['level'];

Foreach ($ options AS $ key => $ value)
{
If ($ key! = $ Spec_cat_id)
{
Unset ($ options [$ key]);
}
Else
{
Break;
}
}
$ Spec_cat_id_array = array ();
Foreach ($ options AS $ key => $ value)
{
If ($ spec_cat_id_level = $ value ['level'] & $ value ['cat _ id']! = $ Spec_cat_id) |
($ Spec_cat_id_level> $ value ['level'])
{
Break;
}
Else
{
$ Spec_cat_id_array [$ key] = $ value;
}
}
$ Cat_options [$ spec_cat_id] = $ spec_cat_id_array;
Return $ spec_cat_id_array;
}
}

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.