search

Home  >  Q&A  >  body text

javascript - add custom data attribute when php loops for second time

<?php foreach($header as $rowItem): ?>
<tr>
    <?php foreach($rowItem as $headerItem): ?>
        <th data-tablesaw-priority="persist" id="<?= $headerItem['id'] ?>" class="<?= $headerItem['class'] ?>"
        <?= (isset($headerItem['colspan']))?'colspan="'. $headerItem['colspan'] .'"':'' ?>
        <?= (isset($headerItem['rowspan']))?'rowspan="'. $headerItem['rowspan'] .'"':'' ?>
        <?= (isset($headerItem['style']))?'style="'. $headerItem['style'] .'"':'' ?> >
            <?= $headerItem['text'] ?>
        </th>
    <?php endforeach ?>
</tr>

<?php endforeach ?>

This loop will have three <tr> tags. Now you need to add custom attributes to the <th> in the second <tr> tag. How does this need to be written?

滿天的星座滿天的星座2734 days ago715

reply all(2)I'll reply

  • typecho

    typecho2017-06-29 10:10:29

    The simplest, if judgment loops out the key

    reply
    0
  • 学习ing

    学习ing2017-06-29 10:10:29

    Method 1, use foreach, add a count variable, record the number of loops, if $count == 1, do what you need to do next

    Method 2, use for, do what you need to do when $i==1

    for($i=0;$i<count($header);$i++)
    {
        if($i === 1) {
            //
        }else{
            //
        }
    }

    reply
    0
  • Cancelreply