subnav loop: calculate if last entry

6 posts by 3 authors in: Forums > CMS Builder
Last Post: December 18, 2012   (RSS)

By buttermilk - December 15, 2012 - edited: December 15, 2012

Hello all,

The following is code that is more or less working on a combo page. It loops through a "category" section to display subnavigation:

+++++++++++++++++++++++++++
<div class="subnav">

<?php foreach ($rootsRecords as $listRecord): ?>

<?php $isSelected = ($listRecord['num'] == $detailRecord['num']); ?>

<?php if ($isSelected):?>
<div class="selected"><?php echo ($listRecord['name']) ?></div> |

<?php else: ?>
<a href="<?php echo htmlencode($listRecord['_link']) ?>"><?php echo htmlencode($listRecord['name']) ?></a> |

<?php endif; ?>

<?php endforeach ?>

<?php if (!$rootsRecords): ?>
No records were found!<br/><br/>
<?php endif ?>


<!--ends subnav-->
</div>
++++++++++++++++++++++

The results are here: http://imagineotherwise.ca/roots.php?Cherokee-Fires-2

You can see the following is displayed:

"A Brief Biography | Cherokee Fires | Places I've Called Home | Advocacy and Activism | My Coat of Arms | "

My problem is that I'd like to not have the pipe character "|" displayed after the final entry ("My Coat of Arms"). All entries are on the same category level. I know I probably need to nest another "if" statement in there that asks "is this the last entry in this section," but I just can't figure out how to do this. Any help would be much appreciated!

Thanks,

Ian
Hey Ian,

I would use css to do this... have a look at http://www.interactivetools.com/forum/forum-posts.php?ul-li-styling-issue-74000
Cheers,

Tim (toledoh.com.au)

By buttermilk - December 16, 2012 - edited: December 16, 2012

Thanks for taking the time to help me out, Tim. I've never used the "last-child" css selector.

I ended up including the following in my css for anybody else who's interested:

++++++++++++++++++++++++
.pipe {
color:#999;
}

.pipe:last-child {
display:none;
}

+++++++++++++++++

And this is the revised php code:

+++++++++++++++++
<?php foreach ($rootsRecords as $listRecord): ?>

<?php $isSelected = ($listRecord['num'] == $detailRecord['num']); ?>

<?php if ($isSelected):?>
<div class="selected"><?php echo ($listRecord['name']) ?></div> <span class="pipe">|</span>

<?php else: ?>
<a href="<?php echo htmlencode($listRecord['_link']) ?>"><?php echo htmlencode($listRecord['name']) ?></a> <span class="pipe">|</span>

<?php endif; ?>

<?php endforeach ?>

<?php if (!$rootsRecords): ?>
No records were found!<br/><br/>
<?php endif ?>

++++++++++++++++++++++++++++++++

I'm still checking to see if this is the proper way to do it, but it does seem to work on the browsers I have installed (as far as I can tell).

Best,

Ian
The other thing you could do is apply some styles to the <a>;

#navigation a{ padding: 5px 20x; border-right: 1px solid #999; }
#navigation a:last-child{ border:none;}

Then, have the loop as:

<div id="navigation">
<?php foreach ($rootsRecords as $listRecord): ?>

<?php $isSelected = ($listRecord['num'] == $detailRecord['num']); ?>

<?php if ($isSelected):?>
<div class="selected"><?php echo ($listRecord['name']) ?></div>
<?php else: ?>
<a href="<?php echo htmlencode($listRecord['_link']) ?>"><?php echo htmlencode($listRecord['name']) ?></a>

<?php endif; ?>

<?php endforeach ?>

</div>
Cheers,

Tim (toledoh.com.au)

By aev - December 17, 2012

Hi,

for category sections you can check among these fields for controlling various scenarios:

_isSelected
_isAncestorSelected
_isDescendantSelected
_isSelectedBranch
_isBreadcrumb
_hasParent
_hasChild
_isFirstChild
_isLastChild
_hasSiblings
_isSiblingSelected
_isParentSelected
_isChildSelected

In your case you can check for:
if ($listRecord['_isLastChild']) { echo "Is last element of branch"; }
or:
if (!$listRecord['_isLastChild']) { echo "Is NOT last element of branch"; }

Don't be confused by the fact that the name contains "child", it is set to "1" even if it is not a child element, like depth0 elements of the category section.

You can use this code for outputting your category fields for debugging. Rename the array if needed ($listRecord):
<pre style="white-space: pre-wrap; font-family: Monaco, 'Courier New', Courier, monospace"><?php print_r($listRecord) ?></pre>

Let me know if this was unclear and I'll dig up some example code.


-aev-