Reputation: 83
I want to add a class to every last <tr>
of a main category. I don't want to add the classname to every <tr>
. What can I change in my script?
Like this:
<table>
<tr>
<th></th>
</tr>
<tr>
<td></td>
</tr>
<tr class="test">
<td></td>
</tr>
<th></th>
<tr>
<td></td>
</tr>
<tr class="test">
<td></td>
</tr>
</table>
<table class="data forum">
<? foreach ($this->mainCategories as $mainCategory): ?>
<tr>
<th><strong><?= $this->escape($mainCategory->fcName) ?></strong></th>
<th> </th>
<th><strong>Topics</strong></th>
<th><strong>Laatste topic</strong></th>
</tr>
<? foreach ($mainCategory->getSubCategories() as $category): ?>
<tr>
<td><a href="<?= $this->url(array('categoryid' => $this->escape($category->getID()), 'controller' => 'forum', 'action' => 'subcategory'), 'default', true) ?>"><?= $this->escape($category->getName()) ?></a></td>
<td><?= $this->escape($category->fcDescription) ?></td>
<? if ($this->escape($category->numTopics) > 0): ?>
<td><?= $this->escape($category->numTopics) ?></td>
<td><?= date_create($this->escape($category->last_topic))->format('d-m-Y H:i') ?></td>
<? else: ?>
<td> </td>
<td> </td>
<? endif ?>
</tr>
<? endforeach ?>
<tr>
<td class="split"></td>
</tr>
<? endforeach ?>
</table>
Upvotes: 0
Views: 160
Reputation: 6346
Grab the count
of your $this->mainCategories
variable and test for the last one against a counter variable...
$count = count($this->mainCategories;
$current = 0;
<?
foreach ($this->mainCategories as $mainCategory):
$current++;
?>
<tr<?= $current == $count?' class="someClass"':''?>>
Update:
Let's look at a simplified version as a proof of concept:
<?php
$mainCategories = array("Eins", "Zwei", "Drei");
$count = count($mainCategories);
$current = 0;
foreach ($mainCategories as $mainCategory):
$current++;
?>
<tr<?= $current == $count?' class="someClass"':''?>>
<td><?= $mainCategory ?></td>
</tr>
<?php endforeach ?>
This produces the following HTML:
<tr>
<td>Eins</td>
</tr>
<tr>
<td>Zwei</td>
</tr>
<tr class="someClass">
<td>Drei</td>
</tr>
Upvotes: 1