Welcome Guest, Not a member yet? Register   Sign In
using $this in a function inside a view
#1

[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>
}
?&gt;

&lt;html code&gt;

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:
&lt;?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;
    }
?&gt;

<div id="leftContainer">
<div id="navContainerHome">
            <div>
                
            
                &lt;?php print_r(menu_tree(0,0)); ?&gt;
                <ul id="nav">
                    &lt;?=$this->session->userdata('logged_in') ? "" : "<li>" . anchor("/capelite/login", "Login") . "</li>"?&gt;
                    &lt;?=$this->session->userdata('logged_in') ? "<li>" . anchor("/capelite/logout", "LOGOUT") . "</li>" : ""?&gt;
                </ul>
            </div>
</div>
        <div id="optContainerHome">
        &lt;?php if ($this->session->userdata('logged_in')): ?&gt;
                &lt;?php if ($this->session->userdata('role') == 'admin'): ?&gt;
                    <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>
                &lt;?php elseif ($this->session->userdata('role') == 'user'): ?&gt;
                    <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>
                &lt;?php endif; ?&gt;
            &lt;?php endif; ?&gt;
        </div>
</div>
#2

[eluser]ray73864[/eluser]
Ahh, nevermind i figured out how to do it, i created a new library called 'Menu' which i then put in the 'autoload.php' file and in my view i simply did: $this->menu->menu_tree()

So now it will appear on all pages that include the menu.php view.
#3

[eluser]mglinski[/eluser]
Never declare functions in a view. ever.
-Matt
#4

[eluser]supahero[/eluser]
try using

Code:
$CI = & get_instance();

then your code will looks like

Code:
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
    $CI = & get_instance();
    $CI->db->select('id, parent_id, label, startpage');
    $CI->db->from('menu');
    
    if ($parent)
        $CI->db->where('parent_id',$parent);
    else
        $CI->db->where('parent_id',0);
        
    $CI->db->order_by('sortorder','asc');
        
    $result = $CI->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 .= $CI->menu_tree($row->id, $level+1);
    }
    $tree.= ($children) ? $indent."</ul>\n" : '';
      
    return $tree;
}
#5

[eluser]mglinski[/eluser]
Move that to a helper file, views are just that, just data displays.
Keep data generators out of views.
-MAtt




Theme © iAndrew 2016 - Forum software by © MyBB