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

[eluser]Antivanity[/eluser]
Found the issue. It had to to with the debug_message function

Code:
function debug_message(&$message){
  ...
}

Passing the $message var by referance using & was causing the issue. I deleted the & and it works fine.

Would like to know why this caused an issue on my end.. kinda odd..
#72

[eluser]Antivanity[/eluser]
Thank you m4rw3r for writing this, you saved me countless hours! I did a quick test with 10,000 items and about 50 deep. The inserting took about 4 minutes, but when reading the data.. its deadly quick, very amazing!

Thanks again!
#73

[eluser]m4rw3r[/eluser]
That is the point with Nested Sets, fast retrieval but slower inserts/moves.

I have "abandoned" MPTtree for a while, because other projects eats a lot of my time (mainly my schoolwork and IgnitedRecord), but I will try to get some time after IgnitedRecord 1.0 is finished.
That is the reason why I haven't made any updates/bugfixes to MPTtree, because I have plans to make an almost complete rewrite.
#74

[eluser]sikkle[/eluser]
Code:
function tree2array($root = 1){

        $node = $this->get_node($root);
        if($node == false)
            return false;
        // query
        $query = $this->db->query('SELECT * FROM '.$this->tree_table.
            ' WHERE '.$this->left_col.' BETWEEN '.$node[$this->left_col].
            ' AND '.$node[$this->right_col].
            ' ORDER BY lft ASC');
            
        $right = array();
        $result = array();
        $current =& $result;
        $stack = array();
        $stack[0] =& $result;
        $lastlevel = 0;
        foreach($query->result_array() as $row){
            // go more shallow, if needed
            if(count($right)){
                while($right[count($right)-1] < $row[$this->right_col]){
                    array_pop($right);
                }
            }
            // Go one level deeper?
            if(count($right) > $lastlevel){
                end($current);
                $current[key($current)]['children'] = array();
                $stack[count($right)] =& $current[key($current)]['children'];
            }
            // the stack contains all parents, current and maybe next level
            $current =& $stack[count($right)];
            // add the data
            $current[] = $row;
            // go one level deeper with the index
            $lastlevel = count($right);
            $right[] = $row[$this->right_col];
        }
        return $result;
    }



You prolly remember that one, i try to think about the best way to sort those by title by recursive sub indeed.

You work on that kind of sorting with tree2array already ?

thanks a lot.
#75

[eluser]m4rw3r[/eluser]
No, because the thing with nested sets is that I cannot really order them by anything else than lft in SQL while maintaining the tree structure.

This is both good and bad, in your case it poses a problem, because you have to fetch them like you traverse an adjacency list, but the good thing is that it supports custom ordering by default.

I'm going to make a rewrite of MPTtree someday, but I'll be when I have time for it (currently I have work + school + IgnitedRecord, and it is a lot).
#76

[eluser]sikkle[/eluser]
By the way, did i miss something or i don't see a way to easyly move parent branche and below child above another parent branche ?

Or i missed something.

Just to confirm.

wish you the best m4rw3r and thanks a lot for sharing!

see ya around.
#77

[eluser]phpserver[/eluser]
[quote author="taewoo" date="1209940247"]@m4rw3r

I am fascinated by your example wiki ...
Is there a "fuller", more complete (i.e. more documented) version of your work that you might've posted somewhere?[/quote]

I may have found something.If you've got sometime,take a look at phpclasses.org.They ask for your nearest country,continent,even city.I have been wondering how they do that.Now i understand.
#78

[eluser]omerorhan[/eluser]
Hello I am new on Code Igniter.

I build a vertical menu like below.

Code:
function menu($lft = 1){
    $node = $this->MPTtree->get_ORM($lft);
    
    if($lft == 1)
    {
        $str = '<ul id="MenuBar1" class="MenuBarVertical">';
    }
    else
    {    
        $str = '<ul>';
    }
    
    foreach($node->children() as $child){
            $str .= "<li>";
            $str .= "<a href='#'>".$child->get('title')."</a>";
            if($child->count_children())
            {
                $str .= $this->menu($child->get('lft'));
            }
            
            $str .= "</li>";    
        }
        $str .= "</ul>";
    return $str;
    }
}

I wanna have two tables.

1 - Pages - Contains the page contents , names and etc.
2 - Page_list - For Mapping pages

I will build the ul list with the second table. (reference to MPTtree Model table)
But, I want to list pages name from the first table's name field.

I know I could join to first table.
But how??

Should i modify $this->MPTtree->get_ORM() method? or is there another way?
or what? and HOW?

I hope i could explain my problem.
#79

[eluser]omerorhan[/eluser]
I could get the detailed information of any record and make an array as like below.

Code:
$root = $this->MPTtree->get_root();
$this->db->select('descendant.title, detail.name');
$this->MPTtree->AR_from_descendants_of($root['lft'],$root['rgt']);
$this->db->from('second_table AS detail');
$this->db->where('descendant.id = detail.page_id');
$this->db->group_by('descendant.id');
$this->db->order_by('descendant.lft');
$query = $this->db->get();

$rows = $query->result_array();

echo "<pre>";
print_r($rows);
echo "</pre>";

But,
How could i make an unordered list from that array?
I know i am so closed, but i am confused a lot.

I've tried the examples on this threads , but not succeeded.

Code:
function menu($lft = 1){
  //How will i provide the structor here ??
}

Please help.
#80

[eluser]m4rw3r[/eluser]
Why not put the slug and title in the tree table? Because I think that is both easier on both performance and usage and it also seems more logical to me.

On how to make an ul from it, look at tree2array() and either use it, or adapt some code from it.




Theme © iAndrew 2016 - Forum software by © MyBB