Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7798235
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T23:56:46+00:00 2026-06-01T23:56:46+00:00

I have a counter of menu items, that basically add incremental value as a

  • 0

I have a counter of menu items, that basically add incremental value as a class to a menu system:

    <?php
    if ($element['#original_link']['depth'] == 1) {
      static $counter = 0;
      $counter++;
      $class = 'count-' .  $counter;
    }
    $output = 'some output code build';
    return '<li class="' . $class . '">' .$output .'</li>';
    ?>

Note, the code is inside each menu item (outside a loop or array). The code will simply output the lists of unordered list, without UL:

<li class="count-1">One</li>
<li class="count-2">Two</li>, ...etc.

This works fine until I change the source of menus.

1). One is using the system menu of my CMS,

2). the latter is using a block output of that system menu,

and both output similar construct of menus, except for the fact that the latter continues the counter from the #1 rather than a reset from a 1 (although #1 is not activated once #2 takes over the place). I haven’t figured out why, but it seems that #2 is a continuance of #1. While I expect each should start an increment from 1.

I can not use a reset here. Is there anything obvious I missed that PHP can handle here, apart from the the way of my CMS do to the counters?

Any pointer would be very much appreciated. Thanks

UPDATE, actual codes in use:

function mytheme_menu_link(array $variables) {
  //dpm($variables);
  $element = $variables['element'];
  $sub_menu = '';

  if ($element['#below']) {
    $sub_menu = drupal_render($element['#below']);
  }

  if ($element['#original_link']['menu_name'] == variable_get('menu_main_links_source', 'main-menu')) {
    if ($element['#original_link']['depth'] == 1) {     
       static $counter = 0;
       $counter++;                    
       $element['#attributes']['class'][] = 'count-' .  $counter;
    }
  }

  $output = l($element['#title'], $element['#href'], $element['#localized_options']);
  return '<li' . drupal_attributes($element['#attributes']) . '>' . $output . $sub_menu . "</li>\n";
}

/**
 * Implements theme_menu_tree()
    No helpful variables, except for a flattened render of tree
 */
function mytheme_menu_tree__main_menu($variables) {
  return '<ul class="menu">' . $variables['tree'] . '</ul>';
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T23:56:47+00:00Added an answer on June 1, 2026 at 11:56 pm

    When using static variables, the values of the variable will not be reset between subsequent executions of the same code. That’s the express purpose of static. Get rid of it if you do not want that behavior.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a counter that counts up every 1 second and add 1 to
I have a counter hash that I am trying to sort by count. The
I have an app that collects Perfmon counter values through the API exposed in
I've designed a simple program that has a counter class and inside that counter
I have a counter field in a myisam table. To update the counter value
I'm trying to have a counter running within my jQuery tmpl so that I
I'm running the following code in my main class, just to have a counter
I have a counter running on one of my add input jquery functions: var
I have a counter in my app that has a reset button, but instead
I want to have a counter that counts down from 60 seconds to 0.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.