title: codeigniter dynamic menu creation
body: <p>I am trying to create dynamic menu with database.</p>
  <p>The <em><strong>Controller</strong></em> is menu.php</p>
  <pre><code>  &lt;?php
    $_SESSION['lang'] = 'EN';
  
  class Menu extends CI_Controller {
  function __construct(){
      parent::__construct();
      $this-&gt;load-&gt;database();
      $this-&gt;load-&gt;model(&quot;menu_model&quot;);
  }
  
  public function index()
  {
      $data['category']= $this-&gt;menu_model-&gt;getCategory('$lang');
      $data['subcategory']= $this-&gt;menu_model-&gt;getSubCategory('$lang');
  
      $this-&gt;load-&gt;view('vwHeader',$data);//Left Menu
  }
  }
  ?&gt;
  </code></pre>
  <p>the <em><strong>Model</strong></em> is menu_model.php</p>
  <pre><code>&lt;?php
   class Menu_model extends CI_Model{
  function __construct(){
      parent::__construct();
      $this-&gt;load-&gt;database();
  }
  
  function getCategory($lang){
      $sql = 'SELECT * FROM category';
      $result = $this-&gt;db-&gt;query($sql);
      if($result-&gt;num_rows() &gt; 0)
      {
      
      return $result;
      }
      
      
  }
  
  function getSubCategory($lang){
      $sql = 'SELECT * FROM subcategory';
      $result = $this-&gt;db-&gt;query($sql);
     if($result-&gt;num_rows() &gt; 0)
      {
     return $result;
      }
      
  }
  
  }
  ?&gt;
  </code></pre>
  <p>the <em><strong>View</strong></em> is vwHeader.php</p>
  <pre><code>&lt;?php
  
  
             foreach($category-&gt;result() as $menu)
  {
  echo &quot;&lt;ul style='margin-left: 148px;margin-top: 15px;'&gt;&quot;;
     echo &quot;&lt;li style='float: left;
  
  
  height: 57px;
  text-decoration: none;
  display: block;'&gt;&lt;a class=\&quot;sf-with-ul\&quot; href=\&quot;category/&quot;.$menu-&gt;urlcategory.&quot;\&quot;&gt;&quot;.$menu-&gt;namecategory.&quot;&lt;/a&gt;&quot;;
     /*check whether sub menu is there if so first print &lt;ul&gt; then &lt;li&gt;&lt;a&gt;*/
     if(count($subcategory-&gt;result()) &gt; 0 &amp;&amp; is_array($subcategory-&gt;result()))
     {
           echo &quot;&lt;ul class=\&quot;sub-menu\&quot;&gt;&quot;;
          foreach($subcategory-&gt;result() as $key=&gt;$submenu)
          {
            if ($menu-&gt;idcategory == $submenu-&gt;idcategory){
             echo &quot;&lt;li&gt;&lt;a href=\&quot;category/&quot;.$submenu-&gt;urlsubcategory.&quot;\&quot;&gt;&quot;.$submenu-&gt;namesubcategory.&quot;&lt;/a&gt;&lt;/li&gt;&quot;;
              }
           }
          
          
          
      }
      else
          {
          echo &quot;&lt;ul style='display:none'&gt;&quot;;
          echo &quot;&lt;/ul&gt;&quot;;
          } 
          echo &quot;&lt;/ul&gt;&quot;;
      echo &quot;&lt;/li&gt;&quot;;
       echo &quot;&lt;/ul&gt;&quot;;
  }
  
                      ?&gt;
  </code></pre>
  <p>Now the error is,</p>
  <blockquote>
  <p>Severity: Notice</p>
  <p>Message: Undefined variable: category</p>
  <p>Filename: views/vwHeader.php</p>
  <p>Line Number: 38</p>
  <p>Fatal error: Call to a member function result() on a non-object in C:\xampp\htdocs\admin\application\views\vwHeader.php on line 38</p>
  </blockquote>
  <p>how can i solve that????</p>