Welcome Guest, Not a member yet? Register   Sign In
hierarchy system in mysql with php
#1

[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
#2

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

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

[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.
#5

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

[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);
#7

[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?
#8

[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.
#9

[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.
#10

[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.




Theme © iAndrew 2016 - Forum software by © MyBB