Guest User

Untitled

a guest
Dec 16th, 2019
91
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2. $thearray = [];
  3. $terms = get_terms("pa_mymelp");
  4. foreach ( $terms as $term ) {
  5. $categories =  $term->name;
  6. array_push($thearray, $categories);
  7. }
  8.  
  9. $categoryLines = $thearray;
  10.            
  11. function buildCategoryTree($categoryLines, $separator) {
  12.     $catTree = array();
  13.     foreach ($categoryLines as $catLine) {
  14.        $path = explode($separator, $catLine);
  15.        $node = & $catTree;
  16.        foreach ($path as $cat) {
  17.            $cat = trim($cat);
  18.            if (!isset($node[$cat])) {
  19.                $node[$cat] = array();
  20.            }
  21.            $node = & $node[$cat];
  22.        }
  23.     }
  24.     return $catTree;
  25. }
  26.  
  27. function displayCategoryTree($categoryTree, $indent = '') {
  28.     foreach ($categoryTree as $node => $children) {
  29.         echo $indent . $node . "\n";
  30.         displayCategoryTree($children, $indent . '|- ');
  31.     }
  32. }
  33.  
  34. $categoryTree = buildCategoryTree($categoryLines, '/');
  35.  
  36. function displayHtmlCategoryTree($categoryTree, $id = null, $pathSeparator = '/', $parents = '') {
  37.     if (empty($categoryTree)) return '';
  38.  
  39.     $str = '<ul' . (!empty($id) ? ' id="'.$id.'"' : '') . '>';
  40.     foreach ($categoryTree as $node => $children) {
  41.         $currentPath = $parents . (empty($parents) ? '' : $pathSeparator) . $node;
  42.         $thelink = '';
  43.         $opener = 0;
  44.         if (substr_count($currentPath, '/')==5){
  45.         $patterns = array(" ", "/", ".");
  46.         $thelink = 'http://caap.co.nz/?pa_mymelp=' . strtolower(str_replace($patterns, '-', $currentPath));
  47.         $str .= '<li title="' . $currentPath . '">' . '<input class="first" name="' . $currentPath . '" type="checkbox" id="' . $currentPath . '">' . '<label  for="' . $currentPath . '">' . '<a href="' . $thelink .'">' . $node . '</a></label>' .
  48.         /*displayHtmlCategoryTree($children, null, $pathSeparator, $currentPath) .
  49.         */'</li>';}
  50.         else       
  51.         {$str .= '<li title="' . $currentPath . '">' . '<input class="first" name="' . $currentPath . '" type="checkbox" id="' . $currentPath . '">' . '<label for="' . $currentPath . '">' . $node . '</label>';
  52.         $str.=displayHtmlCategoryTree($children, null, $pathSeparator, $currentPath);
  53.        
  54.  
  55.         }
  56.  
  57.  
  58.         }
  59.    
  60.     $str .= '</li></ul>';
  61.     return $str;
  62. }
  63.  
  64.     echo displayHtmlCategoryTree($categoryTree, "CategoryTree", '/');
  65.  
  66.  
  67. ?>
RAW Paste Data