Php 为2行的每2个结果添加一个样式,然后跳过2个结果?

Php 为2行的每2个结果添加一个样式,然后跳过2个结果?,php,mysql,mysqli,Php,Mysql,Mysqli,这是一个有点棘手的字,但 我想做的是添加样式“背景色:黑色;”在我的查询中只显示一些结果 所以,如果我要回应我的结果,它会显示如下 1st result no style 2nd result black 3rd result black 4th result no style 5th result no style 7th result black 8th result black 9th result no style 10th result no style 我的问题 $getusers

这是一个有点棘手的字,但

我想做的是添加样式“背景色:黑色;”在我的查询中只显示一些结果

所以,如果我要回应我的结果,它会显示如下

1st result no style
2nd result black
3rd result black
4th result no style
5th result no style
7th result black
8th result black
9th result no style
10th result no style
我的问题

$getusers= $db->query("SELECT * FROM users");
    while($users= $getusers->fetch_assoc()) {

    $color = ??????????????? not sure

    echo '<div style="background-color: ' . $color . ';">';
    echo $users['username'];
    echo '</div>';
}

使用占位符变量

在下面的示例中,按照$i的进度进行操作

$getusers= $db->query("SELECT * FROM users");

// Starting at 2 will cause "no style" to happen only once on first pass.
$i = 2; 

while($users= $getusers->fetch_assoc()) {

    // if $i is 3 or 4 value would be "black"
    $rowBg = "background-color:black";

    // Set to an empty string (no style) if $i is 1 or 2.
    if($i <= 2) {
        $rowBg = "";
    }

    $i++;

    // Start over!
    if($i > 4)
        $i = 1;

    echo '<div style="' . $rowBg . ';">';
    echo $users['username'];
    echo '</div>';
}
这个怎么样:

$getusers= $db->query("SELECT * FROM users");
$r = 1;
while($users= $getusers->fetch_assoc()) {
    $style = ($r == 2 || $r == 3) ? 'style="background-color: black"' : ''; // black for every 2nd & 3rd row
    $r = ($r == 4) ? 1 : $r+1; // reset count to 1 when we get to 4

    echo '<div $style>';
    echo $users['username'];
    echo '</div>';
}

}

但是是否可以忽略第一个结果,只给出第一个无样式,然后开始双模式的事情?否则,我的模式就不能正常工作:@Jayden然后将$i从2开始,这样它就跳过了第一个。更新帖子:@Jayden我将代码从$color更改为$rowBg,因为它看起来更像你想要的。
$count = 1;
$getusers= $db->query("SELECT * FROM users");
while($users= $getusers->fetch_assoc()) {

$color = "#000000";
if($count == 2 || $count == 3){
echo '<div style="background-color: ' . $color . ';">';

if($count==3){
    $count=0;
}
}
else{
    echo "<div>";
}   
echo $users['username'];

echo '</div>';

$count++;