Results 1 to 3 of 3

Hybrid View

  1. #1
    Join Date
    Apr 2008
    Posts
    82
    Plugin Contributions
    0

    Default Categories tree modifications

    Hi all!
    I've been struggling with my categories for some time now and was wondering if somebody could help me figure it out. I am trying to modify my categories so every parent category would be in a div with ul of its subcategories, that's an easy part. Also I need to add class to the first ul and another attribute - data with incremental value for each ul and this is where I am lost, I have no idea how to do it.

    To simplify, I want my categories with 2 parent categories to look like this:
    <div class="col_3">
    <a>Parent Cat</a>
    <ul data="1" class="current">
    <li>sub cat</li>
    <li>sub cat</li>
    </ul>
    </div>

    <div class="col_3">
    <a>Parent Cat</a>
    <ul data="2">
    <li>sub cat</li>
    <li>sub cat</li>
    </ul>
    </div>

    My categories ul generator:
    Code:
    <?php
    
    class zen_categories_ul_generator {
        var $root_category_id = 0,
        $max_level = 6,
        $data = array(),
        $root_start_string = '',
        $root_end_string = '',
        $parent_start_string = '',
        $parent_end_string = '',
        $div_start_string = '<div%s>',
        $div_end_string = '</div>',
        $parent_group_start_string = '<ul%s>',
        $parent_group_end_string = '</ul>',
    
        $child_start_string = '<li%s>',
        $child_end_string = '</li>',
    
        $spacer_string = '
    	',
        $spacer_multiplier = 1;
    
        var $document_types_list = ' (3) ';// acceptable format example: ' (3, 4, 9, 22, 18) '
    
        function zen_categories_ul_generator($load_from_database = true)
        {
            global $languages_id, $db;
            $this->data = array();
            $categories_query = "select c.categories_id, cd.categories_name, c.parent_id
                                 from " . TABLE_CATEGORIES . " c, " . TABLE_CATEGORIES_DESCRIPTION . " cd
                                 where c.categories_id = cd.categories_id
                                 and c.categories_status=1 " .
                                 " and cd.language_id = '" . (int)$_SESSION['languages_id'] . "' " .
                                 " order by c.parent_id, c.sort_order, cd.categories_name";
            $categories = $db->Execute($categories_query);
            while (!$categories->EOF) {
              $this->data[$categories->fields['parent_id']][$categories->fields['categories_id']] = array('name' => $categories->fields['categories_name'], 'count' => 0);
                $categories->MoveNext();
            }
        }
    
        function buildBranch($parent_id, $level = 1, $submenu=false) {
          if ($parent_id != '0') {
            $result = sprintf($this->parent_group_start_string, ($submenu==true) ? ' class="level'. ($level) . '"' : '' );
          }
            if (($this->data[$parent_id])) {
                foreach($this->data[$parent_id] as $category_id => $category) {
                  $category_link = $category_id;
    			  if (($this->data[$category_id])) {
                    $result .= sprintf($this->div_start_string, ($submenu==true) ? ' class="col_3"' : '');
    			  } else {
                    if (($this->data[$category_id]) && ($submenu==false)) {
                      $result .= sprintf($this->parent_group_start_string, ($submenu==true) ? ' class="level'. ($level+1) . '"' : '');
                      $result .= sprintf($this->child_start_string, ($submenu==true) ? ' class="submenu"' : '');
                    } else {
                      $result .= sprintf($this->child_start_string, '');
                    }
    			  }
                  if ($level == 0) {
                    $result .= $this->root_start_string;
                  }
                  $result .= str_repeat($this->spacer_string, $this->spacer_multiplier * $level) . '<a href="' . zen_href_link(FILENAME_DEFAULT, 'cPath=' . $category_link) . '">';
                  $result .= $category['name'];
                  $result .= '</a>';
                  if ($level == 0) {
                    $result .= $this->root_end_string;
                  }
                  if (($this->data[$category_id])) {
                    $result .= $this->parent_end_string;
                  }
                  if (($this->data[$category_id]) && (($this->max_level == '0') || ($this->max_level > $level+1))) {
                    $result .= $this->buildBranch($category_id, $level+1, $submenu);
                  }
    			  
    			    if (($this->data[$category_id])) {
                    $result .= $this->div_end_string;
    			  } else {
                    $result .= $this->child_end_string;
    			  }
                  
                }
            }
            if ($level > 0) {
              $result .= $this->parent_group_end_string;
            }
            return $result;
        }
    
        function buildTree($submenu=false)
        {
            return $this->buildBranch($this->root_category_id, '', $submenu);
        }
    }
    ?>
    I greatly appreciate any help!!!

  2. #2
    Join Date
    Jul 2005
    Location
    Upstate NY
    Posts
    22,010
    Plugin Contributions
    25

    Default Re: Categories tree modifications

    Categories Dressing from Free Addons will generate a correctly nested set of <ul> list(s), with ids for each <ul>. It doesn't give the precise structure you describe, but it will do the same job.

  3. #3
    Join Date
    Apr 2008
    Posts
    82
    Plugin Contributions
    0

    Default Re: Categories tree modifications

    Thank you gjh42, for looking into this for me!
    But I found a much simpler solution, if anybody else needs to do something like this just use "preg_replace_callback" after loading and producing the menu list, with it you can replace whatever you want.
    My function:

    $count = 1;
    $menulist = preg_replace_callback('/level1/', 'rep_count', $menulist);
    function rep_count($matches) {
    global $count;
    return '#section' . $count++;
    }
    $menulist = str_replace('<ul data="#section1">','<ul data="#section1" class="current">',$menulist);

 

 

Similar Threads

  1. Ssimple categories tree
    By Laki in forum Addon Sideboxes
    Replies: 0
    Last Post: 15 Sep 2015, 04:32 PM
  2. Uncollapsed Categories Tree
    By fragfutter in forum Addon Sideboxes
    Replies: 36
    Last Post: 2 Feb 2014, 08:46 PM
  3. Hide categories and tree categories sidebox
    By adowty in forum Addon Sideboxes
    Replies: 1
    Last Post: 16 Aug 2011, 03:09 PM
  4. Categories tree
    By swaper in forum Addon Sideboxes
    Replies: 5
    Last Post: 3 Feb 2011, 02:11 PM
  5. Css tree Categories
    By qingtianyu9 in forum General Questions
    Replies: 0
    Last Post: 28 Aug 2007, 01:59 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
disjunctive-egg