Jump to content

Counting Menu Items


NickBogi

Recommended Posts

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);
  }
 }

Link to comment
Share on other sites

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:

 

Menu-Issue-Update.jpg

 

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)]  
}
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.