Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/278.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP强制将计数器重置为0_Php_Drupal - Fatal编程技术网

PHP强制将计数器重置为0

PHP强制将计数器重置为0,php,drupal,Php,Drupal,我有一个菜单项计数器,基本上作为一个类向菜单系统添加增量值: <?php if ($element['#original_link']['depth'] == 1) { static $counter = 0; $counter++; $class = 'count-' . $counter; } $output = 'some output code build'; return '<li class="

我有一个菜单项计数器,基本上作为一个类向菜单系统添加增量值:

    <?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>';
    ?>

注意,代码在每个菜单项内(循环或数组外)。代码将简单地输出无序列表的列表,不带UL:

<li class="count-1">One</li>
<li class="count-2">Two</li>, ...etc.
  • 一个
  • 两个……等等。
    在我更改菜单源之前,这一切都很正常

    1) 。一个是使用我的CMS系统菜单

    2) 。后者使用该系统菜单的块输出

    两者都输出类似的菜单结构,除了后者从#1继续计数器而不是从1复位(尽管#2接管位置后#1不会被激活)。我还不知道为什么,但似乎2是1的延续。而我希望每个都应该从1开始递增

    我不能在这里使用重置。除了我的CMS对计数器的处理方式之外,PHP在这里还可以处理什么明显的问题吗

    任何指挥员都将不胜感激。谢谢

    更新,实际使用的代码:

    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>';
    }
    
    函数mytheme\u菜单\u链接(数组$variables){
    //dpm(变量);
    $element=$variables['element'];
    $sub_菜单=“”;
    如果($element['#below'])){
    $sub#u menu=drupal_render($element['#below']);
    }
    if($element['#original_link']['menu_name']==variable_get('menu_main_links_source','main menu')){
    如果($element['#original_link']['depth']==1){
    静态$counter=0;
    $counter++;
    $element['#attributes']['class'][]='count-'.$counter;
    }
    }
    $output=l($element['#title']、$element['#href']、$element['#本地化选项']);
    返回“.$output.$sub_菜单。”\n”;
    }
    /**
    *实现主题菜单树()
    没有有用的变量,除了树的展平渲染
    */
    函数mytheme_菜单树主菜单($variables){
    返回“
      ”.$variables['tree']”。
    ; }
    使用
    静态
    变量时,同一代码的后续执行之间不会重置变量值。这就是
    static
    的明确目的。如果您不希望出现这种行为,请删除它。

    谢谢,不幸的是,删除静态时没有增量值,这仅仅是因为代码块不在循环中。都有相似的类=“count-1”。那么我真的不明白这个问题。请提供更多的代码,最好是一个完整的自我包含的例子来演示问题。添加了菜单项列表及其容器UL功能。谢谢