tags:

views:

47

answers:

2

HI all,

This code works perfectly printing all 5 results with borders on the bottom of each list item through CSS.

However the last item id like there to be no border. How could i add a class to the list item on the last iteration?

<?php list($parent) = split('/', $this->url); ?>
<?php  $last_articles = $this->find('/news')->children(array('limit'=>5, 'order'=>'page.created_on DESC')); ?>
<ul id="latest-news">
<?php foreach ($last_articles as $article): ?>
<li>
<?php echo '<h3>'.$article->link($article->title()).'</h3>'; ?>
<?php echo strip_tags(substr($article->content(),0,100)).'...'; ?>
</li>
<?php endforeach; ?>
</ul>

Thanks for your help.

+1  A: 
<?php list($parent) = split('/', $this->url); ?>
<?php  $last_articles = $this->find('/news')->children(array('limit'=>5, 'order'=>'page.created_on DESC')); ?>
<ul id="latest-news">
<?php $count = count($last_articles); $num = 0; ?>
<?php foreach ($last_articles as $article): ?>
<li <?php if($num == $count-1){ ?> class="last-item" <?php } ?>>
<?php echo '<h3>'.$article->link($article->title()).'</h3>'; ?>
<?php echo strip_tags(substr($article->content(),0,100)).'...'; ?>
</li>
<?php $num++ ?>
<?php endforeach; ?>
</ul>

BTW, this adds the class "last-item" to the last item processed.

And yes, you should restructure your code so its readable.

Chacha102
Perfect thank you!!!
Andy
A: 

You can use the :last-child CSS modifier:

ul.latest-news:last-child { border-bottom: none; }

But it's not so widely supported (yet), so adding an explicit .last class as you're about to do is probably best.

Wim
`:last-child` you mean?
porneL
Nice idea thank you.
Andy
Just did some research and its only IE6 that doesnt support it so this is a really nice solution. Thanks
Andy
@porneL you're right, thanks.
Wim
WEll the website i looked at said it was supported but testing it in IE and it doesnt. Suprise suprise.
Andy