简体   繁体   English

如何向最后一个表格行添加类

[英]How to add a class to last table row

I want to add a class to every last <tr> of a main category. 我想向主要类别的每个最后一个<tr>添加一个类。 I don't want to add the classname to every <tr> . 我不想将类名添加到每个<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>&nbsp;</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>&nbsp;</td>
                        <td>&nbsp;</td>
                    <? endif ?>
                </tr>
             <? endforeach ?>
            <tr>
                <td class="split"></td>
            </tr>

         <? endforeach ?>

 </table>

Grab the count of your $this->mainCategories variable and test for the last one against a counter variable... 获取$this->mainCategories变量的count ,并针对计数器变量测试最后一个变量...

$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: 这将产生以下HTML:

<tr>
    <td>Eins</td>
</tr>
<tr>
    <td>Zwei</td>
</tr>
<tr class="someClass">
   <td>Drei</td>
</tr>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM