Part of the EllisLab Network
   
3 of 14
3
MPTtree, Hieararchical trees in a database table
Posted: 17 May 2008 06:51 PM   [ Ignore ]   [ # 21 ]  
Lab Assistant
RankRank
Total Posts:  228
Joined  10-17-2006

First try to find the leaves:

select from tree where lft=rgt-

Then for each do

select from tree where visible=and lft<=$lft and rgt >=$rgt 

Then check if the lineage is correct, I don’t know a way to handle all you want in one query. This will select all visible parents. I usually store the parent_id in a column with mptt so I can derive whether a parent is missing. The other way would be to get the entire lineage of a leaf and then see with a loop in php whether all parents are visible.

I’m not sure it can be done any faster, maybe the more sql proficient know.

Profile
 
 
Posted: 20 May 2008 12:06 PM   [ Ignore ]   [ # 22 ]  
Lab Assistant
Avatar
RankRank
Total Posts:  124
Joined  04-16-2007

This looks great. Thanks for the contribution!

I wonder, is there some way of making the tree2array work nicely with CodeIgniters ol() or ul() HTML-helper?

 Signature 

EVERY TIME YOU ASK ABOUT CODEIGNITER 2.0… PHIL STURGEON KILLS A KITTEN

Profile
 
 
Posted: 30 May 2008 01:24 PM   [ Ignore ]   [ # 23 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  727
Joined  08-03-2006

Ok, been busy and now I’ve noticed your question when checking all suggestions (be patient, I’ll implement some of them), so here comes the answer:

No, I’m afraid not.

You have to either convert this:

array(
    
[0] => array(
        
'id' => '1',
        
'title' => 'A name',
        
'children' => array(
            
[0] => array(
                
'id' => '2',
                
'title' => 'Another name'
            
)
        )
    )

To

array('A name' => 'Another name'); 

Or you could use something like this:

function MPTtree_ul($array){
    $str 
'<ul>';
    foreach(
$array as $data)
        
$str .= '<li>';
        
$str .= '<a href="'.$data['id'].'">'.$data['title'].'</a>'// whatever you want between the <li> </li>
        
if(isset($data['children'])){
            $str 
.= MPTtree_ul($data['children']);
        
}
        $str 
.= '</li>';
    
}
    $str 
.= '</ul>';
    return 
$str;
 Signature 

RapidDataMapper: My new ORM, is now released!

IgnitedRecord: Old ORM

MPTtree: A model to handle trees in a database.

YAYParser - Yet Another YAML Parser

Profile
 
 
Posted: 30 May 2008 05:12 PM   [ Ignore ]   [ # 24 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  709
Joined  06-07-2007

Hey, I just understood the whole “MPTree” concept. I actually started implementing the same thing for the CodeExtinguisher site ( http://71.65.20.84:81/codexsite/index.php/docs/setup )

I remember you were interested in implementing this feature into CodeExtinguisher. If you still are, I’m willing to work through it with you. I haven’t actually tried MPTree yet, but it seems perfect for my use.

 Signature 

jtaby.com

Profile
 
 
Posted: 30 May 2008 05:57 PM   [ Ignore ]   [ # 25 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  727
Joined  08-03-2006

I think it would be a very nice addition, both to CodeExtinguisher and MPTtree, to have a good admin interface for storing tree data in a database.

I’m going to make some other additions to MPTtree and it would be nice to make a release “with” a full admin interface, so I’ll gladly work with you.

 Signature 

RapidDataMapper: My new ORM, is now released!

IgnitedRecord: Old ORM

MPTtree: A model to handle trees in a database.

YAYParser - Yet Another YAML Parser

Profile
 
 
Posted: 30 May 2008 05:58 PM   [ Ignore ]   [ # 26 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  709
Joined  06-07-2007

Cool, I can include this as a “view mode”. Take a look at the preview (in my signature), and see how there is the ability to use different view_modes? If you want more info on view_modes in the context of CodeExtinguisher, take a look at the temporary docs also linked to in my signature.

 Signature 

jtaby.com

Profile
 
 
Posted: 30 May 2008 06:08 PM   [ Ignore ]   [ # 27 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  727
Joined  08-03-2006

Ummm, the view_modes page in your wiki is empty.
But I think it’s not a bad idea. It could be useful with also a “flat” table manipulation option.

The things needed are a few buttons/drag-drop, etc.
Can view modes handle this in a nice way?

 Signature 

RapidDataMapper: My new ORM, is now released!

IgnitedRecord: Old ORM

MPTtree: A model to handle trees in a database.

YAYParser - Yet Another YAML Parser

Profile
 
 
Posted: 30 May 2008 06:17 PM   [ Ignore ]   [ # 28 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  709
Joined  06-07-2007

oh yeah, i thought I had written that page. In any case, yeah, each file in CodeExtinguisher can load its own assets.

 Signature 

jtaby.com

Profile
 
 
Posted: 30 May 2008 06:22 PM   [ Ignore ]   [ # 29 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  727
Joined  08-03-2006

That sounds fine.
Maybe we should continue this discussion via MSN or skype.

(While this is increasing our post count raspberry, I don’t feel like filling a whole thread with a conversation)

 Signature 

RapidDataMapper: My new ORM, is now released!

IgnitedRecord: Old ORM

MPTtree: A model to handle trees in a database.

YAYParser - Yet Another YAML Parser

Profile
 
 
Posted: 30 May 2008 06:24 PM   [ Ignore ]   [ # 30 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  709
Joined  06-07-2007

haha yes I don’t want to steal your thread, could you PM me your email? gmail, msn or skype work for me

 Signature 

jtaby.com

Profile
 
 
   
3 of 14
3