Incrementing a variable within a function

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
dsided
Forum Newbie
Posts: 2
Joined: Sun Jun 14, 2009 2:49 pm

Incrementing a variable within a function

Post 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>';
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Incrementing a variable within a function

Post 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++;
dsided
Forum Newbie
Posts: 2
Joined: Sun Jun 14, 2009 2:49 pm

Re: Incrementing a variable within a function

Post by dsided »

Thank you both very much!
Post Reply