Note that there are some explanatory texts on larger screens.

plurals
  1. POGet Categories and Subcategories Codeigniter
    primarykey
    data
    text
    <p>I have an issue getting the subcategories from the same table, the subcategories have a parentid that represent the main category.</p> <p>Where i'm wrong ?</p> <p>My structure is the next.</p> <p>Controller: </p> <pre><code>$cats = $this-&gt;categories_model-&gt;getCategories(); foreach($cats as $ct){ $cat_id = $ct-&gt;id; } $data['categories'] = $this-&gt;categories_model-&gt;getCategories(); $data['subcategories'] = $this-&gt;categories_model-&gt;getSubcategories($cat_id); </code></pre> <p>Model: </p> <pre><code>public function getCategories() { $query = $this-&gt;db-&gt;get_where('categories', array('visible' =&gt; 1, 'parentid' =&gt; 0)); return $query-&gt;result(); } function getSubcategories($cat_id) { $this-&gt;db-&gt;select('*'); $this-&gt;db-&gt;from('categories'); $this-&gt;db-&gt;where(array('parentid' =&gt; $cat_id, 'visible' =&gt; 1)); $query = $this-&gt;db-&gt;get(); return $query-&gt;result(); } </code></pre> <p>View:</p> <pre><code>&lt;h4&gt;Categories&lt;/h4&gt; &lt;div class="list-group categories"&gt; &lt;?php foreach($categories as $category): ?&gt; &lt;a href="&lt;?php echo site_url() . '/products/catalog_list/' . $category-&gt;id ; ?&gt;" class="list-group-item"&gt;&lt;?php echo $category-&gt;name ; ?&gt;&lt;span class="glyphicon glyphicon-chevron-right"&gt;&lt;/span&gt;&lt;/a&gt; &lt;?php foreach($subcategories as $subcategory): ?&gt; &lt;div class="list-subgroups"&gt; &lt;a href="&lt;?php echo $subcategory-&gt;id; ?&gt;" class="list-subgroup-item"&gt;&lt;?php echo $subcategory-&gt;name; ?&gt;&lt;/a&gt; &lt;/div&gt; &lt;?php endforeach; ?&gt; &lt;?php endforeach; ?&gt; &lt;/div&gt; </code></pre> <p>Table data:</p> <pre><code>id parentid name description metatags visible 1 0 Boots Boots boots 1 2 1 Man boots man boots NULL 1 3 1 Women boots women boots NULL 1 5 0 Jackets Jackets NULL 1 6 5 Women jackets Women jackets NULL 1 </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload