0

私は現在、ブログ プラットフォームを備えた Web サイトの作成に取り組んでおり、ホームページ上の他のすべての投稿コンテナーの色を交互に変更したいと考えています。ライトブルー、ダークブルー、ライトブルー、ダークブルー、ライトブルーなど。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 } ?>
4

2 に答える 2

4

モジュラス2を使用するだけです:

$postcolour = $counter % 2 ? 'lightblue' : 'darkblue';
于 2012-09-13T00:55:01.550 に答える
4

CSSを使用するだけです:

.postcontainer {
    background-color: lightblue;
}

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

これがデモです!

于 2012-09-13T00:59:54.493 に答える