I'm currently working on creating a website with a blogging platform, and I want every other post container on the home page to alternate colours. Like light blue, then dark blue, light blue, dark blue, light blue, etc. I'm using a while loop to get 5 posts from the mysql database. Here is my code.
<?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';
I'd just use CSS:
.postcontainer {
background-color: lightblue;
}
.postcontainer:nth-of-type(even) {
background-color: darkblue;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.