title: How to create categories and subcategories in codeigniter?
body: <p>I want to make online book sales but I am confused by category and subcategory.</p>
  
  <p>So I decided to create two tables as below :</p>
  
  <p>Table Categories</p>
  
  <p><a href="https://i.stack.imgur.com/nTuMn.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/nTuMn.png" alt="enter image description here"></a></p>
  
  <p>Table Product</p>
  
  <p><a href="https://i.stack.imgur.com/Vp3rc.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Vp3rc.png" alt="enter image description here"></a></p>
  
  <p>Model</p>
  
  <pre><code>   &lt;?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
      class Product_model extends CI_Model
      {
          var $table = 'product';
  
          public function ProductsCategories()
          {
              $this-&gt;db-&gt;select('*');
              $this-&gt;db-&gt;from('categories');
              $this-&gt;db-&gt;join('product', 'product.productID = categories.categoriesID','left');
  
              $query = $this-&gt;db-&gt;get();
  
              $products = array();
              if ($query-&gt;num_rows()) 
              {
                foreach ($query-&gt;result_array() as $row) 
                {
                  $products[$row['categoriesName']][] = $row;
                }
              }
              return $query-&gt;result_array();
          }
      }
  </code></pre>
  
  <p>Controller</p>
  
  <pre><code>&lt;?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  session_start();
  class Member extends CI_Controller
  {
      public function __construct()
      {
          parent::__construct();
          if ($this-&gt;session-&gt;userdata('username')=="") {
              redirect('auth');
          }
          $this-&gt;load-&gt;model('product_model','product');
      }
  
      public function index()
      {
          $data['username'] = $this-&gt;session-&gt;userdata('username');
          $data['member'] = $this-&gt;product-&gt;all();
          $data['categories'] = $this-&gt;product-&gt;ProductsCategories();
          $this-&gt;load-&gt;view('member/index', $data);
      }
  }
  </code></pre>
  
  <p>View</p>
  
  <pre><code>&lt;div class="yamm-content"&gt;
      &lt;div class="row"&gt;
      &lt;?php foreach($categories as $categoriesName =&gt; $row) : ?&gt;
          &lt;div class="col-sm-3"&gt;
              &lt;h5&gt;&lt;?php echo $categoriesName;?&gt;&lt;/h5&gt;
              &lt;ul&gt;
              &lt;?php foreach($row as $product) : ?&gt;
                  &lt;li&gt;&lt;?php echo anchor('method/'.$row['productID'], $row['productName']); ?&gt;&lt;/a&gt;&lt;/li&gt;
              &lt;?php endforeach; ?&gt;
              &lt;/ul&gt;
          &lt;/div&gt;
      &lt;?php endforeach;?&gt;
      &lt;/div&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>I want to show you results like this</p>
  
  <p><a href="https://i.stack.imgur.com/5uEBD.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/5uEBD.png" alt="enter image description here"></a></p>
  
  <p>Where is the mistake? thanks</p>