繁体   English   中英

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

[英]How to alternate background colour every other div in PHP

我目前正在使用博客平台创建网站,我希望主页上的所有其他帖子容器都可以使用其他颜色。 像浅蓝色,然后是深蓝色,浅蓝色,深蓝色,浅蓝色等。我正在使用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;
}

这是一个演示!

暂无
暂无

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

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