Third level of sub navigation php - php

I have the following code which works perfectly for producing a two tier navigation system, the problem is I have a requirement where one section has a third level of pages below it.
Edit: The code produces a two tier side navigation item that lists the pages in this section along with the parent item detailed at the top like so:
Page Title
Sub Page 1
Sub Page 2
Another Sub Page 1
Further Sub Page 1
Another Sub Page 1
Sub Page 3
There isn't any abnormal behaviour or error messages, it fails to display the further sub page 1 items in the list.
function insection_make_ul($tree, $level=0) {
$indent = str_repeat(" ", $level);
$result = "\n".$indent."<ul>\n";
foreach($tree as $id => $item) {
$result .= $indent."<li><a href=\"".$item['permalink']."\" class=\""
.($item['selected'] == true ? 'selected' : '')
.($level == 0 ? ' root' : '')."\" >" . $item['title']."</a>";
if(count(#$item['items'])) {
$result .= insection_make_ul($item['items'], ($level+1));
$result .= $indent."</li>\n";
}else{
$result .= "</li>\n";
}
}
$result .= $indent."</ul>\n";
return $result;
}
function insection($structure_id,$custom_selected=false){
$tree = insection_array($structure_id,$custom_selected);
return insection_make_ul($tree);
}
and the code to build the array
function insection_array($data,$custom_selected=false){
global $link;
if(is_numeric($data))
$data = fetch_row('SELECT * FROM content_structure WHERE id = '.$data);
$selected_id = $data['id'];
if($custom_selected) // dynamic item of 'real' parent
$selected_id .= '_'.$custom_selected;
$insection = array();
if($data['parent_id'] > 0){
if(HIDE_EMPTY_STRUCTURE){
$sql = 'SELECT * FROM content_structure WHERE parent_id = '.$data['id'].' AND visible = 1 AND in_menu = 1
AND (item_id > 0 OR redirect <> "")';
}else{
$sql = 'SELECT * FROM content_structure WHERE parent_id = '.$data['id'].' AND visible = 1 AND in_menu = 1';
}
$result = mysqli_query($link, $sql);
if(mysqli_num_rows($result) > 0 || $data['children_php'] != ''){
$parent_id = $data['id'];
}else{
$parent_id = $data['parent_id'];
}
}else{
$parent_id = $data['id'];
}
while($parent_id > 0){
$data = fetch_row('SELECT * FROM content_structure WHERE id = '.$parent_id);
$insection[$parent_id] = array('id' => $data['id'],
'title' => $data['menu_title'],
'permalink' => navlink($data),
'selected' => ($data['id'] == $selected_id ? true : false) );
if(HIDE_EMPTY_STRUCTURE){
$sql = 'SELECT * FROM content_structure WHERE parent_id = '.$parent_id.' AND visible = 1 AND in_menu = 1
AND (item_id > 0 OR redirect <> "") ORDER BY '
.($data['sort_auto'] == 1 ? 'menu_title' : 'sort_order');
}else{
$sql = 'SELECT * FROM content_structure WHERE parent_id = '.$parent_id.' AND visible = 1 AND in_menu = 1 ORDER BY '
.($data['sort_auto'] == 1 ? 'menu_title' : 'sort_order');
}
$result = mysqli_query($link, $sql);
if(!$result){ die('error: '.mysqli_error($link)); }
while($row = mysqli_fetch_assoc($result)){
$insection[$parent_id]['items'][$row['id']] = array('id' => $row['id'],
'title' => $row['menu_title'],
'permalink' => navlink($row),
'selected' => ($row['id'] == $selected_id ? true : false) );
}
// custom start
if($data['children_php'] != ''){ // custom sub items?
$sub_item_result = custom_navigation_array($data['children_php']);
foreach($sub_item_result as $sub_item){
$id = $data['id'].'_'.$sub_item['id']; // realparent_customid
$insection[$parent_id]['items'][$id] = array('id' => $id,
'title' => $sub_item['menu_title'],
'permalink' => $sub_item['href'],
'selected' => ($id == $selected_id ? true : false) );
}
}
//custom end
$parent_id = $data['parent_id'];
}
$insection = array_reverse($insection,true);
$temp = current($insection);
$root_id = #$temp['id'];
$insection_tree[$root_id] = current($insection);
$found_selected = false;
if(is_array(#$insection_tree[$root_id]['items'])){
foreach($insection_tree[$root_id]['items'] as $id => $item){
if(!empty($insection[$id])){
if($insection_tree[$root_id]['items'][$id]['selected'] == true)
$found_selected = true;
$insection_tree[$root_id]['items'][$id] = $insection[$id];
}
}
}
//if(!$found_selected){
// while(!$found_selected){
//
// }
//}
return $insection_tree;
}
Any pointers where I might get this working.
thanks

Personally I'd advise re-looking at your code. There is a lot of code there doing the same thing. Repetition is bad. As a helping hand, here's something to get you going.
Take this menu structure as an example but It's really upto you to produce this array yourself the main thing to notice is the function which will build the array into a <ul><li> string.
$menuItems = array(
array(// Top level items
"name" => "item1",
"subs" => array(// Second Level items
array(
"name" => "1a",
"href" => "something"
),
array(
"name" => "1b",
"subs" => array(// Third Level Items
array("name" => "1ba", "href" => "something"),
array("name" => "1bb", array(
array("name" => "1cca", "href" => "something"),
)
)
)
)
)
),
array(// Top level items
"name" => "item2",
"subs" => array(// Second Level items
array(
"name" => "2a",
"href" => "something"
),
array(
"name" => "2b",
"subs" => array(// Third Level Items
array("name" => "2ba", "href" => "something"),
array("name" => "2bb", array(
array("name" => "2cca", "href" => "something"),
)
)
)
)
)
)
);
$this->menuIterator($menuItems);
die();
The following logic is the important bit, it'll mean your menu could be any levels deep you want and it'll still produce the same result:
public function menuIterator($items) {
print "<ul>";
foreach ($items as $item) {
print "<li>";
print "<a>{$item['name']}</a>";
if (isset($item['subs'])) {
$this->menuIterator($item['subs']);
}
print "</li>";
}
print "</ul>";
return;
}
And the result is:
<ul><li><a>item1</a><ul><li><a>1a</a></li><li><a>1b</a><ul><li><a>1ba</a></li><li><a>1bb</a></li></ul></li></ul></li><li><a>item2</a><ul><li><a>2a</a></li><li><a>2b</a><ul><li><a>2ba</a></li><li><a>2bb</a></li></ul></li></ul></li></ul>

Related

Building a string Concat array with the same ID inside a query loop

i have a query inside a for loop that getting the product name of every array element. Now in every element of my array, i have an ID, where i want to concat all product names with the-same shipping_id.
Here i have my array with values like these:
Array name:id with values of:
Array
(
[0] => Array
(
[product_id] => 1
[shipping_id] => 1
)
[1] => Array
(
[product_id] => 2
[shipping_id] => 1
)
[2] => Array
(
[product_id] => 1
[shipping_id] => 2
)
)
now i made this code with these:
$first = true;
$temp_ship_id = "";
$product_list = "";
foreach ($ids as $product) {
$productname = $this->getproductname($product[0][product_id]);
// if($first) {
// $temp_ship_id = $product[0][shipping_id];
// $first = false;
// }
// if($product[0][shipping_id] == $temp_ship_id) {
// $product_list .= $productname.";
// } else {
// $product_list .= $productname.";
// //$product_list = "";
// $temp_ship_id = $product[0]->shipping_id;
// }
}
public function getproductname($product_id) {
$product = DB::table('products')->select('product_name')
->where(['products.product_id'=>$product_id])
->first();
return $product->product_name;
}
what am i doing is, i am getting the first shipping id and store it and i made a condition if they are thesame then i go concat the productname but, i see my logic is bad.
Please help me in other way. Something like This line of code to begin with:
foreach ($ids as $product) {
$productname = $this->getproductname($product[0][product_id]);
//code for concat goes here
}
public function getproductname($product_id) {
$product = DB::table('products')->select('product_name')
->where(['products.product_id'=>$product_id])
->first();
return $product->product_name;
}
Adjust below to your actual data, let me know if you have questions.
<?php
$concat = array();
$array = array( array( 'product_id'=>1, 'shipping_id'=>1, 'product_name' => 'a' ), array( 'product_id'=>2, 'shipping_id'=>1, 'product_name' => 'b' ), array( 'product_id'=>3, 'shipping_id'=>2, 'product_name' => 'c' ), array( 'product_id'=>4, 'shipping_id'=>1, 'product_name' => 'd' ) );
foreach( $array as $row ) {
if( isset( $concat[ $row['shipping_id'] ] ) ) {
$concat[ $row['shipping_id'] ] .= ',' . $row['product_name'];
} else {
$concat[ $row['shipping_id'] ] .= $row['product_name'];
}
}
var_dump( $concat );
?>

How can I fix this query to return only items with a certain value in its subarray?

I'm trying to modify a voting script(Thumbsup) I need this query to only return only array items that have their 'cat' => '1' in the subarray (proper term?)
<?php $items = ThumbsUp::items()->get() ?>
Here's an example of the live generated array from my database
array (
0 =>
array (
'id' => 1,
'name' => 'a',
'cat' => '1',
),
1 =>
array (
'id' => 2,
'name' => 'b',
'cat' => '2',
),
2 =>
array (
'id' => 3,
'name' => 'c',
'cat' => '2',
),
)
Is this possible by just modifying the query?
edit: heres function get()
public function get()
{
// Start building the query
$sql = 'SELECT id, name, url, cat, closed, date, votes_up, votes_down, ';
$sql .= 'votes_up - votes_down AS votes_balance, ';
$sql .= 'votes_up + votes_down AS votes_total, ';
$sql .= 'votes_up / (votes_up + votes_down) * 100 AS votes_pct_up, ';
$sql .= 'votes_down / (votes_up + votes_down) * 100 AS votes_pct_down ';
$sql .= 'FROM '.ThumbsUp::config('database_table_prefix').'items ';
// Select only either open or closed items
if ($this->closed !== NULL)
{
$where[] = 'closed = '.(int) $this->closed;
}
// Select only either open or closed items
if ($this->name !== NULL)
{
// Note: substr() is used to chop off the wrapping quotes
$where[] = 'name LIKE "%'.substr(ThumbsUp::db()->quote($this->name), 1, -1).'%"';
}
// Append all query conditions if any
if ( ! empty($where))
{
$sql .= ' WHERE '.implode(' AND ', $where);
}
// We need to order the results
if ($this->orderby)
{
$sql .= ' ORDER BY '.$this->orderby;
}
else
{
// Default order
$sql .= ' ORDER BY name ';
}
// A limit has been set
if ($this->limit)
{
$sql .= ' LIMIT '.(int) $this->limit;
}
// Wrap this in an try/catch block just in case something goes wrong
try
{
// Execute the query
$sth = ThumbsUp::db()->prepare($sql);
$sth->execute(array($this->name));
}
catch (PDOException $e)
{
// Rethrow the exception in debug mode
if (ThumbsUp::config('debug'))
throw $e;
// Otherwise, fail silently and just return an empty item array
return array();
}
// Initialize the items array that will be returned
$items = array();
// Fetch all results
while ($row = $sth->fetch(PDO::FETCH_OBJ))
{
// Return an item_id => item_name array
$items[] = array(
'id' => (int) $row->id,
'name' => $row->name,
'url' => $row->url,
'cat' => $row->cat,
'closed' => (bool) $row->closed,
'date' => (int) $row->date,
'votes_up' => (int) $row->votes_up,
'votes_down' => (int) $row->votes_down,
'votes_pct_up' => (float) $row->votes_pct_up,
'votes_pct_down' => (float) $row->votes_pct_down,
'votes_balance' => (int) $row->votes_balance,
'votes_total' => (int) $row->votes_total,
);
}
return $items;
}
thanks.
You can easily modify "get()" to add the desired functionality:
public function get($cat = null)
{
$where = array();
if ($cat !== null) {
$where[] = 'cat = '. (int) $cat;
}
// ... original code ...
}
Usage:
$items = ThumbsUp::items()->get(1);

Zend_Db nested queries where one query needs the ID from the other

I am fairly new to Zend Framework and have searched for days without finding an example for the following issue:
The result I am trying to achieve from the code below is a list that would have the main Category with the matching sub categories beneath it and loop through for all my main categories.
Example:
Appliances (Main Category)
Microwave
Stove
Electronics (Main Category)
Computer
Radio
Here is the code (again I have no idea how to do this; my thought process was to do nested foreach(), with the second foreach() getting the main category id from the first select):
// Get Categories with Sub Categories
$catid = 0;
$selectmain = $this->dbhInstance->select()
->from(array('a' => 'code_sub_category'),
array('b.id as mainid', 'b.site_category'))
->join(array('b' => 'site_categories'),
'b.id = a.site_category_id')
->group("b.id")
->order("b.site_category");
$selectsub = $this->dbhInstance->select()
->from(array('a' => 'code_sub_category'),
array('a.id as subid', 'a.sub_category', 'a.site_category_id'))
->join(array('b' => 'site_categories'),
'b.id = a.site_category_id')
->where("a.site_category_id = '" . $catid . "'")
->order("a.sub_category");
$fetch = $this->dbhInstance->fetchAll($selectmain);
$fetch2 = $this->dbhInstance->fetchAll($selectsub);
//var_dump($fetch2);
$items = array();
$items2 = array();
foreach ($fetch as $key => $value) {
$catid = $value['id'];
$items = array_merge($items, array($key => $value));
foreach ($fetch2 as $key => $value) {
$items = array_merge($items, array($key => $value));
}
$this->view->getsubcategories = $items;
}
$this->view->getmaincategories = $items;
//End FULL Categories for My Categories
Can you try this :
$selectCat = $this->dbhInstance->select()
->from(array('c' => 'site_categories'), array('c.id as id', 'c.site_category as text, 0 as parent'))
->order('c.site_category');
$selectSubCat = $this->dbhInstance->select()
->from(array('sc' => 'code_sub_category'), array('sc.id as id', 'sc.sub_category as text', 'sc.site_category_id as parent')
->order('sc.sub_category');
$select = $this->dbhInstance->select()->union(array($selectCat, $selectSubCat));
$statement = $this->dbhInstance->query($select);
$items = array();
$childs = array();
while( $row = $statement->fetch() ) {
$items[] = (object)array(
'id' => $row['id'],
'text' => $row['text'],
'parent' => $row['parent'],
'item' => array()
);
}
foreach($items as $item)
$childs[$item->parent][] = $item;
foreach($items as $item) if (isset($childs[$item->id]))
$item->item = $childs[$item->id];
if( !isset($childs[0]) ) $this->view->getmaincategories = $getmaincategories = array();
else
$this->view->getmaincategories = $getmaincategories = $childs[0];
// Display the result
Zend_Debug::dump($getmaincategories);
The result will be an array of objects, something like this :
array(
'Appliances' => array(
'Microwave',
'Stove'
),
'Electronics' => array(
'Computer',
'Radio'
)
)
Hope this help!
Regards,
Ahmed.

How can I build a nested HTML list with an infinite depth from a flat array?

I'm trying to produce a multi-level HTML list from a source array that is formatted like this:
/**
* id = unique id
* parent_id = "id" that this item is directly nested under
* text = the output string
*/
$list = array(
array(
'id' => 1,
'parent_id' => 0,
'text' => 'Level 1',
), array(
'id' => 2,
'parent_id' => 0,
'text' => 'Level 2',
), array(
'id' => 3,
'parent_id' => 2,
'text' => 'Level 2.1',
), array(
'id' => 4,
'parent_id' => 2,
'text' => 'Level 2.2',
), array(
'id' => 5,
'parent_id' => 4,
'text' => 'Level 2.2.1',
), array(
'id' => 6,
'parent_id' => 0,
'text' => 'Level 3',
)
);
The goal is a nested <ul> with an infinite depth. The expected output of the array above is this:
Level 1Level 2Level 2.1Level 2.2Level 2.2.1Level 3
If only the array items had a key called child or something that contained the actual sub-array, it would be easy to recurse though these and get the desired output with a function like this:
function makeList($list)
{
echo '<ul>';
foreach ($list as $item)
{
echo '<li>'.$item['text'];
if (isset($item['child']))
{
makeList($item['child']);
}
echo '</li>';
}
echo '</ul>';
}
Unfortunately that's not the case for me - the format of the source arrays can't be changed. So, long ago I wrote this very nasty function to make it happen, and it only works up to three levels (code is pasted verbatim with original comments). I know it's a long boring read, please bear with me:
function makeArray($links)
{
// Output
$nav = array();
foreach ($links as $k => $v)
{
// If no parent_id is present, we can assume it is a top-level link
if (empty($v['parent_id']))
{
$id = isset($v['id']) ? $v['id'] : $k;
$nav[$id] = $v;
// Remove from original array
unset($links[$k]);
}
}
// Loop through the remaining links again,
// we can assume they all have a parent_id
foreach ($links as $k => $v)
{
// Link's parent_id is in the top level array, so this is a level-2 link
// We already looped through every item so we know they are all accounted for
if (isset($nav[$v['parent_id']]))
{
$id = isset($v['id']) ? $v['id'] : $k;
// Add it to the top level links as a child
$nav[$v['parent_id']]['child'][$id] = $v;
// Set a marker so we know which ones to loop through to add the third level
$nav2[$id] = $v;
// Remove it from the array
unset($links[$k]);
}
}
// Last iteration for the third level
// All other links have been removed from the original array at this point
foreach ($links as $k => $v)
{
$id = isset($v['id']) ? $v['id'] : $k;
// Link's parent_id is in the second level array, so this is a level-3 link
// Orphans will be ignored
if (isset($nav2[$v['parent_id']]))
{
// This part is crazy, just go with it
$nav3 = $nav2[$v['parent_id']]['parent_id'];
$nav[$nav3]['child'][$v['parent_id']]['child'][] = $v;
}
}
return $nav;
}
This makes an array like:
array(
'text' => 'Level 1'
'child' => array(
array(
'text' => 'Level 1.2'
'child' => array(
array(
'text' => 'Level 1.2.1'
'child' => array(
// etc.
),
array(
'text' => 'Level 1.2.2'
'child' => array(
// etc.
),
)
)
)
)
);
Usage:
$nav = makeArray($links);
makeList($nav);
I've spent many spare hours trying to work this out, and the original code which I have given here is still the best solution I've been able to produce.
How can I make this happen without that awful function (which is limited to a depth of 3), and have an infinite number of levels? Is there a more elegant solution to this?
Print:
function printListRecursive(&$list,$parent=0){
$foundSome = false;
for( $i=0,$c=count($list);$i<$c;$i++ ){
if( $list[$i]['parent_id']==$parent ){
if( $foundSome==false ){
echo '<ul>';
$foundSome = true;
}
echo '<li>'.$list[$i]['text'].'</li>';
printListRecursive($list,$list[$i]['id']);
}
}
if( $foundSome ){
echo '</ul>';
}
}
printListRecursive($list);
Create multidimensional array:
function makeListRecursive(&$list,$parent=0){
$result = array();
for( $i=0,$c=count($list);$i<$c;$i++ ){
if( $list[$i]['parent_id']==$parent ){
$list[$i]['childs'] = makeListRecursive($list,$list[$i]['id']);
$result[] = $list[$i];
}
}
return $result;
}
$result = array();
$result = makeListRecursive($list);
echo '<pre>';
var_dump($result);
echo '</pre>';
Tested and working :)
$list = array(...);
$nested = array();
foreach ($list as $item)
{
if ($item['parent_id'] == 0)
{
// Woot, easy - top level
$nested[$item['id']] = $item;
}
else
{
// Not top level, find it's place
process($item, $nested);
}
}
// Recursive function
function process($item, &$arr)
{
if (is_array($arr))
{
foreach ($arr as $key => $parent_item)
{
// Match?
if (isset($parent_item['id']) && $parent_item['id'] == $item['parent_id'])
{
$arr[$key]['children'][$item['id']] = $item;
}
else
{
// Keep looking, recursively
process($item, $arr[$key]);
}
}
}
}
Some methods I recently wrote, maybe some will help, sorry I'm short on time and cannot rewite them to match your needs.
This code is actually a part of Kohana Framework Model, method ->as_array() is used to flat an Database_Result object.
function array_tree($all_nodes){
$tree = array();
foreach($all_nodes as $node){
$tree[$node->id]['fields'] = $node->as_array();
$tree[$node->id]['children'] = array();
if($node->parent_id){
$tree[$node->parent_id]['children'][$node->id] =& $tree[$node->id];
}
}
$return_tree = array();
foreach($tree as $node){
if($node['fields']['depth'] == 0){
$return_tree[$node['fields']['id']] = $node;
}
}
return $return_tree;
}
array_tree() is used to make a tree out of a flat array. The key feature is the =& part ;)
function html_tree($tree_array = null){
if( ! $tree_array){
$tree_array = $this -> array_tree();
}
$html_tree = '<ul>'."\n";
foreach($tree_array as $node){
$html_tree .= $this->html_tree_crawl($node);
}
$html_tree .= '</ul>'."\n";
return $html_tree;
}
function html_tree_crawl($node){
$children = null;
if(count($node['children']) > 0){
$children = '<ul>'."\n";
foreach($node['children'] as $chnode){
$children .= $this->html_tree_crawl($chnode);
}
$children .= '</ul>'."\n";
}
return $this->html_tree_node($node, $children);
}
html_tree_node() is a simple method to display current node and children in HTML.
Example below:
<li id="node-<?= $node['id'] ?>">
<?= $node['title'] ?>
<?= (isset($children) && $children != null) ? $children : '' ?>
</li>

Getting nested set model into a <ul> but hiding "closed" subtrees

Based on Getting a modified preorder tree traversal model (nested set) into a <ul>
One of answers gave right code to display full tree. What i need is to always show first level (depth=0) and siblings+childrens for active list item. Goal is to expand visible part of tree when user selects list item which is parent for more list items.
So, if i got this list:
1. item
2. item
2.1. item
2.2. item
2.2.1. item
2.2.2. item
2.2.3. item
2.3. item
2.4. item
2.4.1. item
2.4.2. item
3. item
4. item
4.1. item
4.2. item
4.2.1. item
4.2.2. item
5. item
and if current list item is "2.", list should look like that:
1. item
2. item // this needs class .selected
2.1. item
2.2. item
2.3. item
2.4. item
3. item
4. item
5. item
and if current list item is "2.2.", list should look like that:
1. item
2. item // this needs class .selected
2.1. item
2.2. item // this needs class .selected
2.2.1. item
2.2.2. item
2.2.3. item
2.3. item
2.4. item
3. item
4. item
5. item
Below there is an example code which works well for me to display full tree. I also added lft/rgt/current which will be needed to solve my issue.
<?php
function MyRenderTree ( $tree = array(array('name'=>'','depth'=>'', 'lft'=>'','rgt'=>'')) , $current=false){
$current_depth = 0;
$counter = 0;
$result = '<ul>';
foreach($tree as $node){
$node_depth = $node['depth'];
$node_name = $node['name'];
$node_id = $node['category_id'];
if($node_depth == $current_depth){
if($counter > 0) $result .= '</li>';
}
elseif($node_depth > $current_depth){
$result .= '<ul>';
$current_depth = $current_depth + ($node_depth - $current_depth);
}
elseif($node_depth < $current_depth){
$result .= str_repeat('</li></ul>',$current_depth - $node_depth).'</li>';
$current_depth = $current_depth - ($current_depth - $node_depth);
}
$result .= '<li id="c'.$node_id.'"';
$result .= $node_depth < 2 ?' class="open"':'';
$result .= '>'.$node_name.'';
++$counter;
}
$result .= str_repeat('</li></ul>',$node_depth).'</li>';
$result .= '</ul>';
return $result;
}
// "$current" may contain category_id, lft, rgt for active list item
print MyRenderTree($categories,$current);
?>
As you already managed to sort the sequence, why not just output as needed?
As some leafs need to appear closed, so the iterator should be able to skip children of non-selected nodes.
Doing so lead me to an idea to solve the problem of terminating the output tree (output = parsing). What to do if the last valid node in the sequence is at a higher depth than 0? I appended a NULL terminator for that. So still open levels can be closed before the loop finishes.
Additionally the iterator overloads nodes to offer common methods on them, like comparing against the currently selected element.
The MyRenderTree function (Demo/Full code)
Edit: The Demo Codepad has problems, here is the source-code: Gist
Getting nested set model into a but hiding “closed” subtrees
function MyRenderTree($tree = array(array('name'=>'','depth'=>'', 'lft'=>'','rgt'=>'')) , $current=false)
{
$sequence = new SequenceTreeIterator($tree);
echo '<ul>';
$hasChildren = FALSE;
foreach($sequence as $node)
{
if ($close = $sequence->getCloseLevels())
{
echo str_repeat('</ul></li>', $close);
$hasChildren = FALSE;
}
if (!$node && $hasChildren)
{
echo '</li>', "\n";
}
if (!$node) break; # terminator
$hasChildren = $node->hasChildren();
$isSelected = $node->isSupersetOf($current);
$classes = array();
$isSelected && ($classes[] = 'selected') && $hasChildren && $classes[] = 'open';
$node->isSame($current) && $classes[] = 'current';
printf('<li class="%s">%s', implode(' ', $classes), $node['name']);
if ($hasChildren)
if ($isSelected)
echo '<ul>';
else
$sequence->skipChildren()
;
else
echo '</li>'
;
}
echo '</ul>';
}
This can be solved as well in a single foreach and some variables, however I think for re-useablilty, the implementation based on the SPL Iterators is better.
Instead of using PHP script for handling tree navigation, Jquery can be used.
Once the tree is generated rest of the things will handled on client itself, it will also save server requests.
See Sample 2 and 3
http://jquery.bassistance.de/treeview/demo/
http://docs.jquery.com/Plugins/Treeview
It may help as per your requirement.
The function expect the $tree is order by the 'left'.
I have modified your function to selected items based on the 'left' and 'right' value. Hope it's what you are after.
The modified function:
function MyRenderTree($tree = array(array('name' => '', 'depth' => '', 'lft' => '', 'rgt' => '')), $current=false)
{
$current_depth = 0;
$counter = 0;
$found = false;
$nextSibling = false;
$result = '<ul>';
foreach ($tree as $node) {
$node_depth = $node['depth'];
$node_name = $node['name'];
$node_id = 1;//$node['category_id'];
if ($current !== false) {
if ($node_depth ==0) {
if ($node['lft'] <= $current['lft'] && $node['rgt'] >= $current['rgt']) {
// selected root item
$root = $node;
}
} else if (!isset($root)) {
// skip all items that are not under the selected root
continue;
} else {
// when selected root is found
$isInRange = ($root['lft'] <= $node['lft'] && $root['rgt'] >= $node['rgt']);
if (!$isInRange) {
// skip all of the items that are not in range of the selected root
continue;
} else if (isset($current['lft']) && $node['lft'] == $current['lft']) {
// selected item reached
$found = true;
$current = $node;
} else if ($nextSibling !== false && $nextSibling['depth'] < $node['depth']) {
// if we have siblings after the selected item
// skip any other childerns in the same range or the selected root item
continue;
} else if ($found && $node_depth == $node['depth']) {
// siblings after the selected item
$nextSibling = $node;
}
}
} else if ($node_depth > 0) {
// show root items only if no childern is selected
continue;
}
if ($node_depth == $current_depth) {
if ($counter > 0)
$result .= '</li>';
}
elseif ($node_depth > $current_depth) {
$result .= '<ul>';
$current_depth = $current_depth + ($node_depth - $current_depth);
} elseif ($node_depth < $current_depth) {
$result .= str_repeat('</li></ul>', $current_depth - $node_depth) . '</li>';
$current_depth = $current_depth - ($current_depth - $node_depth);
}
$result .= '<li id="c' . $node_id . '" ';
$result .= $node_depth < 2 ?' class="open"':'';
$result .= '>' . $node_name .'(' . $node['lft'] . '-' . $node['rgt'] . ')' . '';
++$counter;
}
unset($found);
unset($nextSibling);
$result .= str_repeat('</li></ul>', $node_depth) . '</li>';
$result .= '</ul>';
return $result;
}
Usage:
$categories = array(
array('name' => '1. item',
'depth' => '0',
'lft' => '1',
'rgt' => '2'),
array('name' => '2. item',
'depth' => '0',
'lft' => '3',
'rgt' => '22'),
array('name' => '2.1 item',
'depth' => '1',
'lft' => '4',
'rgt' => '5'),
array('name' => '2.2 item',
'depth' => '1',
'lft' => '6',
'rgt' => '13'),
array('name' => '2.2.1 item',
'depth' => '2',
'lft' => '7',
'rgt' => '8'),
array('name' => '2.2.2 item',
'depth' => '2',
'lft' => '9',
'rgt' => '10'),
array('name' => '2.2.3 item',
'depth' => '2',
'lft' => '11',
'rgt' => '12'),
array('name' => '2.3 item',
'depth' => '1',
'lft' => '14',
'rgt' => '15'),
array('name' => '2.4 item',
'depth' => '1',
'lft' => '16',
'rgt' => '21'),
array('name' => '2.4.1 item',
'depth' => '2',
'lft' => '17',
'rgt' => '18'),
array('name' => '2.4.2 item',
'depth' => '2',
'lft' => '19',
'rgt' => '20'),
array('name' => '3. item',
'depth' => '0',
'lft' => '23',
'rgt' => '24'),
array('name' => '4. item',
'depth' => '0',
'lft' => '25',
'rgt' => '34'),
array('name' => '4.1 item',
'depth' => '1',
'lft' => '26',
'rgt' => '27'),
array('name' => '4.2 item',
'depth' => '1',
'lft' => '28',
'rgt' => '33'),
array('name' => '4.2.1 item',
'depth' => '2',
'lft' => '29',
'rgt' => '30'),
array('name' => '4.2.2 item',
'depth' => '2',
'lft' => '31',
'rgt' => '32',
'category_id' => 5),
array('name' => '5. item',
'depth' => '0',
'lft' => '35',
'rgt' => '36'),
);
$current = array('lft' => '9', 'rgt' => '10');
print MyRenderTree($categories, $current);
http://www.jstree.com/ is a jQuery plugin which will handle this for you far more elegantly and quickly than trying to do a PHP based solution.
Check out http://www.jstree.com/demo for a live demo and instruction on how tom implement.
Based on answer by satrun77. I created a helper for symfony + doctrine + nestedset (http://www.doctrine-project.org/projects/orm/1.2/docs/manual/hierarchical-data/en):
function render_tree_html_list($nodes, Doctrine_Record $current_node, $render = true) {
$html = '';
$current_node_level = $current_node->getLevel();
$counter = 0;
$found = false;
$nextSibling = false;
foreach ($nodes as $i => $node):
$node_level = $node->getLevel();
$node_name = $node->getTitulo();
$node_id = $node->getId();
if ($current_node !== false) {
if ($node_level == 0) {
if ($node->getLft() <= $current_node->getLft() && $node->getRgt() >= $current_node->getRgt()) {
// selected root item
$root = $node;
}
} else if (!isset($root)) {
// skip all items that are not under the selected root
continue;
} else {
// when selected root is found
$isInRange = ($root->getLft() <= $node->getLft() && $root->getRgt() >= $node->getRgt());
if (!$isInRange) {
// skip all of the items that are not in range of the selected root
continue;
} else if ($current_node->getLft() && $node->getLft() == $current_node->getLft()) {
// selected item reached
$found = true;
$current_node = $node;
} else if ($nextSibling !== false && $nextSibling->getLevel() < $node->getLevel()) {
// if we have siblings after the selected item
// skip any other childerns in the same range or the selected root item
continue;
} else if ($found && $node_level == $node->getLevel()) {
// siblings after the selected item
$nextSibling = $node;
}
}
} else if ($node_level > 0) {
// show root items only if no childern is selected
continue;
}
if ($node_level == $current_node_level) {
if ($counter > 0)
$html .= '</li>';
}
elseif ($node_level > $current_node_level) {
$html .= '<ol>';
$current_node_level = $current_node_level + ($node_level - $current_node_level);
} elseif ($node_level < $current_node_level) {
$html .= str_repeat('</li></ol>', $current_node_level - $node_level) . '</li>';
$current_node_level = $current_node_level - ($current_node_level - $node_level);
}
$html .= sprintf('<li node="%d" class="%s"><div>%s</div>',
$node_id,
(isset($nodes[$i + 1]) && $nodes[$i + 1]->getLevel() > $node_level) ? "node" : "leaf",
$node->getLevel() > 0 ? link_to($node->getTitulo(), 'cms_categoria_edit', $node) : $node->getTitulo()
);
++$counter;
endforeach;
$html .= str_repeat('</li></ol>', $node_level) . '</li>';
$html = '<ol class="sortable">'. $html .'</ol>';
return $render ? print($html) : $html;
}
Extra tags: tree, node
This method checks to see if the node is a parent of the selected node, the selected node, or depth=0. Only iterations for nodes which meet one of these conditions add list items to the result string. All of the nodes get either the selected class, open class or both. Otherwise, it is your code.
$current_depth = 0;
$counter = 0;
$result = '<ul>';
foreach($tree as $node){
$node_depth = $node['depth'];
$node_name = $node['name'];
$node_id = $node['category_id'];
$selected = false;
if( $node['lft'] <= current['lft'] && $node['rgt'] >= $current['rgt'] ) $selected=true
if ($node_depth == 0 || $selected == true)
{
if($node_depth == $current_depth)
{
if($counter > 0) $result .= '</li>';
}
elseif($node_depth > $current_depth)
{
$result .= '<ul>';
$current_depth = $current_depth + ($node_depth - $current_depth);
}
elseif($node_depth < $current_depth)
{
$result .= str_repeat('</li></ul>',$current_depth - $node_depth).'</li>';
$current_depth = $current_depth - ($current_depth - $node_depth);
}
$result .= '<li id="c'.$node_id.'"';
$result .= ' class="';
$result .= $node_depth < 2 ?' open':' ';
$result .= $select == true ?' selected':' ';
$result .= '"';
$result .= '>'.$node_name.'';
++$counter;
}
}
$result .= str_repeat('</li></ul>',$node_depth).'</li>';
$result .= '</ul>';
return $result;
}
// "$current" may contain category_id, lft, rgt for active list item
print MyRenderTree($categories,$current);
?>
Just wanted to provide a OOP, cleaner version, which should make it easier to add any sort of logic apart from the selected one.
It works properly with the array structure posted by #satrun77.
class Node
{
var $name;
var $category;
var $depth;
var $lft;
var $rgt;
var $selected;
var $nodes = array();
public function __construct( $name, $category, $depth, $lft, $rgt, $selected = false )
{
$this->name = $name;
$this->category = $category;
$this->depth = $depth;
$this->lft = $lft;
$this->rgt = $rgt;
$this->selected = $selected;
}
public function addNode( Node $node )
{
array_push( $this->nodes, $node );
}
public function render()
{
$renderedNodes = '';
if ( $this->isSelected() ) {
$renderedNodes = $this->renderNodes();
}
return sprintf( '<li id="c%s">%s%s</li>', $this->category, $this->name, $renderedNodes );
}
protected function renderNodes()
{
$renderedNodes = '';
foreach ( $this->nodes as $node )
{
$renderedNodes .= $node->render();
}
return sprintf( '<ul>%s</ul>', $renderedNodes );
}
/** Return TRUE if this node or any subnode is selected */
protected function isSelected()
{
return ( $this->selected || $this->hasSelectedNode() );
}
/** Return TRUE if a subnode is selected */
protected function hasSelectedNode()
{
foreach ( $this->nodes as $node )
{
if ( $node->isSelected() )
{
return TRUE;
}
}
return FALSE;
}
}
class RootNode extends Node
{
public function __construct() {}
public function render()
{
return $this->renderNodes();
}
}
function MyRenderTree( $tree, $current )
{
/** Convert the $tree array to a real tree structure based on the Node class */
$nodeStack = array();
$rootNode = new RootNode();
$nodeStack[-1] = $rootNode;
foreach ( $tree as $category => $rawNode )
{
$node = new Node( $rawNode['name'], $category, $rawNode['depth'], $rawNode['lft'], $rawNode['rgt'], $rawNode['lft'] == $current['lft'] );
$nodeStack[($node->depth -1)]->addNode( $node );
$nodeStack[$node->depth] = $node;
end( $nodeStack );
}
/** Render the tree and return the output */
return $rootNode->render();
}
isn't it the best solution. why there are so many classes, objects bla bla.. ?
this simple function is perfect and flexible in everyways.
DEMO
$categories = array(
array('id'=>1,'name'=>'test1','parent'=>0),
array('id'=>2,'name'=>'test2','parent'=>0),
array('id'=>3,'name'=>'test3','parent'=>1),
array('id'=>4,'name'=>'test4','parent'=>2),
array('id'=>5,'name'=>'test5','parent'=>1),
array('id'=>6,'name'=>'test6','parent'=>4),
array('id'=>7,'name'=>'test7','parent'=>6),
array('id'=>8,'name'=>'test7','parent'=>3)
);
$cats = array();
foreach($categories as &$category)
$cats[$category['parent']][] = $category;
unset($categories);
$selected = 6; // selected id;
echo standartCategory($cats,$selected);
function standartCategory(&$categories,$selected = '',$parent = 0 /*MAIN CATEGORY*/)
{
if (!isset($categories[$parent])) return array('',0);
$html = '';
$haveSelected = 0;
foreach($categories[$parent] as $category) {
list($childHtml,$isVisible) = standartCategory($categories,$selected,$category["id"]);
$isSelected = $category['id']===$selected;
if (! ($isVisible | $isSelected)) { // this if to prevent output
$html .= '<li>'.$category['name'].'</li>';
continue;
}
$haveSelected |= $isVisible | $isSelected;
$html .= '<li>'.$category['name'].$childHtml.'</li>';
}
return $parent ? array('<ul>'.$html.'</ul>',$haveSelected) : '<ul>'.$html.'</ul>';
}

Categories