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

[eluser]omerorhan[/eluser]
Because i didn't get the logic well.
I thought its better to store details of the tree in another table on a multiple language supported site.

or ,should I add a lang_id field to tree and filter it?

I modified tree2array() to make ul list.

Code:
function tree2array($root = 1){
        $node = $this->MPTtree->get_node($root);

        if($node == false)
            return false;
        // query
        $query = $this->db->query('SELECT node.id, node.lft, node.rgt , detail.title
             FROM table1 as node,
             table1 as parent ,
             table1_details AS detail
             where node.lft BETWEEN '.$node['lft'].' AND  '.$node['rgt'].'
             and node.id = detail.page_id
             Group by node.id
             ORDER BY node.lft');

        $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['rgt']){
                    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['rgt'];
        
        }
        return $result[0]['children'];
    }

Now i had to make a select list for admin panel.

Code:
$levels = $this->getLevels();
$data['tree'] = $this->cat_select($this->tree2array(),$levels);


function cat_select($array , $levels, $lft = 1){
    if($lft == 1)
    {
        echo "<select name=\"where\">";
    }
                foreach($array as $data)
                {
                    $indent = (str_repeat("&nbsp;", $this->getLevel($levels,$data['lft'])*2));
                    
                    echo '<option value="'.$data['id'].'">'.$indent.$data['title'].'</option>';    
                    
                    if(isset($data['children'])){
                        echo  $this->cat_select($data['children'], $levels , 0);
                    }        
                }
        echo "</select>";
}

function getLevels()
{
    $sql = "select node.id, node.lft, node.title, (COUNT(parent.title) -1) as depth
            From deneme as node,
            deneme as parent
            Where node.lft BETWEEN parent.lft AND parent.rgt
            GROUP BY node.id
            Order BY node.lft
            ";
    $query = $this->db->query($sql);
    
    $result = $query->result_array();
    
    return $result;
}
function getLevel($array, $lft)
{
        foreach($array as $data)
        {
            if($data['lft'] == $lft)
            {
                $depth = $data['depth'];
            }
        }
        if(isset($depth))
        {
            return $depth;
        }
        else
        {
            return;
        }
}

What is wrong in cat_select() function?

It doesn't add options in select box after first leaf ends.


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