css - using php to add classes to li then alternate after every third item? -
this may stupidly simple i'm trying figure out way add class unordered list, alternate class after every third item..
i've mananged add class on every third item (which not want), here's code:
<?php $i=1; foreach($this->items $item) : ?> <li class="<?php if ($i % 3 == 0) : ?>odd<?php endif; ?>"><a href="<?php echo $linky; ?>">xxx</a></li> <?php $i++; endforeach; ?>
which spits out:
<li class="">xxx</li> <li class="">xxx</li> <li class="odd">xxx</li> <li class="">xxx</li> <li class="">xxx</li> <li class="odd">xxx</li>
but i'm hoping is:
<li class="odd">xxx</li> <li class="odd">xxx</li> <li class="odd">xxx</li> <li class="even">xxx</li> <li class="even">xxx</li> <li class="even">xxx</li>
and forth.. i'd use jquery this, have use php in case.. appreciated :)
use boolean flag flipped (negated) every time $i % 3 == 0
:
// start 0 instead of 1 $i=0; // flag starts true $state = true; foreach ($this->items $item) { if ($i % 3 === 0) { // flip opposite state $state = !$state; } ?> <li class="<?php if ($state) : ?>odd<?php else: ?>even<?php endif; ?>"><a href="<?php echo $linky; ?>">xxx</a></li> <?php $i++; }
here demonstration. though you'll need inspect output see classes change.
Comments
Post a Comment