Page 1 of 1

Incrementing a variable within a function

Posted: Sun Jun 14, 2009 3:17 pm
by dsided
I'm adjusting a Joomla menu module called extended menu. I want to be able to give each <li> a unique class so I can use a single large image sprite for the navigation.

I thought the best way would be to increment a variable and add this to the end of the li class, so the output would be:

Code: Select all

<li class=menuitem1>Home</li>
<li class=menuitem2>About</li>
<li class=menuitem3>Links</li>
Unfortunately it outputs each list item with the same number.

Code: Select all

<li class=menuitem2>Home</li>
<li class=menuitem2>About</li>
<li class=menuitem2>Links</li>
Here the code, my variable is called a. Thanks in advance for your help!

Code: Select all

foreach(array_keys($menuNodeList) as $id) {
            $menuNode           =& $menuNodeList[$id];
            $itemHierarchy      = $hierarchy;
            $itemHierarchy[]    = (1 + $index);
                       /* New variable */           
                       $a = 1;
            $result .= '<li';
            if ($this->activeMenuClassContainer) {
        $a++;           
                $result .= ' class="test_'. $a .$this->getContainerMenuClassName($menuNode, $level).'"';
                                  /* Previous code: */
                /*  $result .= 'class="test_'.$this->getContainerMenuClassName($menuNode, $level).'"'; */
            }
            if ($this->hierarchyBasedIds) {
                $result .= ' id="menuitem_'.$this->getHierarchyString($itemHierarchy).$this->idSuffix.'"';
            }
            $result .= '>';
            $linkOutput = $this->mosGetMenuLink($menuNode, $level, $this->params, $itemHierarchy);
            $result .= $linkOutput;
            if (($level < $this->maxDepth) && ($menuNode->isExpanded())) {
                $subMenuNodeList    =& $menuNode->getChildNodeList();
                if (count($subMenuNodeList) > 0) {
                    $result .= $this->_renderMenuNodeList($subMenuNodeList, $level+1, $itemHierarchy);
                }
            }
            $result .= '</li>';

Re: Incrementing a variable within a function

Posted: Sun Jun 14, 2009 6:56 pm
by califdon
I see that you assigned the value of 1 to the variable $a, and that's what it is using. If you want it to increment, you will have to increment it after you use it. In PHP, you can increment with just the statement: $a++;

Re: Incrementing a variable within a function

Posted: Fri Jun 19, 2009 9:31 am
by dsided
Thank you both very much!