Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

Parent and SubCategory Drop Down Menu

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> General
View previous topic :: View next topic  
Author Message
ShamusMay
Smarty Rookie


Joined: 28 Apr 2015
Posts: 18

PostPosted: Tue Apr 28, 2015 12:38 pm    Post subject: Parent and SubCategory Drop Down Menu Reply with quote

I'm using Smarty, PHP and MySQL.

This should be very easy for someone that understands how to do this. I'm a noob and have no idea.

Trying to get a horizontal nav bar with parent and subcategories in the drop down.

The current code only displays the parent and I need the subcategories to display under.

I don't think I have any SQL to get the subcats and don't really know how to write it, so maybe someone can help.

Here's my simple DB layout.

TABLE NAME : CATEGORY

Code:
   category_id  name      parent_id
    1            Parent    0
    2            Parent    0
    2            Parent    0
    3            Child     1
    4            Child     2
    5            Child     2
    6            Child     3
    7            Child     3


Current Function to get Categories

Code:
 // list of all categories
function getCategoriesList($include_subcats = false) {

 $where = '';

 if ($include_subcats != false) {
   $where = ' where parent_id = 0 ';
 }
$list =
 getSqlResult(
   "select * from category $where ORDER BY parent_id ASC",
   SQL_RESULT_ALL);

 return $list;
}

$smarty->assign('CategoriesList', getCategoriesList());



Template Code to Display the Menu

Code:
{foreach name=CategoriesList from=$CategoriesList item=i}
<li class="dropdown "><a href="/{$i.category_filename}" class="dropdown-toggle" data-toggle="dropdown">{$i.category_name}<b class="caret"></b></a>
</li>{/foreach}
Back to top
View user's profile Send private message
U.Tews
Administrator


Joined: 22 Nov 2006
Posts: 5068
Location: Hamburg / Germany

PostPosted: Tue Apr 28, 2015 9:54 pm    Post subject: Reply with quote

Create a nested array like this
Code:
 // list of all categories
function getCategoriesList($include_subcats = false) {

 $where = '';

 if ($include_subcats != false) {
   $where = ' where parent_id = 0 ';
 }
$list =
 getSqlResult(
   "select * from category $where ORDER BY parent_id ASC",
   SQL_RESULT_ALL);

 if ($include_subcats != false) {
   foreach ($list as $key => $parent) {
     $subcat =
       getSqlResult(
          "select * from category where parent_id = '{$parent['category_id']}'  ORDER BY parent_id ASC",
              SQL_RESULT_ALL);
          $list[$key]['subcats'] = $subcat;
       
      }
 }
 return $list;
}

$smarty->assign('CategoriesList', getCategoriesList());




Code:

{foreach from=$CategoriesList item=parent}
   .... parent output
   {foreach from=$parent.subcats item=subcat}
   ......   subcat output
   {/foreach}
{/foreach}
Back to top
View user's profile Send private message
ShamusMay
Smarty Rookie


Joined: 28 Apr 2015
Posts: 18

PostPosted: Tue Apr 28, 2015 10:11 pm    Post subject: Reply with quote

Much appreciated.

Do you know what the smarty output code would look like?

Here is what I tried, but it only shows the parent when displayed.

Code:
        {foreach name=CategoriesList from=$CategoriesList item=parent}
       <li class="dropdown "><a href="/{$parent.category_filename}" class="dropdown-toggle" data-toggle="dropdown">{$parent.category_name}<b class="caret"></b></a>
         
          <ul class="dropdown-menu">
            {foreach name=CategoriesList from=$parent.subcats item=subcat}
                <li><a href="/{$subcat.category_filename}">{$subcat.category_name}</a></li>
                 <li class="divider"></li>
            {/foreach}
        </ul>
         
         </li>{/foreach}
Back to top
View user's profile Send private message
U.Tews
Administrator


Joined: 22 Nov 2006
Posts: 5068
Location: Hamburg / Germany

PostPosted: Tue Apr 28, 2015 10:43 pm    Post subject: Reply with quote

var_dump() the returned $list in getCategoriesList() to see if the subcats got loaded as sub array.

Remove name=CategoriesList in {foreach} (not needed in this case) or use different names for the two loops. Same name may confuse {foreach}
Back to top
View user's profile Send private message
ShamusMay
Smarty Rookie


Joined: 28 Apr 2015
Posts: 18

PostPosted: Wed Apr 29, 2015 12:44 pm    Post subject: Reply with quote

Looks like the sub array is loading correct. I am thinking the issue is with the SMARTY code loop I'm using. I took the "name" out of the Smarty loops and also tried renaming, but neither worked.

Var_Dump ($list) Results

Code:
array(2) {
  [0]=>
  array(25) {
    [0]=>
    string(1) "1"
    ["category_id"]=>
    string(1) "1"
    [1]=>
    string(23) "Example Parent Category"
    ["category_name"]=>
    string(23) "Example Parent Category"
    [2]=>
    string(16) "Description Text"
    ["category_description"]=>
    string(16) "Description Text"
    [3]=>
    string(22) "Description Index Text"
    ["category_short_desc"]=>
    string(22) "Description Index Text"
    [4]=>
    string(21) "Meta Description Text"
    ["category_meta_description"]=>
    string(21) "Meta Description Text"
    [5]=>
    string(24) "Keyword Description Text"
    ["category_meta_keywords"]=>
    string(24) "Keyword Description Text"
    [6]=>
    string(1) "0"
    ["category_pos"]=>
    string(1) "0"
    [7]=>
    string(18) "whiskey-barrel.gif"
    ["category_img_name"]=>
    string(18) "whiskey-barrel.gif"
    [8]=>
    string(18) "whiskey-barrel.gif"
    ["category_img_big_name"]=>
    string(18) "whiskey-barrel.gif"
    [9]=>
    string(10) "parent.php"
    ["category_filename"]=>
    string(10) "parent.php"
    [10]=>
    string(19) "2015-04-29 07:34:10"
    ["category_date"]=>
    string(19) "2015-04-29 07:34:10"
    [11]=>
    string(1) "0"
    ["category_parent_id"]=>
    string(1) "0"
    ["subcats"]=>
    array(1) {
      [0]=>
      array(24) {
        [0]=>
        string(1) "2"
        ["category_id"]=>
        string(1) "2"
        [1]=>
        string(20) "Example Sub Category"
        ["category_name"]=>
        string(20) "Example Sub Category"
        [2]=>
        string(21) "Sub  Description Text"
        ["category_description"]=>
        string(21) "Sub  Description Text"
        [3]=>
        string(26) "Sub Description Index Text"
        ["category_short_desc"]=>
        string(26) "Sub Description Index Text"
        [4]=>
        string(25) "Sub Meta Description Text"
        ["category_meta_description"]=>
        string(25) "Sub Meta Description Text"
        [5]=>
        string(21) "Sub Meta Keyword Text"
        ["category_meta_keywords"]=>
        string(21) "Sub Meta Keyword Text"
        [6]=>
        string(1) "0"
        ["category_pos"]=>
        string(1) "0"
        [7]=>
        string(18) "firkin-cask-lg.jpg"
        ["category_img_name"]=>
        string(18) "firkin-cask-lg.jpg"
        [8]=>
        string(18) "firkin-cask-lg.jpg"
        ["category_img_big_name"]=>
        string(18) "firkin-cask-lg.jpg"
        [9]=>
        string(7) "sub.php"
        ["category_filename"]=>
        string(7) "sub.php"
        [10]=>
        string(19) "2015-04-29 07:34:20"
        ["category_date"]=>
        string(19) "2015-04-29 07:34:20"
        [11]=>
        string(1) "1"
        ["category_parent_id"]=>
        string(1) "1"
      }
    }
  }
  [1]=>
  array(25) {
    [0]=>
    string(1) "2"
    ["category_id"]=>
    string(1) "2"
    [1]=>
    string(20) "Example Sub Category"
    ["category_name"]=>
    string(20) "Example Sub Category"
    [2]=>
    string(21) "Sub  Description Text"
    ["category_description"]=>
    string(21) "Sub  Description Text"
    [3]=>
    string(26) "Sub Description Index Text"
    ["category_short_desc"]=>
    string(26) "Sub Description Index Text"
    [4]=>
    string(25) "Sub Meta Description Text"
    ["category_meta_description"]=>
    string(25) "Sub Meta Description Text"
    [5]=>
    string(21) "Sub Meta Keyword Text"
    ["category_meta_keywords"]=>
    string(21) "Sub Meta Keyword Text"
    [6]=>
    string(1) "0"
    ["category_pos"]=>
    string(1) "0"
    [7]=>
    string(18) "firkin-cask-lg.jpg"
    ["category_img_name"]=>
    string(18) "firkin-cask-lg.jpg"
    [8]=>
    string(18) "firkin-cask-lg.jpg"
    ["category_img_big_name"]=>
    string(18) "firkin-cask-lg.jpg"
    [9]=>
    string(7) "sub.php"
    ["category_filename"]=>
    string(7) "sub.php"
    [10]=>
    string(19) "2015-04-29 07:34:20"
    ["category_date"]=>
    string(19) "2015-04-29 07:34:20"
    [11]=>
    string(1) "1"
    ["category_parent_id"]=>
    string(1) "1"
    ["subcats"]=>
    array(0) {
    }
  }
}
Back to top
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1785

PostPosted: Wed Apr 29, 2015 1:26 pm    Post subject: Reply with quote

Use Smarty3 {foreach} syntax.
Back to top
View user's profile Send private message
ShamusMay
Smarty Rookie


Joined: 28 Apr 2015
Posts: 18

PostPosted: Wed Apr 29, 2015 1:41 pm    Post subject: Reply with quote

My current Smarty code looks like this and it only shows the parent categories. The old syntax was working to display the parent categories too, so my issue is still that the subcats are not displaying.

Code:
{foreach $CategoriesList as $parent}
       <li class="dropdown "><a href="/{$parent.category_filename}" class="dropdown-toggle" data-toggle="dropdown">{$parent.category_name}<b class="caret"></b></a>
         <ul class="dropdown-menu">
           {foreach $parent.subcats as $subcat}
                <li><a href="/{$subcat.category_filename}">{$subcat.category_name}</a></li>
                 <li class="divider"></li>
            {/foreach}
</ul>
         </li>{/foreach}
Back to top
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1785

PostPosted: Wed Apr 29, 2015 3:03 pm    Post subject: Reply with quote

{$CategoriesList|var_dump}
?
Back to top
View user's profile Send private message
ShamusMay
Smarty Rookie


Joined: 28 Apr 2015
Posts: 18

PostPosted: Wed Apr 29, 2015 4:55 pm    Post subject: SOLVED Reply with quote

Changed the operators from != to == and it started working.

// list of all categories
function getCategoriesList($include_subcats = false) {

$where = '';

if ($include_subcats == false) {
$where = ' where category_parent_id = 0 ';
}
$list =
getSqlResult(
"select * from category $where ORDER BY category_parent_id ASC", SQL_RESULT_ALL);

if ($include_subcats == false) {
foreach ($list as $key => $parent) {
$subcat =
getSqlResult(
"select * from category where category_parent_id = '{$parent['category_id']}' ORDER BY category_parent_id ASC", SQL_RESULT_ALL);
$list[$key]['subcats'] = $subcat;
}
}
return $list;
}
Back to top
View user's profile Send private message
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> General All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP