title: CodeIgniter - Severity: Notice Message: Trying to get property of non-object
body: <p>I have just started using codeigniter, PHP and facing issue while trying to build UI dynamically by fetching data from DB:</p>
  
  <p><strong>Requirement:</strong>
  I need to create a list of parent - child dynamically by reading info from DB. 
  I have two tables admin_menu and admin_menu_item. Admin menu contains parent menu options and admin_menu_item contains each parent's detail option. admin_menu_item has AD_MENU_ID column storing parent's id giving me hierarchical structure.</p>
  
  <p><strong>My Controller:</strong></p>
  
  <p>class Home extends CI_Controller {</p>
  
  <pre><code>public function index()
  {
      $this-&gt;load-&gt;helper('url');
      $this-&gt;load-&gt;helper('array');
  $this-&gt;load-&gt;model('Menu');
  $header_data['base_url'] = base_url(); 
      $data['menu'] = $this-&gt;Menu-&gt;get_admin_menu_data();
  
      $data['menu_item'] = array();
  foreach($data['menu'] as $row){
   array_push($data['menu_item'], 
       $this-&gt;Menu-&gt;get_admin_menu_item_data($row-&gt;ad_menu_id));
  }
              $this-&gt;load-&gt;view('homepage/header');
      $this-&gt;load-&gt;view('homepage/admin_menu',$data);
  }
  </code></pre>
  
  <p>}</p>
  
  <p>Here is the structure of the code</p>
  
  <p><strong>My Model:</strong></p>
  
  <pre><code>class Menu extends CI_Model{
  
  function __construct()
  {
      // Call the Model constructor
      parent::__construct();
  }
  
  function get_admin_menu_data()
  {
      $this-&gt;db-&gt;select('ad_menu_id,ad_menu_name');
      $query = $this-&gt;db-&gt;get('admin_menu');
      return $query-&gt;result();
  }
  
  function get_admin_menu_item_data($menu_id)
  {
      $query = $this-&gt;db-&gt;query("SELECT ad_menu_item_name FROM ADMIN_MENU_ITEM WHERE AD_MENU_ID = " . $menu_id);
  
      if( $query-&gt;num_rows() &gt; 0 ){
          return $query-&gt;result();
        }
  }
  </code></pre>
  
  <p>}</p>
  
  <p>When I use $data variable in my view I get a blank parent row resulting in an extra item where parent doesn't have a value and I get following error</p>
  
  <p>A PHP Error was encountered
  Severity: Notice
  Message: Trying to get property of non-object
  Filename: homepage/admin_menu.php
  Line Number: 9</p>
  
  <p>which is basically my view line where I access parent element</p>
  
  <p><strong>My View:</strong></p>
  
  <pre><code>      echo "&lt;ul class=\"accordion\" id=\"accordion-1\"&gt;";
          foreach ($menu as $key =&gt; $value) {
          print "&lt;li class=\"dcjq-current-parent\"&gt;"; //add li tag
          print "&lt;a href=\"#\"&gt;" . $value-&gt;ad_menu_name . "&lt;/a&gt;\n";
  
          print "&lt;ul&gt;";                
          foreach($menu_item as $key_item =&gt; $value_item){
                print "&lt;li&gt;&lt;a href=\"#\"&gt;";
                print  $value_item-&gt;ad_menu_item_name . "&lt;/a&gt;&lt;/li&gt;\n";
                $action_type = '';
          }//end of menu item for each
         print "&lt;/ul&gt;&lt;/li&gt;\n";
        }//end of menu for each
          print "&lt;/ul&gt;";
  </code></pre>
  
  <p>Sorry, for the long post but I am really stuck here and cannot figure out what is wrong with this?</p>
  
  <p><strong>Update:</strong></p>
  
  <p>I was finally able to make it work ny changing the code in model and view. The issue was objects within objects causing the hierarchy to become too complex. 
  The final solution was to make just one database call and process data in the view.
  Thanks Yan for working with me and guiding to the solution.</p>