[eluser]ray73864[/eluser]
Hi Guys,
So i have a dynamic 'menu' which is all in a view called 'menu.php', now this menu is used by all of my controllers for loading up each page so i can't put this function in any one of them (well, i suppose i could but that would be duplicating the same code in all of them).
My problem is that inside the view i want to do:
<?php
function menu_tree()
$this->db->query("SELECT blah from blah");
<plus a lot of other stuff>
}
?>
<html code>
menu_tree() is a recursive function, unfortunately php comes up with: Fatal error: Using $this when not in object context in /home/http/capelite/system/application/views/menu.php on line 8
The full code:
Code:
<?php
function menu_tree($parent=0,$level=0)
{
// $parent is the parent of the children we want to see
// $level is increased when we go deeper into the tree,
// used to display a nice indented tree
// retrieve all children of $parent
$this->db->select('id, parent_id, label, startpage');
$this->db->from('menu');
if ($parent)
$this->db->where('parent_id',$parent);
else
$this->db->where('parent_id',0);
$this->db->order_by('sortorder','asc');
$result = $this->db->get();
$indent = ($parent != NULL) ? str_repeat(' ',$level*2) : '' ;
$children = ($result->num_rows()>0) ? TRUE : FALSE;
// display each child
$tree ='';
$tree .= ($children) ? $indent."<ul>\n" : '';
foreach ($result->result() as $row) {
// indent and display the title of this child
$tree .= $indent .
'<li>' .
($row->startpage) ? anchor('/','HOME') : anchor('/capelite/page/' . $row->id, stripslashes($row->label)) .
'</li>';
// call this function again to display this child's children
$tree .= $this->menu_tree($row->id, $level+1);
}
$tree.= ($children) ? $indent."</ul>\n" : '';
return $tree;
}
?>
<div id="leftContainer">
<div id="navContainerHome">
<div>
<?php print_r(menu_tree(0,0)); ?>
<ul id="nav">
<?=$this->session->userdata('logged_in') ? "" : "<li>" . anchor("/capelite/login", "Login") . "</li>"?>
<?=$this->session->userdata('logged_in') ? "<li>" . anchor("/capelite/logout", "LOGOUT") . "</li>" : ""?>
</ul>
</div>
</div>
<div id="optContainerHome">
<?php if ($this->session->userdata('logged_in')): ?>
<?php if ($this->session->userdata('role') == 'admin'): ?>
<div>
<h4>Site Options</h4>
<ul id="nav">
<li><a href="/admin">Admin Home</a></li>
<li><a href="/pagesmith">Page Smith</a></li>
<li><a href="/admin_catalogue/products">Products Catalogue</a></li>
<li><a href="/admin_catalogue/categories">Categories Catalogue</a></li>
<li><a href="/admin/orders">Online Orders</a></li>
</ul>
</div>
<?php elseif ($this->session->userdata('role') == 'user'): ?>
<div>
<h4>User Options</h4>
<ul id="nav">
<li><a href="/user/details">Update Details</a></li>
<li><a href="/user/orders_pending">Orders Pending</a></li>
<li><a href="/user/orders_shipped">Orders Shipped</a></li>
</ul>
</div>
<?php endif; ?>
<?php endif; ?>
</div>
</div>