Reputation: 581
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 } ?>
Upvotes: 0
Views: 2191
Reputation: 224913
I'd just use CSS:
.postcontainer {
background-color: lightblue;
}
.postcontainer:nth-of-type(even) {
background-color: darkblue;
}
Upvotes: 4
Reputation: 219938
Just use modulus two:
$postcolour = $counter % 2 ? 'lightblue' : 'darkblue';
Upvotes: 4