Unlimited grading and tree node output

Source: Internet
Author: User
  • $strRe = '
  • ';
    By passing in simple raw data, you can derive n relationships between nodes, and you can output tree-like DOM
    Note: Please refer to the latest code on git.oschina.net (click "Source Sources" link)
    1. /**
    2. * Output Unlimited classification, I wrote the Oh ~
    3. *
    4. * @author binny_w@qq.com
    5. * @since 2013-09-24 AM
    6. */
    7. /* Use example */
    8. /*
    9. $arrAll = Array (
    10. Array (' id ' = + 1, ' name ' = ' = ' column category _1 ', ' name_en ' = ' cat_1 ', ' parent_id ' and ' = 0 '),
    11. Array (' id ' = + 2, ' name ' = ' = ' column category _2 ', ' name_en ' = ' cat_2 ', ' parent_id ' and ' = 0 '),
    12. Array (' id ' = + 3, ' name ' = ' = ' column category _3 ', ' name_en ' = ' cat_3 ', ' parent_id ' and ' = 1 '),
    13. Array (' id ' = + 4, ' name ' = ' = ' column category _4 ', ' name_en ' = ' cat_4 ', ' parent_id ' and ' = 1 '),
    14. Array (' id ' = + 5, ' name ' = ' = ' column category _5 ', ' name_en ' = ' cat_5 ', ' parent_id ' and ' = 2 '),
    15. Array (' id ' = + 6, ' name ' = ' = ' column category _6 ', ' name_en ' = ' cat_6 ', ' parent_id ' and ' = 4 '),
    16. Array (' id ' = + 7, ' name ' = ' = ' column category _7 ', ' name_en ' = ' cat_7 ', ' parent_id ' and ' = 6 '),
    17. Array (' id ' = + 8, ' name ' = ' = ' column category _8 ', ' name_en ' = ' cat_8 ', ' parent_id ' and ' = 7 '),
    18. Array (' id ' = + 9, ' name ' = ' = ' column category _9 ', ' name_en ' = ' cat_9 ', ' parent_id ' and ' = 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_frame ') && 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 (); Multi-dimensional relationships by _id as key names
    40. Public $arrIdRelationSimple = Array (); Simplification of multidimensional relationships by _id as key names, used to output tree graphs
    41. Public $arrIdAll = Array (); An array of _id that convert raw data into a key name
    42. Public $arrIdSon = Array (); All parent-child relationships
    43. Public $arrIdLeaf = Array (); _id of leaf nodes
    44. Public $arrIdRoot = Array (); _id of the root node
    45. Public $arrIdChildren = Array (); Descendants of _id under each node
    46. Public $arrIdBackPath = Array (); Each node is inverted back to the root
    47. Public $strItem = '
      {$STRSEP} {$name} '; Structure of the output tree
    48. /**
    49. * constructor, incoming 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;
  • }
  • /**
  • * Displayed in the drop-down box
  • * Example:
  • * $objTreeList->getselect (' parent_id ', 0, False, ' class= ' Span5 "', Array (0, ' ≡ as a first column ≡ ')))
  • */
  • 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 that handle data, such as recursion and looping, which are complex! ----- */
  • 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)? ' ├ ': ' │ ');
  • $i + +;
  • }
  • !empty ($strRe) && $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;
  • }
  • Copy the Code
  • Related Article

    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.