Home > Backend Development > PHP Tutorial > Unlimited hierarchical and tree node output

Unlimited hierarchical and tree node output

WBOY
Release: 2016-07-25 08:49:51
Original
1248 people have browsed it
';
  • $strRe = '
  • By passing in simple original data, N types of relationships between nodes can be derived, and a tree-like DOM can be output
    Note: Please refer to the latest code on git.oschina.net (click the "Source Code Source" link) Unlimited hierarchical and tree node output
    1. /**
    2. * Output unlimited classification, I wrote it myself~
    3. *
    4. * @author binny_w@qq.com
    5. * @since 2013-09-24 AM
    6. */
    7. /* Usage example*/
    8. /*
    9. $arrAll = array(
    10. array('id' => 1, 'name' => 'Column classification_1', 'name_en' => 'cat_1', 'parent_id' => 0),
    11. array('id' => 2, 'name' => 'Column classification_2', 'name_en' => 'cat_2', 'parent_id' => 0),
    12. array('id' => 3, 'name' => 'Column classification_3', 'name_en' => ' cat_3', 'parent_id' => 1),
    13. array('id' => 4, 'name' => 'Column category_4', 'name_en' => 'cat_4', 'parent_id' = > 1),
    14. array('id' => 5, 'name' => 'Column classification_5', 'name_en' => 'cat_5', 'parent_id' => 2),
    15. array ('id' => 6, 'name' => 'Column classification_6', 'name_en' => 'cat_6', 'parent_id' => 4),
    16. array('id' => 7, 'name' => 'Column classification_7', 'name_en' => 'cat_7', 'parent_id' => 6),
    17. array('id' => 8, 'name' => ; 'Column classification_8', 'name_en' => 'cat_8', 'parent_id' => 7),
    18. array('id' => 9, 'name' => 'Column classification_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_FRAME') && die('404 Page');
    34. class TreeList {
    35. /* *
    36. * Analyze all possible data
    37. */
    38. public $arrAll = array(); // Original data
    39. public $arrIdRelation = array(); // Multidimensional relationship with _ID as key name
    40. public $arrIdRelationSimple = array(); // Simplification of multi-dimensional relationships based on _ID as key name, used to output tree diagram
    41. public $arrIdAll = array(); // Convert original data into an array with _ID as key name
    42. public $arrIdSon = array() ; // All parent-child relationships
    43. public $arrIdLeaf = array(); // _ID of leaf nodes
    44. public $arrIdRoot = array(); // _ID of root nodes
    45. public $arrIdChildren = array(); // The descendant_ID of each node
    46. public $arrIdBackPath = array(); // Each node goes back to the root
    47. public $strItem = '
      {$strSep}{$name}'; / / Output the structure of the tree
    48. /**
    49. *Constructor, pass in original 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 .= '< tr>
  • ';
  • $strRe .= $this->genHtml();
  • $strRe .= '
  • StructureChinese nameEnglish name
    ';
  • return $strRe;
  • }
  • /* *
  • * Display
  • in the drop-down box * example:
  • * $objTreeList->getSelect('parent_id', 0, false, 'class="span5"', array(0, '≡ as a first-level 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 = '' ;
  • $strRe = '';
  • return $strRe;
  • }
  • /* ----- The following are private functions for processing data, recursion and loops Like, 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) ? '├' : '│');
  • $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 code


    source:php.cn
    Previous article:Automatically solve arithmetic problems - give it to your son to practice quick calculations o(∩_∩)o Next article:Send messages to your own WeChat at any time - PHP
    Statement of this Website
    The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
    Latest Articles by Author
    Latest Issues
    Related Topics
    More>
    Popular Recommendations
    Popular Tutorials
    More>
    Latest Downloads
    More>
    Web Effects
    Website Source Code
    Website Materials
    Front End Template