Welcome Guest, Not a member yet? Register   Sign In
MPTtree, Hieararchical trees in a database table

[eluser]crises[/eluser]
I finally found a way of doing a collapsible tree menu from data stored in this way (in my case categories of products for an online catalog). But now i would like to know if this solution is efficient.

In my app i have categories stored as:
Code:
+Root
|--Main Category 1
| |--Subcat 1
| |--Subcat 2
| | |--Sub-subcat 1
| | |--Sub-subcat 2
| |--Subcat 3
|--Main Category 2
| |--Subcat 4
| |--Subcat 5
|   |--Sub-subcat 3
|--Main Category 3
|--Main Category 4
| |--Subcat 6
| |--Subcat 7
I think you got the idea, nothing strange Smile Then i want a menu that firstly shows only the Main Categories, and then if i click in Main Category 1 for example it shows ONLY the first two childs of Main Category (Subcat 1 and Subcat 2). Then if i click in Subcat 2 the menu should show me the childs of Main Category 1 plus childs of Subcat 2. And so on, usual menu as shown in many websites.

To the point. In my catalog controller:
Code:
function category($current_category_url = null, $page=null)
{
    /*Rest of stuf*/

    $current_category = $this->Catalog_model->get_current_category($current_category_url);//Array with all data of the current category
    $root = $this->MPTtree->get_root();
    $main_categories = $this->MPTtree->get_children($root['lft'], $root['rgt']);//only Main Categories, without their childrens
    $data['menu'] = $this->Catalog_model->do_menu($main_categories, $current_category);

    /*Rest of stuf*/
}

And the functions in the model (Catalog_model):
Code:
function get_current_category($category_url)
{
    $this->db->select('*');
    $this->db->from('prod_category');
    $this->db->where('category_url =', $category_url);
    $this->db->limit(1);
    $query = $this->db->get();
    
    if ($query->num_rows() > 0)
    {
        $result = $query->row_array();
        return $result;
    }
}

function do_menu($tree, $current_category)
{
    $category_childrens = $this->MPTtree->get_children($current_category['lft'], $current_category['rgt']);
    $parents_current_category = $this->MPTtree->get_parents($current_category['lft'], $current_category['rgt']);
    $str = '<ul class="category-list>"';
    foreach($tree as $category)
    {
        $str .= '<li>';
        if ($category['name'] == $current_category['name'])
        {
            $str .= '<strong><a href="'.base_url().'catalog/category/'.$category['category_url'].'">'.$category['name'].'</a></strong>';
            $str .= $this->do_submenu($category_childrens, $current_category);
        }
        else
        {
            $str .= '<a href="'.base_url().'catalog/category/'.$category['category_url'].'">'.$category['name'].'</a>';
        }
        if ($current_parent = $this->check_parents($category, array_reverse($parents_current_category)))
        {
            $childrens_of_current_parent = $this->MPTtree->get_children($current_parent['lft'], $current_parent['rgt']);
            $str .= $this->do_submenu($childrens_of_current_parent, $current_category);
        }
        $str .= '</li>';
    }
    $str .= '</ul>';
    return $str;
}

function do_submenu($tree, $current_category)
{
    $category_childrens = $this->MPTtree->get_children($current_category['lft'], $current_category['rgt']);
    $parents_current_category = $this->MPTtree->get_parents($current_category['lft'], $current_category['rgt']);
    $str = '<ul class="subcategory-list">';
    foreach($tree as $category)
    {
        $str .= '<li>';
        if ($category['name'] == $current_category['category'])
        {
            $str .= '<strong><a href="'.base_url().'catalog/category/'.$category['category_url'].'">'.$category['name'].'</a></strong>';
            $str .= $this->do_submenu($category_childrens, $category);
        }
        else
        {
            $str .= '<a href="'.base_url().'catalog/category/'.$category['category_url'].'">'.$category['name'].'</a>';
        }
        if ($current_parent = $this->check_parents($category, array_reverse($parents_current_category)))
        {
            $childrens_of_current_parent = $this->MPTtree->get_children($current_parent['lft'], $current_parent['rgt']);
            $str .= $this->do_submenu($childrens_of_current_parent, $current_category);
        }
        $str .= '</li>';
    }
    $str .= '</ul>';
    return $str;
}
    
function check_parents($category, $parents_current_category)
{
    foreach($parents_current_category as $parent)
    {
        if ($category['name'] == $parent['name'] && $parent['name'] != 'Root') //Name of root node in tree
        {
            $current_parent = $parent;
            return $current_parent;
            exit;
        }
    }
}
Functions do_menu() and do_submenu() are esentially the same, but as i want different <ul> class i had to make two functions. If you dont need it you can only use one function. Well the code is not as simple as i would like, but at least is working. Now the question again: it is efficient enought? ty


Messages In This Thread
MPTtree, Hieararchical trees in a database table - by El Forum - 03-13-2008, 02:15 PM
MPTtree, Hieararchical trees in a database table - by El Forum - 03-14-2008, 01:35 PM
MPTtree, Hieararchical trees in a database table - by El Forum - 03-22-2008, 12:17 PM
MPTtree, Hieararchical trees in a database table - by El Forum - 03-27-2008, 01:25 PM
MPTtree, Hieararchical trees in a database table - by El Forum - 04-06-2008, 04:11 AM
MPTtree, Hieararchical trees in a database table - by El Forum - 04-06-2008, 05:15 AM
MPTtree, Hieararchical trees in a database table - by El Forum - 04-06-2008, 06:27 AM
MPTtree, Hieararchical trees in a database table - by El Forum - 05-04-2008, 11:30 AM
MPTtree, Hieararchical trees in a database table - by El Forum - 05-04-2008, 11:54 AM
MPTtree, Hieararchical trees in a database table - by El Forum - 05-11-2008, 05:49 AM
MPTtree, Hieararchical trees in a database table - by El Forum - 05-11-2008, 06:01 AM
MPTtree, Hieararchical trees in a database table - by El Forum - 05-11-2008, 06:11 AM
MPTtree, Hieararchical trees in a database table - by El Forum - 05-11-2008, 06:35 AM
MPTtree, Hieararchical trees in a database table - by El Forum - 05-16-2008, 03:19 PM
MPTtree, Hieararchical trees in a database table - by El Forum - 05-16-2008, 05:44 PM
MPTtree, Hieararchical trees in a database table - by El Forum - 05-16-2008, 06:07 PM
MPTtree, Hieararchical trees in a database table - by El Forum - 05-16-2008, 07:01 PM
MPTtree, Hieararchical trees in a database table - by El Forum - 05-17-2008, 04:05 AM
MPTtree, Hieararchical trees in a database table - by El Forum - 05-17-2008, 05:19 AM
MPTtree, Hieararchical trees in a database table - by El Forum - 05-17-2008, 05:50 AM
MPTtree, Hieararchical trees in a database table - by El Forum - 05-17-2008, 04:32 PM
MPTtree, Hieararchical trees in a database table - by El Forum - 05-17-2008, 04:51 PM
MPTtree, Hieararchical trees in a database table - by El Forum - 05-20-2008, 10:06 AM
MPTtree, Hieararchical trees in a database table - by El Forum - 05-30-2008, 11:24 AM
MPTtree, Hieararchical trees in a database table - by El Forum - 05-30-2008, 03:12 PM
MPTtree, Hieararchical trees in a database table - by El Forum - 05-30-2008, 03:57 PM
MPTtree, Hieararchical trees in a database table - by El Forum - 05-30-2008, 03:58 PM
MPTtree, Hieararchical trees in a database table - by El Forum - 05-30-2008, 04:08 PM
MPTtree, Hieararchical trees in a database table - by El Forum - 05-30-2008, 04:17 PM
MPTtree, Hieararchical trees in a database table - by El Forum - 05-30-2008, 04:22 PM
MPTtree, Hieararchical trees in a database table - by El Forum - 05-30-2008, 04:24 PM
MPTtree, Hieararchical trees in a database table - by El Forum - 06-11-2008, 12:42 PM
MPTtree, Hieararchical trees in a database table - by El Forum - 08-01-2008, 08:44 AM
MPTtree, Hieararchical trees in a database table - by El Forum - 08-01-2008, 01:05 PM
MPTtree, Hieararchical trees in a database table - by El Forum - 08-01-2008, 02:48 PM
MPTtree, Hieararchical trees in a database table - by El Forum - 08-01-2008, 03:07 PM
MPTtree, Hieararchical trees in a database table - by El Forum - 08-01-2008, 03:15 PM
MPTtree, Hieararchical trees in a database table - by El Forum - 08-01-2008, 03:18 PM
MPTtree, Hieararchical trees in a database table - by El Forum - 08-01-2008, 03:32 PM
MPTtree, Hieararchical trees in a database table - by El Forum - 08-02-2008, 09:38 AM
MPTtree, Hieararchical trees in a database table - by El Forum - 08-02-2008, 09:44 AM
MPTtree, Hieararchical trees in a database table - by El Forum - 08-02-2008, 03:06 PM
MPTtree, Hieararchical trees in a database table - by El Forum - 08-03-2008, 05:07 AM
MPTtree, Hieararchical trees in a database table - by El Forum - 08-18-2008, 10:33 AM
MPTtree, Hieararchical trees in a database table - by El Forum - 08-18-2008, 01:17 PM
MPTtree, Hieararchical trees in a database table - by El Forum - 08-22-2008, 08:13 AM
MPTtree, Hieararchical trees in a database table - by El Forum - 08-22-2008, 11:41 AM
MPTtree, Hieararchical trees in a database table - by El Forum - 09-12-2008, 09:03 PM
MPTtree, Hieararchical trees in a database table - by El Forum - 09-13-2008, 08:24 AM
MPTtree, Hieararchical trees in a database table - by El Forum - 09-23-2008, 12:19 PM
MPTtree, Hieararchical trees in a database table - by El Forum - 09-23-2008, 12:28 PM
MPTtree, Hieararchical trees in a database table - by El Forum - 09-23-2008, 12:33 PM
MPTtree, Hieararchical trees in a database table - by El Forum - 09-23-2008, 12:36 PM
MPTtree, Hieararchical trees in a database table - by El Forum - 09-24-2008, 02:55 AM
MPTtree, Hieararchical trees in a database table - by El Forum - 10-14-2008, 03:58 AM
MPTtree, Hieararchical trees in a database table - by El Forum - 10-14-2008, 06:55 AM
MPTtree, Hieararchical trees in a database table - by El Forum - 10-14-2008, 01:20 PM
MPTtree, Hieararchical trees in a database table - by El Forum - 10-14-2008, 01:34 PM
MPTtree, Hieararchical trees in a database table - by El Forum - 10-14-2008, 04:13 PM
MPTtree, Hieararchical trees in a database table - by El Forum - 10-14-2008, 04:32 PM
MPTtree, Hieararchical trees in a database table - by El Forum - 10-14-2008, 11:31 PM
MPTtree, Hieararchical trees in a database table - by El Forum - 10-14-2008, 11:37 PM
MPTtree, Hieararchical trees in a database table - by El Forum - 10-14-2008, 11:45 PM
MPTtree, Hieararchical trees in a database table - by El Forum - 10-15-2008, 01:17 AM
MPTtree, Hieararchical trees in a database table - by El Forum - 10-15-2008, 09:11 AM
MPTtree, Hieararchical trees in a database table - by El Forum - 10-15-2008, 09:45 AM
MPTtree, Hieararchical trees in a database table - by El Forum - 10-17-2008, 07:12 AM
MPTtree, Hieararchical trees in a database table - by El Forum - 10-28-2008, 12:45 PM
MPTtree, Hieararchical trees in a database table - by El Forum - 11-12-2008, 08:46 AM
MPTtree, Hieararchical trees in a database table - by El Forum - 11-14-2008, 11:30 AM
MPTtree, Hieararchical trees in a database table - by El Forum - 11-14-2008, 11:50 AM
MPTtree, Hieararchical trees in a database table - by El Forum - 11-14-2008, 03:33 PM
MPTtree, Hieararchical trees in a database table - by El Forum - 11-15-2008, 06:41 AM
MPTtree, Hieararchical trees in a database table - by El Forum - 01-05-2009, 02:02 PM
MPTtree, Hieararchical trees in a database table - by El Forum - 01-06-2009, 10:15 AM
MPTtree, Hieararchical trees in a database table - by El Forum - 01-06-2009, 01:44 PM
MPTtree, Hieararchical trees in a database table - by El Forum - 01-08-2009, 01:47 AM
MPTtree, Hieararchical trees in a database table - by El Forum - 01-15-2009, 02:16 PM
MPTtree, Hieararchical trees in a database table - by El Forum - 01-16-2009, 10:21 AM
MPTtree, Hieararchical trees in a database table - by El Forum - 01-16-2009, 11:18 AM
MPTtree, Hieararchical trees in a database table - by El Forum - 01-17-2009, 06:56 AM
MPTtree, Hieararchical trees in a database table - by El Forum - 01-17-2009, 09:17 AM
MPTtree, Hieararchical trees in a database table - by El Forum - 01-29-2009, 09:00 AM
MPTtree, Hieararchical trees in a database table - by El Forum - 04-11-2009, 10:31 AM
MPTtree, Hieararchical trees in a database table - by El Forum - 04-12-2009, 04:28 AM
MPTtree, Hieararchical trees in a database table - by El Forum - 04-12-2009, 04:47 AM
MPTtree, Hieararchical trees in a database table - by El Forum - 04-12-2009, 04:55 AM
MPTtree, Hieararchical trees in a database table - by El Forum - 04-13-2009, 08:11 AM
MPTtree, Hieararchical trees in a database table - by El Forum - 04-13-2009, 08:23 AM
MPTtree, Hieararchical trees in a database table - by El Forum - 04-13-2009, 09:20 AM
MPTtree, Hieararchical trees in a database table - by El Forum - 04-13-2009, 02:04 PM
MPTtree, Hieararchical trees in a database table - by El Forum - 04-13-2009, 06:26 PM
MPTtree, Hieararchical trees in a database table - by El Forum - 04-14-2009, 12:40 AM
MPTtree, Hieararchical trees in a database table - by El Forum - 04-22-2009, 02:17 AM
MPTtree, Hieararchical trees in a database table - by El Forum - 04-22-2009, 03:52 AM
MPTtree, Hieararchical trees in a database table - by El Forum - 04-22-2009, 03:56 AM
MPTtree, Hieararchical trees in a database table - by El Forum - 04-22-2009, 04:24 AM
MPTtree, Hieararchical trees in a database table - by El Forum - 04-28-2009, 05:44 AM
MPTtree, Hieararchical trees in a database table - by El Forum - 05-01-2009, 08:37 PM
MPTtree, Hieararchical trees in a database table - by El Forum - 05-02-2009, 03:34 AM
MPTtree, Hieararchical trees in a database table - by El Forum - 05-02-2009, 04:17 AM
MPTtree, Hieararchical trees in a database table - by El Forum - 05-02-2009, 07:12 AM
MPTtree, Hieararchical trees in a database table - by El Forum - 05-02-2009, 07:43 AM
MPTtree, Hieararchical trees in a database table - by El Forum - 05-02-2009, 10:13 AM
MPTtree, Hieararchical trees in a database table - by El Forum - 05-06-2009, 10:10 PM
MPTtree, Hieararchical trees in a database table - by El Forum - 05-07-2009, 07:31 AM
MPTtree, Hieararchical trees in a database table - by El Forum - 05-11-2009, 09:16 AM
MPTtree, Hieararchical trees in a database table - by El Forum - 05-12-2009, 12:34 PM
MPTtree, Hieararchical trees in a database table - by El Forum - 05-15-2009, 09:31 AM
MPTtree, Hieararchical trees in a database table - by El Forum - 08-12-2009, 02:37 AM
MPTtree, Hieararchical trees in a database table - by El Forum - 08-12-2009, 02:08 PM
MPTtree, Hieararchical trees in a database table - by El Forum - 08-21-2009, 09:20 AM
MPTtree, Hieararchical trees in a database table - by El Forum - 09-07-2009, 05:19 PM
MPTtree, Hieararchical trees in a database table - by El Forum - 09-10-2009, 12:43 PM
MPTtree, Hieararchical trees in a database table - by El Forum - 09-23-2009, 02:13 AM
MPTtree, Hieararchical trees in a database table - by El Forum - 11-13-2009, 02:39 PM
MPTtree, Hieararchical trees in a database table - by El Forum - 11-15-2009, 11:21 AM
MPTtree, Hieararchical trees in a database table - by El Forum - 11-16-2009, 04:06 AM
MPTtree, Hieararchical trees in a database table - by El Forum - 11-16-2009, 11:11 AM
MPTtree, Hieararchical trees in a database table - by El Forum - 11-17-2009, 09:59 AM
MPTtree, Hieararchical trees in a database table - by El Forum - 11-17-2009, 03:54 PM
MPTtree, Hieararchical trees in a database table - by El Forum - 03-17-2010, 12:18 PM
MPTtree, Hieararchical trees in a database table - by El Forum - 06-09-2010, 10:07 AM
MPTtree, Hieararchical trees in a database table - by El Forum - 01-13-2011, 05:11 AM
MPTtree, Hieararchical trees in a database table - by El Forum - 03-23-2011, 03:36 AM
MPTtree, Hieararchical trees in a database table - by El Forum - 05-26-2011, 12:25 PM
MPTtree, Hieararchical trees in a database table - by El Forum - 05-31-2011, 11:35 AM
MPTtree, Hieararchical trees in a database table - by El Forum - 05-31-2011, 11:46 AM
MPTtree, Hieararchical trees in a database table - by El Forum - 05-31-2011, 11:55 AM
MPTtree, Hieararchical trees in a database table - by El Forum - 05-31-2011, 04:05 PM
MPTtree, Hieararchical trees in a database table - by El Forum - 06-28-2011, 05:21 AM
MPTtree, Hieararchical trees in a database table - by El Forum - 06-28-2011, 06:50 AM
MPTtree, Hieararchical trees in a database table - by El Forum - 06-28-2011, 08:12 AM
MPTtree, Hieararchical trees in a database table - by El Forum - 06-28-2011, 09:13 AM
MPTtree, Hieararchical trees in a database table - by El Forum - 06-28-2011, 09:58 AM
MPTtree, Hieararchical trees in a database table - by El Forum - 06-28-2011, 10:28 AM
MPTtree, Hieararchical trees in a database table - by El Forum - 06-28-2011, 10:42 AM
MPTtree, Hieararchical trees in a database table - by El Forum - 04-22-2012, 05:04 PM
MPTtree, Hieararchical trees in a database table - by El Forum - 04-23-2013, 06:00 AM
MPTtree, Hieararchical trees in a database table - by El Forum - 10-01-2013, 09:36 AM



Theme © iAndrew 2016 - Forum software by © MyBB