CodeIgniter Forums
hierarchy system in mysql with php - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: hierarchy system in mysql with php (/showthread.php?tid=19372)



hierarchy system in mysql with php - El Forum - 06-05-2009

[eluser]FutureKing[/eluser]
Hi,

I am building a custom cms for my website. I need to create category hierarchy system in my cms. That is admin can create delete edit categories according to his/her needs in any depth.
I need...
1) good tutorial about it
2) piece of already written code that I can use in my program.(if possible)
3) A good UI for managing categories.

Please help

I have a website in asp.net mssql. But I want to change it into php mysql. Currently I am using multiple tables for category-sub-category system. But it has its own limitation. For example I can not go to 3 level depth if I have only 2 tables. Therefore I want a good algorithm+program with which I can create such type of system in single table


hierarchy system in mysql with php - El Forum - 06-05-2009

[eluser]sl3dg3hamm3r[/eluser]
See this.
And this.


hierarchy system in mysql with php - El Forum - 06-05-2009

[eluser]TheFuzzy0ne[/eluser]
Try [url="http://ellislab.com/forums/viewthread/74114/"]MPTTree[/url].


hierarchy system in mysql with php - El Forum - 06-07-2009

[eluser]FutureKing[/eluser]
[quote author="TheFuzzy0ne" date="1244214670"]Try [url="http://ellislab.com/forums/viewthread/74114/"]MPTTree[/url].[/quote]

Thanks but I want to know how to use it. Where to place these files.


hierarchy system in mysql with php - El Forum - 06-07-2009

[eluser]FutureKing[/eluser]
I got error on this line : $this->load->MPTT('wiki_tree', 'pages');


hierarchy system in mysql with php - El Forum - 06-07-2009

[eluser]TheFuzzy0ne[/eluser]
I don't use the loader. I include the mpttree.php file:
Code:
include(APPPATH.'models/mpttree.php');

and initialise it within scope of my model:
Code:
$this->mptt = new MPTTree();
$this->mptt->set_table($this->table);



hierarchy system in mysql with php - El Forum - 06-07-2009

[eluser]FutureKing[/eluser]
Thank you very much.

One more thing I want to ask;

Please look at the code below:
Code:
function index()
    {
        $this->load->model('MPTtree');
        $this->MPTtree->set_opts(array( 'table' => 'orgchart',
        'left' => 'lft',
        'right' => 'rgt',
        'id' => 'catid',
        'title' => 'member'));
        $tree = $this->MPTtree->tree2array();    
        $this->doit($tree);
    }
    function doit($tree,$prev_text=''){                
        foreach($tree as $node){                        
            if($prev_text!=''){
                echo $prev_text.$node['member'];                
            }else{
                echo $node['member'];
            }
            echo "<br />";
            //echo $node['lft'].'<br />';
            
            if(isset($node['children'])){
                $prev_text=$prev_text.$node['member'].'--';
                $tree=$node['children'];
                $this->doit($tree,$prev_text);
            }
        }
        $prev_text='';        
    }

The above code generates the following output from tree2array() method:
albert
albert--A child!
albert--bert
albert--chuck
albert--chuck--donna
albert--chuck--eddie
albert--chuck--fred

I just want to ask, I am using $this->doit($tree,$prev_text); from doit() function.
Is it ok to call any function from inside of itself?

If not then Is there any alternative way to get the same output?


hierarchy system in mysql with php - El Forum - 06-07-2009

[eluser]TheFuzzy0ne[/eluser]
I'm sure you're code can be neatened up a little more, but I do think you're on the right track. Recursion is definitely the answer. If you're still struggling tomorrow, I might be able to give you a working example.


hierarchy system in mysql with php - El Forum - 06-08-2009

[eluser]FutureKing[/eluser]
[quote author="TheFuzzy0ne" date="1244431844"]I'm sure you're code can be neatened up a little more, but I do think you're on the right track. Recursion is definitely the answer. If you're still struggling tomorrow, I might be able to give you a working example.[/quote]

Thank you very much. You have provided great support and solved my one of biggest problem.


hierarchy system in mysql with php - El Forum - 06-08-2009

[eluser]TheFuzzy0ne[/eluser]
Here's a function that doesn't echo anything. It just calls upon itself and builds the string up and passes it back when it's done.

Code:
function doit($tree, $prefix="")
{
    $title = $tree['title'];
    $prefix = ($prefix) ? $prefix.'--'.$title : $title;
    $str = $title."<br />\n";
    if (isset($tree['children']))
    {
        foreach ($tree['children'] as $node)
        {
            $str .= $prefix.'--'.doit($node, $prefix);
            $str .= ( ! isset($node['children'])) ? "<br />\n" : '';
        }
    }
    
    return $str;
}

Hope this helps. You can always add it to your model if you want, but I'd probably keep it as a helper, since it's just formatting data, not actually retrieving any.