对于每个输出(sql)一个不同的类


for each output (sql) a different class

if ($result = $mysqli->query($query)) {
    while ($list = $result->fetch_object()) {
        $titel = $list->titel;
        echo "<li class='"wow fadeInLeft'">$titel </li> ";
    }    
    $result->close();
}

在一个表中有几行。有些行需要fadeInleft类,第二行需要fadeInRight类,第三行需要fadeInleft,第四行需要fadeInRight等。

最好的方法是什么?

也许有更好的办法,但是

$check=True;
while ($list = $result->fetch_object()) {
     $titel = $list->titel;
     $cls=$check ? "fadeInRight" : "fadeInLeft"
     echo "<li class='"wow $cls'">$titel </li> ";
     $check=!$check
}    
$result->close();

奇数/偶数需要不同的类。试试这个-

$i = 1;
while ($list = $result->fetch_object()) {
    if($i %2 == 0) {
        $class = 'fadeInRight';
    } else {
        $class = 'fadeInLeft';
    }
    //Use $class in li
    echo "<li class='wow $class'>$titel</li>";
    $i++;
}

可以使用模。这样的:

$i = 1;
if ($result = $mysqli->query($query)) {
    while ($list = $result->fetch_object()) {
        $titel = $list->titel;
        $class = ($i % 2 == 0) ? 'fadeInLeft' : 'fadeInRight';
        echo "<li class='wow $class'>$titel</li>";
    }
    $result->close();   
}

或者按位使用:

$i = 1;
if ($result = $mysqli->query($query)) {
    while ($list = $result->fetch_object()) {
        $titel = $list->titel;
        $class = ($i & 1) ? 'fadeInLeft' /*odd*/ : 'fadeInRight' /*even*/;
        echo "<li class='wow $class'>$titel</li>";
    }
    $result->close();   
}

@paubo147解决方案的稍微简短的版本,它消除了对额外变量的需要,并在检查值的同时进行赋值。

$check=True;
while ($list = $result->fetch_object()) 
{
     $titel = $list->titel;
     echo "<li class='"wow ".(($check=!$check) ? "fadeInRight" : "fadeInLeft")."'">$titel </li> ";
}    
$result->close();