在php循环里面隔行换色

2025年03月23日 23:32
有5个网友回答
网友(1):

这个问题有很多答案,看了下面好多写的不规范,我晒出这个看看





$sql="select * from a order by id desc limit 0,6";
require 'conn.php';
$i = '1';
while($row=mysql_fetch_array($result))
{
$color_z = '';
$zhiz = $i%2;
($zhiz == '1') ? $color_z ="#00FF99": $color_z = "#cccccc";
?>
'>



$i++;
}
?>
标题时间

晕死我觉得php代码最好写规范点哦

网友(2):






$sql="select * from a order by id desc limit 0,6";
require 'conn.php';
while($row=mysql_fetch_array($result))
{
$n++;
$j = $n%2;
?>
>



}
?>
标题时间


这个其实很简单
就是个计数器
判断一下行数就行

网友(3):

while($row=mysql_fetch_array($result))
{
$red = '#FF0000';
$blue = '#0000FF';
$bgcolor = ($bgcolor == $blue) ? $red : $blue;
?>




}

网友(4):

$sql="select * from a order by id desc limit 0,6";
require 'conn.php';
$c = "color=\"#red\"";
while($row=mysql_fetch_array($result))
{
if ($c == "color=\"#red\"") {
$c = "color=\"#blue\"";
} else $c = "color=\"#red\"";
?>
>



}
?>

网友(5):

$sql = "SELECT * FROM a ORDER BY id DESC LIMIT 0,6";
require 'conn.php';
?>





while($row = mysql_fetch_array($result)){
$color = 'blue';
$color = $color == 'blue' ? 'red' : 'blue';
?>




}
?>

标题时间