Welcome Guest, Not a member yet? Register   Sign In
Multi-level dynamic menus
#1

[eluser]shinokada[/eluser]
I need to create more than one menus dynamically.

I search wiki, but I could not find any.

I am not sure if this http://codeigniter.com/wiki/Nested_sets/ can be used for one table for the menu.

I will appreciate any advices.
#2

[eluser]stommert[/eluser]
Hi,

As you might know, dynamic menu's with unlimited submenu's are a pain.
the nested sets implementation of mysql is nice but hard to deal with when creating, updating and deleting items, because the whole set has to be recalculated.

When you decide to only have one sublevel in your menu, I would suggest you use parent_id's.
When this doesn't do the trick for you, You can decide to make loops in loops in loops etc. as you might know, this is a bit dirty and slow. On the other hand it is quite manageable.
And there are many other way's to Rome.
<a href="http://ellislab.com/forums/viewthread/132374/">see this post</a>
#3

[eluser]bgreene[/eluser]
you can also use just 2 functions, the second one calling itself recursively.
this has the advantage of allowing submenus ad nauseum
function gettabs(){
$tabs = result of select all tabs, ordered by pntid
$pntid = 0;
return _addmenuitems($pntid, $tabs);
}

function _addmenuitems($pntid, $tabs) {
foreach tab with this pntid = $pntid {
1. create menu item
2. append subtabs of this tab
_addmenuitems($tab->id,$tabs)
}
}
#4

[eluser]shinokada[/eluser]
@bgreene Is it possible to add more detailed code?
#5

[eluser]bgreene[/eluser]
not in the office at the moment, back in an hour or two, will add more code then
#6

[eluser]bgreene[/eluser]
Ok, here we go. Take a hefty slug of brandy before reading the code - I'm only a beginner at php. You can see it in operation (and see the resultant html) on ecoland.com - its my first website so consider it an alpha. However, the menus work which is what you are interested in.

function get_tabs_side($controller, $pnt)
{
$sql = 'SELECT c2g,pnt_c2g,href,english AS title FROM tabs WHERE (deleted = 0)'
. ' ORDER BY pnt_c2g,seq,title';
$query = $this->db->query($sql);
$iIndx = 1;
$iLevel = 0;
return chr(10) . $this->_get_tabs_side('',$query->result(), $controller, $pnt, $iIndx, 0, $iLevel, TRUE);
}

function _get_tabs_side($leader, $tabs, $controller, $parentc2g, $iIndex, $iclrinit, $iLevel, $init)
{
$output = '';
if ($init) $output .= $leader . '<ul class="mnubtns" id="ul_1">' . chr(10);
$itabs = 0;
foreach($tabs as $tab) if ($tab->pnt_c2g == $parentc2g) $itabs ++;

$isssl = $this->config->is_ssl();
if ($itabs > 0)
{
if (! $init) {
// add a submenu and hide it until user clicks on it
$output .= $leader .'<ul style="display: none;" class="mnubtns ulsub" id="ul_' . $iIndex . '">' . chr(10);
}
$icnt = 0; // tab index
$iclr = $iclrinit; // colour index
foreach($tabs as $tab)
{
if ($tab->pnt_c2g == $parentc2g)
{
// check if we have sub tabs
$hassubtabs = FALSE;
if ($controller == '')
foreach($tabs as $stb) {
if ($stb->pnt_c2g == $tab->c2g) {
$hassubtabs = TRUE;
break;
}
}
if ($tab->href == '') {$str = $tab->c2g; } else {$str = $tab->href;}
if ( $init ) {
$class = 'class="mnubtn mnubtn_' . $iclr .' mnulvl_'.$iLevel.'"';
$iclr ++;
if ($iclr > 16) $iclr = 0;
} else {
$class = 'class="mnubtn mnubtn_' . $iclr .' mnulvl_'.$iLevel.'"';
$iclr ++;
if ($iclr > 16) $iclr = 0;
}

if ($hassubtabs == TRUE) $tit = '+&nbsp;'.$tab->title;
else $tit = '&nbsp;&nbsp;&nbsp;'.$tab->title;
if ($controller == '')
$output .= $leader . ' <li id="li_'.($iIndex*100 + $icnt).'" '.$class.'>'
. '<a href="#">' . $tit . '</a>';
else
$output .= $leader . ' <li id="li_'.($iIndex*100 + $icnt).'" '.$class.'>'
. anchor($controller.'/'.$tab->href, $tit, '', $isssl);
if ($hassubtabs == TRUE)
$output .= chr(10) . $this->_get_tabs_side($leader . ' ',$tabs,
$controller, $tab->c2g, $iIndex*100+$icnt, $iclr, $iLevel+1, FALSE);
$output .= $leader . ' </li>' . chr(10);
$icnt ++;
};
}
if (! $init) $output .= $leader . '</ul>' . chr(10);
$iIndex ++;
}
if ($init) $output .= $leader . '</ul>' . chr(10);
return $output;
}
#7

[eluser]shinokada[/eluser]
@bgreene Can I ask one more favour? Can you add sql damp as well for the menu part? I know I am asking a lot.
But I appreciate it.
Thanks again.
#8

[eluser]bgreene[/eluser]
is this what you want? (only a small sample as full list has 360 rows)
"c2g";"pnt_c2g";"href";"title"
"1";"0";"home";"Home"
"2";"0";"shop";"On-Line Shop"
"4";"0";"info";"Information"
"5";"0";"bespoke";"Promotional Products"
"16";"0";"checkout";"Checkout"
"18";"0";"personal";"Personal"
"90";"1";"about";"About us"
"91";"1";"contact";"Contact Us"
"765";"1";"feedback";"Feedback"
"768";"1";"links";"Links"
"92";"1";"privacy";"Privacy"
"93";"1";"support";"Support"
"94";"1";"staff";"The people"
"40";"2";"bags.htm";"Bags And Packaging"
"41";"2";"cater.htm";"Catering Products"
"714";"2";"cleaning.htm";"Cleaning"
"713";"2";"computing.htm";"Computing"
"715";"2";"desktop.htm";"Desktop Accessories"
"232";"2";"envelopes.htm";"Envelopes"
"350";"2";"bindingaccessories.htm";"Filing, Binding &amp; Folders"
"317";"2";"designstationary.htm";"Gift &amp; Design Stationery"
"384";"2";"ecogimmicks.htm";"Gifts &amp; Toys"
"712";"2";"stationary.htm";"Notebooks &amp; Pads"

and further down
"22";"28";"copyofficepaper.htm";"Copy/Laser/Inkjet Paper"
"677";"28";"card.htm";"Copy/Laser/Inkjet Card"
"620";"28";NULL;"Fax Rolls"
"383";"28";NULL;"OHP Clear Film"
"24";"28";NULL;"Plotter Paper - A2"
"25";"28";"corporatestationary.htm";"Corporate Stationary"
"720";"29";"new_orgcot.htm";"Organic Cotton Bags"
"719";"29";"cater.htm";"PLA Catering"
"628";"40";"bags_org.htm";"Bags-Cotton Organic"
"42";"40";"bags_cot.htm";"Bags-Cotton Standard"
"278";"40";"othercarrierbags.htm";"Bags-Other"
"625";"40";"bags_pap.htm";"Bags-Paper Carriers"
"448";"40";"cutting.htm";"Cutting"
"449";"40";"rubberbands.htm";"Elastics &amp; String"
"447";"40";"tape.htm";"Tape"
"716";"40";"packaging.htm";"Wrapping"
"641";"41";NULL;"PLA Corn Starch"
"642";"41";NULL;"Potato Starch Cutlery"
"639";"41";NULL;"Serviettes"
"643";"41";NULL;"Sugar Cane Catering"
and lots more...




Theme © iAndrew 2016 - Forum software by © MyBB