我想一個類添加到項目中在我的循環,每次是第二項有它上面的環形項目,所以像這樣:PHP循環 - 檢查每一次循環中
loopitem1
loopitem2 <- add class to this one
loopitem3
loopitem4 <- add class to this one
loopitem5
loopitem6 <- add class to this one
我怎樣才能做到這一點?
我的循環如下所示:
while ($timeline = $fetchtimeline->fetch(PDO::FETCH_ASSOC)) {
}
提前感謝!
編輯;我現在所擁有的;
<?php
$counter = 0;
while ($timeline = $fetchtimeline->fetch(PDO::FETCH_ASSOC)) {
$inverted = "";
if($counter == 2) {
$inverted = 'class="timeline-inverted"';
$counter = 0;
} else {
$counter++;
}
?>
<li <?php echo $inverted; ?>>
<div class="timeline-badge primary"><i class="fa fa-hashtag"></i></div>
<div class="timeline-panel">
<div class="timeline-heading">
<h4 class="timeline-title"><?php echo $timeline['title']; ?></h4>
<p><small class="text-muted"><?php echo $engine->readable($timeline['timestamp']); ?> by <?php echo $engine->pullName($timeline['postedby']); ?></small></p>
</div>
<div class="timeline-body">
<p><?php echo $timeline['content']; ?></p>
</div>
</div>
</li>
<?php
}
?>
你的意思是每隔一行或你可以更具體? '模數運算符'浮現在腦海 – chris85
modulo http://php.net/manual/en/language.operators.arithmetic.php –
@ chris85啊,你編輯過。 *頑皮淘氣*大聲笑 –