PHP Classes

output only one branch

Recommend this page to a friend!

      Nested Nodes Class  >  All threads  >  output only one branch  >  (Un) Subscribe thread alerts  
Subject:output only one branch
Summary:starting to build the tree not at the root, but at a given node?
Messages:3
Author:Jurand Kaminski
Date:2007-06-27 15:31:20
Update:2007-08-02 15:30:47
 

  1. output only one branch   Reply   Report abuse  
Picture of Jurand Kaminski Jurand Kaminski - 2007-06-27 15:31:20
first of all thank you very much for this excellent class, you did a great job on that.

I have read the post about the browse_by_id(id) method, which will store the child elements of a given node in an array. My question is a little different:

is it possible to output the tree below a certain node with the same or similar methods as the whole tree is handled ?

The situation I'm in is the following:

the tree i'm talking about will have about 2000-4000 nodes
the software i'm working on is supposed to give different level of access to the users:

1. access to the whole tree
2. access to fragments of the tree
3. access to single folders.

Obviously the 1. and 3. assignments are easy ones. The 2 would be easy when operating with a tree organized with parent nodes. Now i wonder how this could be done using this method.

Basically it should be a function which takes a nodes id as a parameter and builds the tree's structure below this node much like the function:

$output = $nodes->html_output($ctg_id , true );

Any hint how to accomplish this would be welcome.

Thank you very much,
Jurand


  2. Re: output only one branch   Reply   Report abuse  
Picture of Dennis Dennis - 2007-07-30 19:50:59 - In reply to message 1 from Jurand Kaminski
Hello Jurand,

If you add another function to the class to get the parent-node from a certain node, you can easily achieve this. I added this functionality already:

<?php

function GetParent($id = 0)
{
// Get the position of a specific node
$thisPosition = $this->get_position($id);

// Turn this position into an array
$PositionS = explode(">" , $thisPosition);

// Remove the last two elements from this array ( 1e = empty, 2e = id specific node )
array_pop($PositionS);
array_pop($PositionS);

// If this remaining array is bigger then zero, then you know the next element isthe parent
if(count($PositionS) > 0)
{
// Save this element in a variable
$parent = array_pop($PositionS);
} else {

// The parent is equal to the root
$parent = '0';
}

return $parent;
}

?>

I hope this answers you're question so far
With kind regards,

Dennis


  3. Re: output only one branch   Reply   Report abuse  
Picture of S.Shaban S.Shaban - 2007-08-02 15:30:47 - In reply to message 1 from Jurand Kaminski
Hello Jurand,

here is a modified version of the html_output() function to echo a segment only of the categories.

<?php

// ********************************************************
// Build HTML output
// ********************************************************

function html_output($id=0 , $clickable = false)
{

if(!$clickable){
$tree = $this->build_list( 0 , 0 ); // display the full list
}else{
$tree = $this->build_list($id , $clickable); // display clickable list (one sub-level list)
}


$output = "\n<!-- Using OpenTag -->\n";
$output .= $this->HtmlTree["OpenTag"];




if(is_array($tree))
{

// lets fetch the top level

foreach($tree as $tmp){ if(!isset($topmax)) $topmax = $tmp['prefix']; if($tmp['prefix'] < $topmax) $topmax = $tmp['prefix']; }

$start = $topmax;
$next_loop_level = $topmax;
$tree = array_values($tree);

$end = count($tree);
for($i=0; $i<$end ;$i++)
{

$body = "";
$c = $tree[$i];
$i2 = $i + 1;

if($i2 < $end){
$next_loop = $tree[$i2];
$next_loop_level = $next_loop['prefix'];
}else $next_loop_level = $topmax;
// are we getting into sub-level the next loop ?

if( $next_loop_level > $c['prefix']){

if($c['prefix'] > $topmax){
// if so then lets use the LevelOpenTag
$body .= "\n<!-- Using LevelOpenTag -->\n";
if($c[$this->table_fields['id']] == $id) $body .= $this->HtmlTree['LevelOpenTagSelected'];
else $body .= $this->HtmlTree['LevelOpenTag'];
}else{
$body .= "\n<!-- Using FirstLevelOpenTag -->\n";
// we are on the roots.
if($c[$this->table_fields['id']] == $id) $body .= $this->HtmlTree['FirstLevelOpenTagSelected'];
else $body .= $this->HtmlTree['FirstLevelOpenTag'];
}
}elseif( $next_loop_level < $c['prefix'] AND $next_loop_level >= $topmax){


if($next_loop_level == $topmax && $c['prefix'] == $topmax + 1){
// we are on the roots.
$body .= "\n<!-- Using Node -->\n";
if($c['id'] == $id) $body .= $this->HtmlTree['NodeSelected'];
else $body .= $this->HtmlTree['Node'];
$body .= "\n<!-- Using FirstLevelCloseTag -->\n";
if($c['id'] == $id) $body .= $this->HtmlTree['FirstLevelCloseTagSelected'];
else $body .= $this->HtmlTree['FirstLevelCloseTag'];

}else{
// if so then lets use the LevelCloseTag
$body .= "\n<!-- Using Node -->\n";
if($c[$this->table_fields['id']] == $id) $body .= $this->HtmlTree['NodeSelected'];
else $body .= $this->HtmlTree['Node'];


for($j = $c['prefix']; $j > $next_loop_level ; $j--){
if($j == $topmax + 1){
$body .= "\n<!-- Using FirstLevelCloseTag -->\n";
if($c[$this->table_fields['id']] == $id) $body .= $this->HtmlTree['FirstLevelCloseTagSelected'];
else $body .= $this->HtmlTree['FirstLevelCloseTag'];
}else{
$body .= "\n<!-- Using LevelCloseTag -->\n";
if($c[$this->table_fields['id']] == $id) $body .= $this->HtmlTree['LevelCloseTagSelected'];
else $body .= $this->HtmlTree['LevelCloseTag'];
}
}

}

}else{
// neither getting in or out of a level .. use the normal node tags
$body .= "\n<!-- Using Node -->\n";
if($c[$this->table_fields['id']] == $id) $body .= $this->HtmlTree['NodeSelected'];
else $body .= $this->HtmlTree['Node'];

}

foreach($c as $key => $value)
{
$body = str_replace("[$key]" ,$value, $body);
}

$next_loop_level--;

$output .= $body;

}
}

$output .= "\n<!-- Using CloseTag -->\n";
$output .= $this->HtmlTree['CloseTag'];
return $output;
}

?>



just replace yours, and to print a list of childs under a parent category ... lets say the parent id is "11", just use the sql_condition feature
<?php

$nodes->sql_condition = "AND category_position RLIKE '^11>.*' "; // << note the id 11 , change it to yours
echo $nodes->html_output();

?>

now you have the tree starting from 11 as the top category,

let me know if this is what you want,
Regards,