0

I have an if statement that is at the end of a for each loop that adds the | character at the end of a generated unordered list.

My goal is to add | after "not the last two".

<?php if (! $_link->getIsLast()):?>|<?php endif;?>

I was hoping I could input a value in the getIsLast or getIsFirst functions like this:

<?php if (! $_link->getIsLast(2)):?>|<?php endif;?>

I was hoping the above statement would add the | character for each generated list item except for the last two. However, it doesn't seem to be working.

Does anyone know the proper syntax to do something like this?

The for each loops is below:

<ul class="links pull-right"<?php if($this->getName()): ?> id="<?php echo $this->getName() ?>"<?php endif;?>>
    <?php foreach($_links as $_link): ?>
        <?php if ($_link instanceof Mage_Core_Block_Abstract):?>
            <?php echo $_link->toHtml() ?>
        <?php else: ?>
            <li<?php if($_link->getIsFirst()||$_link->getIsLast()): ?> class="<?php if($_link->getIsFirst()): ?>first<?php endif; ?><?php if($_link->getIsLast()): ?> last<?php endif; ?>"<?php endif; ?> <?php echo $_link->getLiParams() ?>><?php echo $_link->getBeforeText() ?><a href="<?php echo $_link->getUrl() ?>" title="<?php echo $_link->getTitle() ?>" <?php echo $_link->getAParams() ?>><?php echo $_link->getLabel() ?></a><?php echo $_link->getAfterText() ?></li>
        <?php endif;?>
        <?php if (! $_link->getIsLast()):?>|<?php endif;?>
    <?php endforeach; ?>
</ul>
3
  • so what you are saying is you want to place the | only for items m..n where m is some starting index and n is an ending index of the list? Commented Apr 14, 2013 at 23:46
  • 1
    Why all this complication just to write a |? echo "|"; does the same crap. Commented Apr 14, 2013 at 23:49
  • My Account | My Wishlist | My Cart (1 item) | Checkout ----Looking for something like this. Note that there are five items total and the last one is not listed, hence, I am trying to add the | for all but the last two items. Commented Apr 14, 2013 at 23:57

1 Answer 1

1

Try this.

<ul class="links pull-right"<?php if($this->getName()): ?> id="<?php echo $this->getName() ?>"<?php endif;?>>
    <?php 
        $numberOfRows = count($_links);
        $currentIndex=0;
        foreach($_links as $_link): 
    ?>
        <?php if ($_link instanceof Mage_Core_Block_Abstract):?>
            <?php echo $_link->toHtml() ?>
        <?php else: ?>
            <li<?php if($_link->getIsFirst()||$_link->getIsLast()): ?> class="<?php if($_link->getIsFirst()): ?>first<?php endif; ?><?php if($_link->getIsLast()): ?> last<?php endif; ?>"<?php endif; ?> <?php echo $_link->getLiParams() ?>><?php echo $_link->getBeforeText() ?><a href="<?php echo $_link->getUrl() ?>" title="<?php echo $_link->getTitle() ?>" <?php echo $_link->getAParams() ?>><?php echo $_link->getLabel() ?></a><?php echo $_link->getAfterText() ?></li>
        <?php endif;?>
        <?php if ($currentIndex<$numberOfRows-2):?>|<?php endif;?>
    <?php 
    $currentIndex++;
    endforeach; ?>
</ul>
|
Sign up to request clarification or add additional context in comments.

4 Comments

I guess not since when I tried this, it added the | for every case.
That should be <=, otherwise it will skip the last 3.
The variable names is for sample. You may have another before the foreach. Why do you not post the foreach statement to us?
No, it will not Mark Parnell. $numberofRows is not the same that a max index. An Array with indexs of 0 to 4 have 5 rows. So, writting only < will already remove this difference.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.