Unlimited classification and tree node output

Source: Internet
Author: User
Unlimited classification and tree node output
  • $ StrRe ='
  • ';
    Input simple raw data to obtain N relationships between nodes and output tree-like DOM
    Note: Please refer to the latest code on git.oschina.net (click the "source code" link)

    1. /**
    2. * The output is infinite classification. I wrote it myself ~
    3. *
    4. * @ Author binny_w@qq.com
    5. * @ Since 2013-09-24 AM
    6. */
    7. /* Example */
    8. /*
    9. $ ArrAll = array (
    10. Array ('id' => 1, 'name' => 'topic category _ 1', 'Name _ EN' => 'cat _ 1 ', 'parent _ id' => 0 ),
    11. Array ('id' => 2, 'name' => 'topic category _ 2', 'Name _ EN' => 'cat _ 2 ', 'parent _ id' => 0 ),
    12. Array ('id' => 3, 'name' => 'topic category _ 3', 'Name _ EN' => 'cat _ 3 ', 'parent _ id' => 1 ),
    13. Array ('id' => 4, 'name' => 'topic category _ 4', 'Name _ EN' => 'cat _ 4 ', 'parent _ id' => 1 ),
    14. Array ('id' => 5, 'name' => 'topic category _ 5', 'Name _ EN' => 'cat _ 5 ', 'parent _ id' => 2 ),
    15. Array ('id' => 6, 'name' => 'topic category _ 6', 'Name _ EN' => 'cat _ 6 ', 'parent _ id' => 4 ),
    16. Array ('id' => 7, 'name' => 'topic category _ 7', 'Name _ EN' => 'cat _ 7 ', 'parent _ id' => 6 ),
    17. Array ('id' => 8, 'name' => 'topic category _ 8', 'Name _ EN' => 'cat _ 8 ', 'parent _ id' => 7 ),
    18. Array ('id' => 9, 'name' => 'topic category _ 9', 'Name _ EN' => 'cat _ 9 ', 'parent _ id' => 6)
    19. );
    20. $ ObjT = new TreeList ($ arrAll );
    21. Print_r ($ objT-> arrAll );
    22. Print_r ($ objT-> arrIdAll );
    23. Print_r ($ objT-> arrIdChildren );
    24. Print_r ($ objT-> arrIdSon );
    25. Print_r ($ objT-> arrIdLeaf );
    26. Print_r ($ objT-> arrIdRelation );
    27. Print_r ($ objT-> arrIdRelationSimple );
    28. Print_r ($ objT-> arrIdRoot );
    29. Print_r ($ objT-> arrIdBackPath );
    30. Print ($ objT-> getTable ());
    31. Print ($ objT-> getSelect ('cat', array (1, 8), true ));
    32. */
    33. //! Defined ('in _ framework') & die ('404 page ');
    34. Class TreeList {
    35. /**
    36. * Analyze all data that may be used
    37. */
    38. Public $ arrAll = array (); // raw data
    39. Public $ arrIdRelation = array (); // multidimensional relationship named by _ ID
    40. Public $ arrIdRelationSimple = array (); // simplify the multi-dimensional relationship by pressing _ ID as the key name to output a tree chart
    41. Public $ arrIdAll = array (); // The _ ID converted from the original data as the key name array.
    42. Public $ arrIdSon = array (); // all parent-child relationships
    43. Public $ arrIdLeaf = array (); // The _ ID of the leaf node
    44. Public $ arrIdRoot = array (); // _ ID of the root node
    45. Public $ arrIdChildren = array (); // descendant _ ID under each node
    46. Public $ arrIdBackPath = array (); // Each node returns to the root
    47. Public $ strItem ='
      {$ StrSep} {$ name} '; // Structure of the output tree
    48. /**
    49. * Constructor to pass in raw data
    50. */
    51. Public function _ construct ($ arrData ){
    52. $ This-> arrAll = $ arrData;
    53. $ This-> processData ();
    54. }
    55. /**
    56. * Simple tree
    57. */
    58. Public function getHtml (){
    59. Return $ this-> genHtml ();
    60. }
    61. /**
    62. * Use Table to draw trees
    63. */
    64. Public function getTable (){
    65. $ This-> strItem ='
    {$ StrSep} {$ name} {$ Name} {$ Name_en}
  • $ StrRe. ='
  • $ StrRe. = $ this-> genHtml ();
  • $ StrRe. ='
  • '; ';
    Structure Chinese name English name
    ';
  • Return $ strRe;
  • }
  • /**
  • * Display in the drop-down list
  • * Example:
  • * $ ObjTreeList-> getSelect ('parent _ id', 0, false, 'class = "span5" ', array (0, 'Category as a level-1 topic comment ')))
  • */
  • Public function getSelect ($ strName = 'tree', $ arrValue = array (), $ blmMulti = false, $ strExt = '', $ arrFirst = null ){
  • ! Is_array ($ arrValue) & $ arrValue = array ($ arrValue );
  • Foreach ($ this-> arrIdAll as $ strTemp => $ arrTemp ){
  • $ This-> arrIdAll [$ strTemp] ['selected'] = '';
  • If (in_array ($ arrTemp ['id'], $ arrValue )){
  • $ This-> arrIdAll [$ strTemp] ['selected'] = 'Selected = "selected "';
  • }
  • }
  • $ This-> strItem ='{$ StrSep} {$ name}';
  • $ StrRe ='$ StrRe. = ($ blmMulti? 'Multiple = "multiple" ': ''). (empty ($ strExt )? '':''. $ StrExt). '> ';If (is_array ($ arrFirst) & count ($ arrFirst) = 2 ){$ StrRe. =''. $ ArrFirst [1].'';}$ StrRe. = $ this-> getHtml ().'';
  • Return $ strRe;
  • }
  • /* ----- The following are private functions for data processing, such as recursion and loops. they are very complicated! -----*/
  • Private function helpForGetRelation ($ arrData ){
  • $ ArrRe = array ();
  • Foreach ($ arrData as $ strTemp => $ arrTemp ){
  • $ ArrRe [$ strTemp] = $ arrTemp;
  • If (isset ($ this-> arrIdRelation [$ strTemp]) {
  • $ ArrRe [$ strTemp] = $ this-> arrIdRelation [$ strTemp];
  • }
  • If (count ($ arrRe [$ strTemp])> 0 ){
  • $ ArrRe [$ strTemp] = $ this-> helpForGetRelation ($ arrRe [$ strTemp]);
  • } Else {
  • Array_push ($ this-> arrIdLeaf, $ strTemp );
  • }
  • }
  • Return $ arrRe;
  • }
  • Private function helpForGetChildren ($ arrData ){
  • $ ArrRe = array_keys ($ arrData );
  • Foreach ($ arrData as $ arrTemp ){
  • $ ArrRe = array_merge ($ arrRe, $ this-> helpForGetChildren ($ arrTemp ));
  • }
  • Return $ arrRe;
  • }
  • Private function helpForGetBackPath ($ str ){
  • $ ArrRe = array ();
  • $ IntTemp = $ this-> arrIdAll [$ str] ['parent _ id'];
  • If ($ intTemp> 0 ){
  • $ IntTemp = '_'. $ intTemp;
  • Array_push ($ arrRe, $ intTemp );
  • $ ArrRe = array_merge ($ arrRe, $ this-> helpForGetBackPath ($ intTemp ));
  • }
  • Return $ arrRe;
  • }
  • Private function processData (){
  • Foreach ($ this-> arrAll as $ arrTemp ){
  • $ StrTemp = '_'. $ arrTemp ['id'];
  • $ This-> arrIdAll [$ strTemp] = $ arrTemp;
  • If ($ arrTemp ['parent _ id']> 0 ){
  • $ StrTemp _ = '_'. $ arrTemp ['parent _ id'];
  • ! Isset ($ this-> arrIdRelation [$ strTemp _]) & $ this-> arrIdRelation [$ strTemp _] = array ();
  • $ This-> arrIdRelation [$ strTemp _] [$ strTemp] = array ();
  • ! Isset ($ this-> arrIdSon [$ strTemp _]) & $ this-> arrIdSon [$ strTemp _] = array ();
  • Array_push ($ this-> arrIdSon [$ strTemp _], $ strTemp );
  • } Else {
  • ! Isset ($ this-> arrIdRelation [$ strTemp]) & $ this-> arrIdRelation [$ strTemp] = array ();
  • Array_push ($ this-> arrIdRoot, $ strTemp );
  • }
  • }
  • $ This-> arrIdRelation = $ this-> helpForGetRelation ($ this-> arrIdRelation );
  • $ This-> arrIdLeaf = array_unique ($ this-> arrIdLeaf );
  • Foreach ($ this-> arrIdRelation as $ strTemp => $ arrTemp ){
  • $ This-> arrIdChildren [$ strTemp] = $ this-> helpForGetChildren ($ arrTemp );
  • In_array ($ strTemp, $ this-> arrIdRoot) & $ this-> arrIdRelationSimple [$ strTemp] = $ arrTemp;
  • }
  • $ ArrTemp = array_keys ($ this-> arrIdAll );
  • Foreach ($ arrTemp as $ strTemp ){
  • $ This-> arrIdBackPath [$ strTemp] = $ this-> helpForGetBackPath ($ strTemp );
  • }
  • }
  • Private function genSeparator ($ intLen ){
  • $ StrRe = '';
  • $ I = 0;
  • While ($ I <$ intLen ){
  • $ StrRe. = ''. ($ I + 1 = $ intLen )? 'Hangzhou': '│ ');
  • $ I ++;
  • }
  • ! Empty ($ strRe) & amp; $ strRe. = ';
  • Return $ strRe;
  • }
  • Private function genHtml ($ arrRelation = null, $ intSep = 0 ){
  • $ StrRe = '';
  • Null ===$ arrRelation & $ arrRelation = $ this-> arrIdRelationSimple;
  • Foreach ($ arrRelation as $ strKey => $ arrTemp ){
  • If (count ($ this-> arrIdAll [$ strKey])> 0 ){
  • $ StrSep = $ this-> genSeparator ($ intSep );
  • Extract ($ this-> arrIdAll [$ strKey]);
  • Eval ('$ strRe. = "'. $ this-> strItem .'";');
  • Count ($ arrTemp)> 0 & $ strRe. = $ this-> genHtml ($ arrTemp, ($ intSep + 1 ));
  • }
  • }
  • Return $ strRe;
  • }
  • }

  • 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.