CodeIgniter - Not allowing $this usage within Function

Discussion in 'PHP' started by davidcubed, Sep 10, 2009.

  1. #1
    Trying to show a category tree (with subcats and sub-subcats)

    Using this code:
    <?php 
    	$categories = get_categories();
    	
    	foreach($categories as $category) {
    		    // Print the top level category
    		    echo $category->title .'<br />';
    		    
    		    // get the sub categories
    		    $subcategories = get_categories($category->id);
    		    foreach($subcategories as $subcategory) {
    		        echo '- '. $subcategory->title. '<br />'; 
    		    }
    		}
    		
    		function get_categories($parent = 0) {
    		    $this->db->where('parent', $parent); 
    		    $query = $this->db->get('bin_location');
    		    return $query->result();
    		}
    	?>
    PHP:
    Getting: Fatal error: Call to a member function on a non-object in /home/domains/public_html/system/application/views/frontpage_view.php on line 25

    Anyone have any ideas why I am having this problem?
     
    davidcubed, Sep 10, 2009 IP
  2. bitist

    bitist Peon

    Messages:
    28
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #2
    You can't use $this-> in this context.
    First you have to do:
    
    $this->CI =& get_instance();
    
    PHP:
    then using $this->CI->db->where('parent', $parent) won't produce any error.
     
    bitist, Jan 28, 2010 IP