CodeIgniter Forums

Full Version: creating category tree array
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[eluser]Constantin Iliescu[/eluser]
Hello!

I have this piece of code I found on the Internet:

Code:
function build_child($old_id)
    {
            $temp_tree = "";
            
            $children = $this->category_model->get_subcategories(array('category_id' => $old_id));
            
            foreach ($children as $child)
            {
                $id = $child->id_subcat;
                $title = $this->category_model->get_title_or_alias($child->id_subcat);
                
                for ( $c=0;$c<$this->depth;$c++ )            // Indent over so that there is distinction between levels
                { $temp_tree .= "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"; }
                
                $temp_tree .= "- ". $title . ' - ' . $this->height . ' - ' . $this->depth . "<br />";
                $this->depth++;
                
                $temp_tree .= $this->build_child($child->id_subcat);
                
                $this->depth--;
                array_push($this->exclude, $child->id_subcat);

                $this->height++;
            }
            
            return $temp_tree;
    }
    
    function get_tree_by_category_alias($alias = FALSE)
    {
        if ( ! $alias)
            return FALSE;
            
        $tree = "";
        $this->load->model('category_model');
        $left_category = $this->category_model->get_categories(array('alias' => $alias));
                
        $left_categories = $this->category_model->get_subcategories(array('category_id' => $left_category->id_cat));
        
        $tree .= $this->build_child($left_category->id_cat);
        
        return $tree;
    }

This code displays a string like this:
Code:
- Cat 1
  - Cat 11
    - Cat 111
    - Cat 112
  - Cat 12
- Cat 2
  - Cat 21
  - Cat 22

What I want to do is save this tree like this:
Code:
array(
[0] = array (
   'title' => Cat 1,
   'id'    => 1,
   'subcategs' => array(
      [0] => array(
        'title' => Cat 11,
        'id'    => 11,
        'subcategs' => array(
          and so on...
        )
      )
)
)

The call looks like this:
Code:
$tree = $this->get_tree_by_category_alias('LeftMenu');

It may have a simple solution, but I'm struggling with this for a hour.
I'd appreciate any idea.

Thanks!

El Forum

[eluser]WanWizard[/eluser]
From the top of my head:
Code:
function build_child($old_id)
{
    $result = array();

    $children = $this->category_model->get_subcategories(array('category_id' => $old_id));

    foreach ($children as $child)
    {
        $id = $child->id_cat;

        $result[$id] = $child;
        
        $children = $this->build_child($child->id_subcat);
        
        if ( count($children) )
        {
            $result[$id]['subcategs'] = $children;
        }
    }
    
    return $result;
}

function get_tree_by_category_alias($alias = FALSE)
{
    if ( ! $alias)
    {
        return FALSE;
    }
        
    $this->load->model('category_model');

    $left_category = $this->category_model->get_categories(array('alias' => $alias));

    return $this->build_child($left_category->id_cat);
}

El Forum

[eluser]Constantin Iliescu[/eluser]
Perfect! Smile

Thanks a lot, WanWizard!

El Forum

[eluser]Unknown[/eluser]
I am facing same problem. how can i print and make dropdown list for category with view.