NickBogi Posted January 29, 2013 Share Posted January 29, 2013 I am writing my own theme and I came up on a PHP problem. Here is the original default.php i was working with which worked well for what it did: public function process($module, $element) { self::_process($module, $element->first('ul:first')); return $element; } /* Function: _process Returns: Void */ protected static function _process($module, $element, $level = 0) { if ($level == 0) { $element->attr('class', 'menu '.$module->menu_style); } else { $element->addClass('level'.($level + 1)); } foreach ($element->children('li') as $li) { // is active ? if ($active = $li->attr('data-menu-active')) { $active = $active == 2 ? ' active current' : ' active'; } // is parent ? $ul = $li->children('ul'); $parent = $ul->length ? ' parent' : null; // set class in li $li->attr('class', sprintf('level%d item%s'.$parent.$active, $level + 1, $li->attr('data-id'))); // set class in a/span foreach ($li->children('a,span') as $child) { // get title $title = $child->first('span:first'); // set subtile $subtitle = $title ? explode('||', $title->text()) : array(); if (count($subtitle) == 2) { $li->addClass('hassubtitle'); $title->html(sprintf('<span class="title">%s</span><span class="subtitle">%s</span>', trim($subtitle[0]), trim($subtitle[1]))); } // set image if ($image = $li->attr('data-menu-image')) { $title->prepend(sprintf('<span class="icon" style="background-image: url(\'%s\');"> </span>', $image)); } $child->addClass(sprintf('level%d'.$parent.$active, $level + 1)); } // process submenu if ($ul->length) { self::_process($module, $ul->item(0), $level + 1); } } The problem is, I am changing my menu style and I now need to differentiate between the first menu item, the last, and any in between (to add in a CSS class). So here is what I did (changes in RED) with the code but it is not working: public function process($module, $element) { self::_process($module, $element->first('ul:first')); return $element; } /* Function: _process Returns: Void */ protected static function _process($module, $element, $level = 0) { if ($level == 0) { $element->attr('class', 'menu '.$module->menu_style); } else { $element->addClass('level'.($level + 1)); } foreach ($element->children('li') as $li) { // is active ? if ($active = $li->attr('data-menu-active')) { $active = $active == 2 ? ' active current' : ' active'; } // is parent ? $ul = $li->children('ul'); $parent = $ul->length ? ' parent' : null; [color=#ff0000] // is first or last ? $lis = $element->children("ul"); for($forl=0,$imax=count($lis);[/color] [color=#ff0000] $forl<$imax;$forl++){ $forl = array(); if (forl==0) $position_n = 'first'; elseif ($forl==$imax-1) $position_n = 'last';[/color] [color=#ff0000] else $position_n = null; }[/color] // set class in li $li->attr('class', sprintf('level%d item%s'.$parent.$active, $level + 1, $li->attr('data-id'), [color=#ff0000]$position_n[/color])); // set class in a/span foreach ($li->children('a,span') as $child) { // get title $title = $child->first('span:first'); // set subtile $subtitle = $title ? explode('||', $title->text()) : array(); if (count($subtitle) == 2) { $li->addClass('hassubtitle'); $title->html(sprintf('<span class="title">%s</span><span class="subtitle">%s</span>', trim($subtitle[0]), trim($subtitle[1]))); } // set image if ($image = $li->attr('data-menu-image')) { $title->prepend(sprintf('<span class="icon" style="background-image: url(\'%s\');"> </span>', $image)); } $child->addClass(sprintf('level%d'.$parent.$active, $level + 1)); } // process submenu if ($ul->length) { self::_process($module, $ul->item(0), $level + 1); } } Quote Link to comment https://forums.phpfreaks.com/topic/273777-counting-menu-items/ Share on other sites More sharing options...
NickBogi Posted January 29, 2013 Author Share Posted January 29, 2013 sorry, the color didn't come through in the code. Just look for anything in between the shortcodes to see what i was trying to highlight red Quote Link to comment https://forums.phpfreaks.com/topic/273777-counting-menu-items/#findComment-1408894 Share on other sites More sharing options...
NickBogi Posted January 30, 2013 Author Share Posted January 30, 2013 I found a few errors and have modified the code which I will paste below. That seems to have helped get me 1/3 of the way there. What I mean is that I am now successfully appending the class for all LIs. Unfortunately, they are all being appended with "first" regardless of whether they are the first, middle or last. See the pic below for the developer tools window of the page's code: And here is the code I am now using. ANY HELP WOULD BE SOOOO APPRECIATED! public function process($module, $element) { self::_process($module, $element->first('ul:first')); return $element; }[/background][/size][/font][/color] [color=#464646][font='Helvetica Neue', Helvetica, Arial, sans-serif][size=3][background=rgb(244, 244, 244)]/* Function: _process[/background][/size][/font][/color] [color=#464646][font='Helvetica Neue', Helvetica, Arial, sans-serif][size=3][background=rgb(244, 244, 244)] Returns: Void */ protected static function _process($module, $element, $level = 0) {[/background][/size][/font][/color] [color=#464646][font='Helvetica Neue', Helvetica, Arial, sans-serif][size=3][background=rgb(244, 244, 244)] if ($level == 0) { $element->attr('class', 'menu '.$module->menu_style); } else { $element->addClass('level'.($level + 1)); } foreach ($element->children('li') as $li) { // is active ? if ($active = $li->attr('data-menu-active')) { $active = $active == 2 ? ' active current' : ' active'; } // is parent ? $ul = $li->children('ul'); $parent = $ul->length ? ' parent' : null;[/background][/size][/font][/color] [color=#464646][font='Helvetica Neue', Helvetica, Arial, sans-serif][size=3][background=rgb(244, 244, 244)] // is first or last ? $lis = $element->children("li"); for($forl=0,$imax=count($lis); $forl<$imax;$forl++){ if ($forl==0) $position_n = 'first'; elseif ($forl==$imax-1) $position_n = 'last'; else $position_n = null; } // set class in li $li->attr('class', sprintf('level%d item%s '. $position_n .$parent.$active, $level + 1, $li->attr('data-id'))); // set class in a/span foreach ($li->children('a,span') as $child) {[/background][/size][/font][/color] [color=#464646][font='Helvetica Neue', Helvetica, Arial, sans-serif][size=3][background=rgb(244, 244, 244)] // get title $title = $child->first('span:first');[/background][/size][/font][/color] [color=#464646][font='Helvetica Neue', Helvetica, Arial, sans-serif][size=3][background=rgb(244, 244, 244)] // set subtile $subtitle = $title ? explode('||', $title->text()) : array(); if (count($subtitle) == 2) { $li->addClass('hassubtitle'); $title->html(sprintf('<span class="title">%s</span><span class="subtitle">%s</span>', trim($subtitle[0]), trim($subtitle[1]))); }[/background][/size][/font][/color] [color=#464646][font='Helvetica Neue', Helvetica, Arial, sans-serif][size=3][background=rgb(244, 244, 244)] // set image if ($image = $li->attr('data-menu-image')) { $title->prepend(sprintf('<span class="icon" style="background-image: url(\'%s\');"> </span>', $image)); }[/background][/size][/font][/color] [color=#464646][font='Helvetica Neue', Helvetica, Arial, sans-serif][size=3][background=rgb(244, 244, 244)] $child->addClass(sprintf('level%d'.$parent.$active, $level + 1)); }[/background][/size][/font][/color] [color=#464646][font='Helvetica Neue', Helvetica, Arial, sans-serif][size=3][background=rgb(244, 244, 244)] // process submenu if ($ul->length) { self::_process($module, $ul->item(0), $level + 1); } } [/background][/size][/font][/color] [color=#464646][font='Helvetica Neue', Helvetica, Arial, sans-serif][size=3][background=rgb(244, 244, 244)] } Quote Link to comment https://forums.phpfreaks.com/topic/273777-counting-menu-items/#findComment-1409086 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.