0

次のようなコードがあるとします。

while ($info = mysql_fetch_assoc($data_p)) {
    $name = stripslashes($info['name']);
    $desc = stripslashes($info['description']);
    $desc = substr($desc, 0, 150);
    $price = stripslashes($info['price']);
    Print "<div style=\"width:600px; height:150px; border:1px solid black; overflow:hidden\"><div style=\"height:148px; width:25%; border:1px solid red; float:left\"><center><img src=\"".$picture."\" height=\"120\" width=\"120\" style=\"margin-top:15px\" /></center></div><div style=\"height:150px; width:50%; border:1px solid blue; float:left; text-overflow: ellipsis; padding-top:5px\"><center><font size=\"+1\"><b><a href=\"result.php?product=".urlencode($name)."\">".$name."</b></a></font><br><br>".$desc."...</center></div><div style=\"height:150px; width:24%; border:1px solid green; float:left\"><center><h1>$".$price."</h1><button>Add to Cart</button></center></div></div>";

while ループで最初に定義されたメインの DIV では、2 つのグレーの色合いを交互に使用したいと考えています。結果では、明るい暗い明るい暗いなどのように見えます...別の色でエコーをもう一度繰り返してみましたが、各結果が重複しています。これを行う方法はありますか?

4

3 に答える 3

1
$c = 1;

while ($info = mysql_fetch_assoc($data_p)) {
    $name = stripslashes($info['name']);
    $desc = stripslashes($info['description']);
    $desc = substr($desc, 0, 150);
    $price = stripslashes($info['price']);
    Print "<div style=\"background:" . ($c ? '#ccc' : '#aaa') . "width:600px; height:150px; border:1px solid black; overflow:hidden\"><div style=\"height:148px; width:25%; border:1px solid red; float:left\"><center><img src=\"".$picture."\" height=\"120\" width=\"120\" style=\"margin-top:15px\" /></center></div><div style=\"height:150px; width:50%; border:1px solid blue; float:left; text-overflow: ellipsis; padding-top:5px\"><center><font size=\"+1\"><b><a href=\"result.php?product=".urlencode($name)."\">".$name."</b></a></font><br><br>".$desc."...</center></div><div style=\"height:150px; width:24%; border:1px solid green; float:left\"><center><h1>$".$price."</h1><button>Add to Cart</button></center></div></div>";

    $c = 1 - $c;
于 2013-08-31T21:04:53.387 に答える
1

デザインとロジックを分離したい場合は、css を使用することもできます。

div:nth-child(odd) {
   background:#c0c0c0;
}
div:nth-child(even) {
   background:#a0a0a0;
}
于 2013-08-31T21:09:40.507 に答える
1

DIV の数を数え、奇数と偶数で色を変えることができます。

$num = 0;
while ($info = mysql_fetch_assoc($data_p)) {
 $num++;
 $color = ($num % 2) ? '#ccc;' : '#aaa';
//***insert your relevant variables
}
于 2013-08-31T21:20:20.413 に答える