CodeIgniter Forums
Message: Trying to get property of non-object - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Message: Trying to get property of non-object (/showthread.php?tid=43804)



Message: Trying to get property of non-object - El Forum - 07-24-2011

[eluser]ibnu drim[/eluser]
Hello,

I want to display in the gallery child view,, I select from the gallery where Id = child ,,,
but why do I find the error
Code:
A PHP Error was encountered
Severity: Notice
Message: Trying to get property of non-object
and
Code:
A PHP Error was encountered Severity: Notice Message: Uninitialized string offset: 0
Filename: libraries/Loader.php(673) : eval()'d code
I have several functions in my Model that all look like this (that find various things):
this is my controller :
Code:
<?php
class Gallery extends Controller {

    function Gallery()
    {
        parent::Controller();    
        $this->load->helper('image_helper');
        
    }
    
    function index( $slug = FALSE )
    {
        #show lists or post?
        if( empty( $slug ) )
            $this->lists();
        else
            $this->post( $slug );
        
    }
    
    function lists()
    {
        $this->load->model('model_posts');
        $this->load->model('model_gallery');
        $posts = $this->model_gallery->getPosts();
        $post = $this->model_gallery->getChild();
        
        $data = array(
            'contentPage'        =>    'gallerys',
            'breadcrumb'        =>    $this->model_posts->createBreadCrumb(),
            'advTop'            =>  FALSE,
            'posts'             =>  $posts,
        );
        $this->load->view('index', $data);
    }
    
    function post( $slug = FALSE )
    {
        if( empty( $slug ) )
            show_404();
            
        $this->load->model('model_posts');
        $this->load->model('model_gallery');
        $posts = $this->model_gallery->getPosts();
        $post = $this->model_gallery->getChild($slug);
        
        $data = array(
            'contentPage'        =>    'gallery',
            'breadcrumb'        =>    $this->model_posts->createBreadCrumb(),
            'advTop'            =>  FALSE,
            'posts'             =>  $post
        );
        if($post)
        $data['show'] = $post->title;
          
        $this->load->view('index', $data);
    }
}

this my model :
Code:
<?php
class Model_gallery extends Model {

    function Model_gallery()
    {
        parent::Model();
    }

    function getPosts()
    {
        $this->db->where('status', 1)
                 ->where('parent', 0)
                 ->order_by('date_add', 'DESC')
                 ->from('gallery');
        $RES = $this->db->get();
        
        return $RES->num_rows() <= 0? FALSE:$RES;
    }
    
    function getPost( $slug = FALSE )
    {
        $this->db->where('status', 1)
                // ->where('parent', $slug->ID)
                 ->where('slug', $slug)
                 //->where('parent', $slug->ID)
                 ->from('gallery');
        $RES = $this->db->get()->row();
        
        return empty( $RES )? FALSE:$RES;
    }
    
    function getChild( $slug = FALSE )
    {
        $this->db->where('status', 1)
                 //->where('parent', 0)
                 ->where('slug', $slug)
                 ->from('gallery');
                
        $Query = $this->db->get();
            
        if( $Query->num_rows <= 0 )
            return FALSE;
            
        $posts = array();
        foreach( $Query->result() as $result )
        {
            $galler = array();
            
            #has Child?
            $child = $this->pagesHierarchy( $result->ID );
            if( $child )
            {
                foreach( $child->result() as $c )
                    $galler['child'][] = $c;
                        
                $galler['parent'] = $result;
                
                $posts[] = $galler ;
            }
        }
        //return $posts ;
        return !count( $posts )? FALSE:$posts;
    }
    
    function pagesHierarchy( $idParent = FALSE )
    {
        if( !$idParent || empty( $idParent ) )
            return FALSE;
        
        $this->db->where('parent', $idParent)
                 ->from('gallery')
                 ->order_by('ID', 'ASC');
                
        $RES = $this->db->get();
        
        return $RES->num_rows() <= 0? FALSE:$RES;
    }
    
    function getByID( $ID )
    {
        $this->db->where('ID', $ID)
                 ->from('gallery');
        
        $query = $this->db->get();
        
        if( $query->num_rows() <= 0 )
            show_error('Not found Content with ID = ' . $ID);
        else
            return $query->row();
    }
    
}

this my view :
Code:
if( $posts ):
?&gt;
                    <table class="gallery-lists">
&lt;?php
        $count = 1; $tCount = 1;
        foreach( $posts as $post ):
            if( !isset( $post['parent'] ) )
                continue;
                
            $post = $post['child'];
            echo print_r($post);
            
            $primaryImg = $this->models->_unserialize( $post->image_primary );
            $secondaryImg = $this->models->_unserialize( $post->image_secondary );
            
            if( $count <= 1 ):
?&gt;                    
                       <tr>
&lt;?php
            endif;
?&gt;
                            <td>
                                <a href="&lt;?php echo base_url(); ?&gt;img.php?act=view&src;=&lt;?php echo $primaryImg['file_name']; ?&gt;" rel="group" class="colorbox">title; ?&gt;">
                                    <span class="img"><span>&lt;?php echo auto_resize( !$secondaryImg? $primaryImg:$secondaryImg, 100, 0, base_url(), FALSE ) ?&gt;</span></span>
                                    <span class="description">
                                        <span class="title">&lt;?php echo $post->title; ?&gt;</span>
                                        <span class="text">&lt;?php echo $post->content; ?&gt;</span>
                                    </span>

Any idea ? Thanks


Message: Trying to get property of non-object - El Forum - 07-24-2011

[eluser]toopay[/eluser]
I have an idea, but since this is not the right place to share it, then you could re-post your thread elsewhere in other section. Your error is not a bug, it just a general php warning.


Message: Trying to get property of non-object - El Forum - 07-24-2011

[eluser]InsiteFX[/eluser]
For one what version of CI are you running?

InsiteFX


Message: Trying to get property of non-object - El Forum - 07-26-2011

[eluser]ibnu drim[/eluser]
ci version 1.7 ,, master ...