[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 .= " "; }
$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!