Note that there are some explanatory texts on larger screens.

plurals
  1. POCategories & Sub-Categories with php, mysql and smarty
    text
    copied!<p>I'm building a custom shopping cart for a supermarket with php, mysql and smarty template the products categories and sub categories must be in tow separate tables categories table (id, catname) subcategories table (id,cat_id,subcategory) where cat_id refers to the category let say I have 2 categories and 2 subcategories for each category my php code is</p> <pre><code>$smarty = new Smarty; $smarty-&gt;caching = true; $smarty-&gt;cache_lifetime = 120; $category_mysql = "SELECT * FROM category"; $run_category = mysql_query($category_mysql, $link) or die(mysql_error()); $all_category = mysql_num_rows($run_category); $category_link = array(); $i=0; while ($category = mysql_fetch_assoc($run_category)) { $ml = array( 'id' =&gt; $category['id'], 'catname'=&gt; $category['catname'], ); $category_link[$i++] = $ml; } $smarty-&gt;assign('category_link', $category_link); $smarty-&gt;display("products.tpl"); </code></pre> <p>and my html code is:</p> <pre><code>{section name=cats loop=$category_link} &lt;div class="category"&gt;{$category_link[cats].catname}&lt;/div&gt; {/section} </code></pre> <p>How could I view each sub-category below the category it belongs to?? like:</p> <pre><code>&lt;div class="category"&gt;Category 1&lt;/div&gt; &lt;div class="subcat"&gt;Sub Cat 1&lt;/div&gt; &lt;div class="subcat"&gt;Sub Cat 2&lt;/div&gt; &lt;div class="category"&gt;Category 2&lt;/div&gt; &lt;div class="subcat"&gt;Sub Cat 3&lt;/div&gt; &lt;div class="subcat"&gt;Sub Cat 4&lt;/div&gt; </code></pre> <p>Regards</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