如何在PHP中每隔一个div替换背景颜色



我目前正在创建一个带有博客平台的网站,我希望主页上的其他帖子容器都能替换颜色。比如浅蓝色,然后是深蓝色,浅蓝色,深蓝色,淡蓝色等等。我使用while循环从mysql数据库中获得5个帖子。这是我的密码。

<?php
$sql = mysql_query("SELECT * FROM posts ORDER BY id DESC LIMIT 6");
$array = mysql_fetch_array($sql);
while ($array = mysql_fetch_array($sql)) {
//The php below this is the problem
$counter = 0;
$counter++;
$postcolour =  WHAT DO I PUT HERE ? 'lightblue' : 'darkblue';

?>

<div class="postcontainer" style="background-color: <?php echo $postcolour; ?>;">
</div> <?php } ?>

只需使用模数二:

$postcolour = $counter % 2 ? 'lightblue' : 'darkblue';

我只想使用CSS:

.postcontainer {
    background-color: lightblue;
}
.postcontainer:nth-of-type(even) {
    background-color: darkblue;
}

这是一个演示!

最新更新