Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>There are any number of different ways to skin this particular cat, some more efficient than others. Below is off the top of my head and meant to get you headed in the right general direction.</p> <p>In addition to your <code>$category_link</code> you need something that indexes your subcategories by category. Something like <code>$subcategory_link</code> which might be an array of arrays, indexed first by the category id (or name), and then by an incrementing integer for each subcategory (or its id, or whatever). In addition to your existing query and <code>while</code> loop, run a query that retrieves all of the subcategories, and then fills in <code>$subcategory_link</code> with the relevant information in a second, similar <code>while</code> loop.</p> <pre><code>$sc = array( 'id' =&gt; $subcategory['id'], 'subcategory' =&gt; $subcategory['subcategory'], ); $subcategory_link[$subcategory['cat_id']][$j++] = $sc; </code></pre> <p>Note that this array is first indexed by the category id. Then in your template:</p> <pre><code>{foreach $category_link as $cat} &lt;div class="category"&gt;{$cat.catname}&lt;/div&gt; {foreach $subcategory_link[$cat.cat_id] as $subcat} &lt;div class="subcat"&gt;{$subcat.subcategory}&lt;/div&gt; {/foreach} {/foreach} </code></pre> <p><em><strong>Note</em></strong>: The above has not been checked for proper Smarty syntax. You should get the idea though.</p> <p>It is left as an exercise for you to determine how to handle categories with no subcategories, and to improve efficiency.</p>
 

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