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

Menu and Submenu creation problem

 
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 -> Tips and Tricks
View previous topic :: View next topic  
Author Message
websterz
Smarty n00b


Joined: 17 Dec 2011
Posts: 2

PostPosted: Sat Dec 17, 2011 2:49 pm    Post subject: Menu and Submenu creation problem Reply with quote

Hello,
I am newbie to PHP and Smarty and working on a web site that contains menu and submenu retrieved from database, i am using PDO to fetch the main menu but i am not able to succeed while retrieving the submenu for the main menu. here is the code for template and for php as well:

PHP Code:
Code:
//Gets main menu from the table ("sub" is a column which tells whether it is main menu or sub menu, it contains an id of main menu for the submenu)
$res = $db->prepare("select * from main_menu where sub='' ORDER BY id ASC");
  $res->execute();
  $res->setFetchMode(PDO::FETCH_LAZY);

//Gets main menu from the db and apply the while loop on to get proper ids of submenu items.
$res1 = $db->prepare("select * from main_menu where sub='' ORDER BY id ASC");
  $res1->execute();
  $res1->setFetchMode(PDO::FETCH_LAZY);
while($row = $res1->fetch()) {
    $id = $row['id'];

//gets submenus to the related main menu by sub id.
   $res2 = $db->prepare("select * from main_menu where sub = '$id'");
     $res2->execute();
     $res2->setFetchMode(PDO::FETCH_LAZY);
   }


and the temlpate part is :

Code:
<ul>                   
                    {foreach $res as $r}
                    <li><a href="#">{$r.id}</a>
                       
                        <ul>
                        {foreach $res2 as $r2}
                        <li><a href="#">{$r2.sub}</a></li>
                        {/foreach}
                        </ul>
                       
                        </li>
                    {foreachelse}
                    .. no results ..
                    {/foreach}
   
               </ul>



The output for above codes is :


Menu ID 1 | Menu ID 2 | Menu ID 3 | Menu ID 4
S Menu 1
S Menu 1


All submenus with the greatest id are displayed under first Main menu (Menu ID 1) only and other main menus remain blank.

I have developed same thing using mysql_query method and without smarty, but i want to create same thing using smarty.
Can anybody tell me the proper approach to this, i will be appreciate your help.

Thank you!
Back to top
View user's profile Send private message
dleffler
Smarty Rookie


Joined: 20 Sep 2011
Posts: 8

PostPosted: Mon Dec 19, 2011 4:54 pm    Post subject: Inaccurate looping logic Reply with quote

I won't call myself an expert in php, but your 'logic' in getting the submenus is flawed as the $res2 variable is reinitialized in each pass of the while statement so that it would only contain the last $db pull. Evenmoreso, the template logic is flawed since the inner loop ($res2) isn't referencing the outer loop ($res1).

You might try changing the php code to simply get the entire menu:
Code:
//Gets main menu from the table ("sub" is a column which tells whether it is main menu or sub menu, it contains an id of main menu for the submenu)
$res = $db->prepare("select * from main_menu where sub='' ORDER BY id ASC, sub ASC");
  $res->execute();
  $res->setFetchMode(PDO::FETCH_LAZY);
and the template to something like:
Code:
               <ul>                   
                    {foreach $res as $r}
                      {if (empty($r.sub))}<li><a href="#">{$r.id}</a>{/if}
                      {foreach $res as $r2}
                        {if (!empty($r2.sub)) && $r2.sub == $r.id)}
                          <ul>
                            <li><a href="#">{$r2.sub}</a></li>
                          </ul>
                        {/if}
                      {/foreach}
                      {if (empty($r.sub))}</li>{/if}
                    {foreachelse}
                    .. no results ..
                    {/foreach}
   
               </ul>
where the first foreach loop runs through the array pulling out only the main menu items and the inner foreach loop only pulls out submenu items matching the main menu id.
Back to top
View user's profile Send private message
U.Tews
Administrator


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

PostPosted: Mon Dec 19, 2011 5:54 pm    Post subject: Reply with quote

See similar problem and it's solution here http://www.smarty.net/forums/viewtopic.php?t=19590

The main idea is to create a nested array. To each row of the main menu/categorie and and array of the belonging submenues/categorie.

The solution of dleffer is not efficient as for each main entrie you loop through all entries and must do a lot of conditional testing.
Back to top
View user's profile Send private message
websterz
Smarty n00b


Joined: 17 Dec 2011
Posts: 2

PostPosted: Mon Dec 19, 2011 6:17 pm    Post subject: Reply with quote

Well, i appreciate both of your efforts, i just wrote a new script and got the problem solved, here is the code, i hope it will solve others problem regarding the menu creation.

PHP CODE:

Code:
$res = $db->prepare("select * from main_menu where sub='' ORDER BY id ASC");
$res->execute();
$i=0;
while($row= $res->fetch())
{
   $idr = $row['id'];

   $res1 = $db->prepare("select * from main_menu where sub='$idr' ORDER BY id ASC");
   $e = $res1->execute();

   while($row2 = $res1->fetch())
      {
            $sub[] = $row2;      
      }
   $smarty->assign('sub',$sub);
   $id[$i++] = $row;
}
$smarty->assign('menu',$id);



AND here is the Template Code:
Code:
<ul>
                    <li><a href="" class="staticlinks">Home</a><li>
                   
                    {foreach $menu as $main_menu}
                    <li><a href="">{$main_menu.id}</a>
                    <ul>
                    {foreach $sub as $sub_menu}
                    {if $main_menu.id == $sub_menu.sub}
                    <li><a href="">{$sub_menu.sub}</a></li>
                    {/if}
                    {/foreach}
                    </ul>
                    </li>
               {/foreach}
                   
</ul>


It gives me the same required result.
Thank you very much![/code]
Back to top
View user's profile Send private message
U.Tews
Administrator


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

PostPosted: Mon Dec 19, 2011 7:59 pm    Post subject: Reply with quote

Your solution has also the drawback that for each entry in $menu you loop over all entries in $sub.

With just a small modification you can avoid it by creation of a nested array.

Code:
$res = $db->prepare("select * from main_menu where sub='' ORDER BY id ASC");
$res->execute();
$i=0;
while($row= $res->fetch())
{
   $idr = $row['id'];

   $res1 = $db->prepare("select * from main_menu where sub='$idr' ORDER BY id ASC");
   $e = $res1->execute();

   $sub = array();                        <---------
   while($row2 = $res1->fetch())
      {
            $sub[] = $row2;     
      }
   $row['submenus'] = $sub;         <---------
   $id[$i++] = $row;
}
$smarty->assign('menu',$id);


Code:
<ul>
                    <li><a href="" class="staticlinks">Home</a><li>
                   
                    {foreach $menu as $main_menu}
                    <li><a href="">{$main_menu.id}</a>
                    <ul>
 ------->         {foreach $main_menu.submenus as $sub_menu}
                    <li><a href="">{$sub_menu.sub}</a></li>
                    {/if}
                    {/foreach}
                    </ul>
                    </li>
               {/foreach}
                   
</ul>
Back to top
View user's profile Send private message
devendra
Smarty n00b


Joined: 24 Nov 2012
Posts: 2

PostPosted: Thu Dec 06, 2012 11:22 am    Post subject: Drop down list Reply with quote

can we make drop down list on sub menu for its appropriate option.

for example,

Menu :- Department
Sub Menu :- IT Department
And drop down list on mouse hover effect :-

Faculty
Time Table
Syllabus
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 -> Tips and Tricks 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