0

I'm able to generate output as:

Cat1
SubCat1
SubCat3

However I need the output as

Cat1
Subcat1
 SubCat2
SubCat3

Well to build my menu, i use the db structure as :

site_id | parent_id    | site_name    | site_url  | Level
1       | 0            | Category 1   |example.com|   0
2       | 1            | Sub Cat 1    |example.com|   1
3       | 2            | Sub Cat 2    |example.com|   2
4       | 1            | Sub Cat 3    |example.com|   1

I'm able to generate my array as:

Array ( 
    [0] => Array ( 
        [0] => 63 
        [site_id] => 63 
        [1] => 0 
        [parent_id] => 0
        [2] => Category 1 
        [site_name] => Category 1 
        [3] => Link1 
        [site_url] => Link1 
        [4] => 0 
        [level] => 0 
        [children] => Array ( 
            [0] => Array ( 
                [0] => 66 
                [site_id] => 66 
                [1] => 63 
                [parent_id] => 63 
                [2] => Sub Cat 3 
                [site_name] => Sub Cat 3 
                [3] => 
                [site_url] => 
                [4] => 1
                [level] => 1 
                [children] => Array ( ) 
            ) 
            [1] => Array ( 
                [0] => 64
                [site_id] => 64 
                [1] => 63 
                [parent_id] => 63 
                [2] => Sub Cat 1
                [site_name] => Sub Cat 1 
                [3] => 
                [site_url] => 
                [4] => 2 
                [level] => 2
                [children] => Array ( 
                    [0] => Array ( 
                        [0] => 65 
                        [site_id] => 65 
                        [1] => 64 
                        [parent_id] => 64 
                        [2] => Sub Cat2 
                        [site_name] => Sub Cat2 
                        [3] =>
                        [site_url] => 
                        [4] => 1 
                        [level] => 1 
                        [children] => Array ( ) 
                    ) 
                ) 
            ) 
        ) 
    )
)

using the function from : https://stackoverflow.com/a/2795069/1137983. I have used only getTopCategories() and getCategories function from the post

Inorder to generate the output, I use:

{foreach from=$sitemap item=c name=sitemap}
 {if $c.level==0 }
<li><h2><a title="{$c.site_name}" href="{$c.site_url}">{$c.site_name}</a></h2><ul>
    {foreach item=d from=$c.children name=sitemap} 
<li><a title="{$d.site_name}" href="{$d.site_url}">{$d.site_name}</a></li>
    {/foreach}
{else}  
<li><h2><a title="{$c.site_name}" href="{$c.site_url}">{$c.site_name}</a></h2><ul>
{/if}
</ul>
</li>
{/foreach}  
</ul>
Community
  • 1
  • 1

1 Answers1

1

You can try to use this - this is the part of SiMan CMS but you can simply change it for your data structure:

    function siman_load_menu($menu_id, $maxlevel=-1)
        {
            global $nameDB, $lnkDB, $_servervars, $tableprefix, $_settings, $special;
            $i=0;
            $addsql='';
            if ($maxlevel>=0)
                $addsql.=' AND submenu_from=0 ';
            $sql="SELECT * FROM ".$tableprefix."menu_lines WHERE id_menu_ml=".intval($menu_id)." $addsql ORDER BY submenu_from, position";
            $result=database_db_query($nameDB, $sql, $lnkDB);
            while ($row=database_fetch_object($result))
                {
                    $menu[$i]['id']=$row->id_ml;
                    $menu[$i]['mid']=$menu_id;
                    $menu[$i]['pos']=$row->position;
                    $menu[$i]['add_param']=$menu_id.'|'.$row->id_ml;
                    $menu[$i]['level']=1;
                    $menu[$i]['submenu_from']=$row->submenu_from;
                    $menu[$i]['sublines_count']=0;
                    $menu[$i]['url']=$row->url;
                    $menu[$i]['caption']=$row->caption_ml;
                    $menu[$i]['partial']=$row->partial_select;
                    $menu[$i]['alt']=$row->alt_ml;
                    $menu[$i]['attr']=$row->attr_ml;
                    $menu[$i]['newpage']=$row->newpage_ml;
                    $line_id=$row->id_ml;
                    $i++;
                }
            $maxlev=0;
            for ($i=0; $i<count($menu); $i++)
                {
                    $pos[$i]=0;
                }
            $fistlevelposition=0;
            $fistlevellastposition=0;
            for ($i=0; $i<count($menu); $i++)
                {
                    if ($menu[$i]['submenu_from']==0)
                        {
                            $maxpos=0;
                            for ($j=0; $j<count($menu); $j++)
                                if ($maxpos<$pos[$j])
                                    $maxpos=$pos[$j];
                            $pos[$i]=$maxpos+1;
                            $fistlevelposition++;
                            $menu[$i]['submenu_position']=$fistlevelposition;
                            $fistlevellastposition=$i;
                        }
                    else
                        {
                            $rootpos=0;
                            $childpos=-1;
                            for ($j=0; $j<count($menu); $j++)
                                {
                                    if ($menu[$j]['id']==$menu[$i]['submenu_from'])
                                        {
                                            $rootpos=$pos[$j];
                                            $menu[$i]['level']=$menu[$j]['level']+1;
                                            $menu[$j]['sublines_count']++;
                                            $menu[$j]['is_submenu']=1;
                                            $menu[$i]['submenu_position']=$menu[$j]['sublines_count'];
                                        }
                                    if ($menu[$j]['submenu_from']==$menu[$i]['submenu_from'] && $j!=$i && $childpos<$pos[$j])
                                        $childpos=$pos[$j];
                                }
                            $pos[$i]=($rootpos>$childpos) ? ($rootpos+1) : ($childpos+1) ;
                            for ($j=0; $j<count($menu); $j++)
                                {
                                    if ($pos[$j]>=$pos[$i] && $j!=$i)
                                        $pos[$j]++;
                                }
                        }
                }
            if (count($menu)>0)
                {
                    $menu[0]['first']=1;
                    $menu[$fistlevellastposition]['last']=1;
                }
            for ($i=0; $i<count($menu); $i++)
                {
                    $rmenu[$pos[$i]-1]=$menu[$i];
                }
            return $rmenu;
        }

In the output you'll get an array with the key level which define how much level is your menu item.

{section name=i loop=$menu}
{if $smarty.section.i.index eq 0}{else}<br>{/if}
{section name=j loop=$menu[i].level start=1} - {/section}
<a href="{$menu[i].url}" >{$menu[i].caption}</a>
{/section}
Roman S
  • 162
  • 8
  • Thank you very much, but it would be helpful if you could show me your table fields so that it can be easier to understand the code – Vishal Kumar Verma Sep 07 '12 at 04:51
  • 1
    I think that you interested in these fields: id_ml - navigation item identifier, submenu_from - id of root navigation item (this field is zerofilled for top level item). Or you can download and install SiMan CMS and look to their solution live – Roman S Sep 07 '12 at 11:32
  • Thanks, I went through it, it's working great however inorder to use for my project, some modifications need to be done. Working on it now.. – Vishal Kumar Verma Sep 08 '12 at 18:45
  • Thank you for the answer, i had took some time to understand it n was able to change according to my needs.. And it works perfectly.. Just some alignments to be done now.. thank you – Vishal Kumar Verma Sep 08 '12 at 21:21
  • You're welcome. Sorry for late answer - I missed notification about your comments. – Roman S Sep 08 '12 at 23:20
  • Sorry to bother you again..But I have some issues again, the smarty section is working fine.. However i need to put into this order:
    • Main
      • Sub Main
        • Sub Sub Main
    Could you help me
    – Vishal Kumar Verma Sep 13 '12 at 12:28
  • I don't think that you can realize this without recursion :( – Roman S Sep 13 '12 at 23:49
  • Could you tell me how do it with recursion, I'll try to present it to my higher authorities and try explaining them.. – Vishal Kumar Verma Sep 14 '12 at 03:56