一尘不染

php while循环变量,每三个div

mysql

是while循环中为while循环中的每三个项目将变量分配给div中的类的一种方式。我使用的是蓝图结构,最后是第三个div,我需要在每个第三个div上附加一个“
last”类名,以便第3 div,第6 div,第9 div等?

/* LOOP THROUGH SHOEDATA TABLE */

$results = mysql_query("SELECT * FROM shoeData");


while($row = mysql_fetch_array($results)){

$name = $row['name'];
$about = $row['about'];
$company = $row['company'];
$buy = $row['buy'];
$tags = $row['tags'];
$id = $row['id'];
$image = $row['image'];


/* ECHO THE SHOEDATA RESULTS */

    echo "<div class='imageBorder span-8 column'>";
        echo "<div id='imageHeight'>";
        echo "<img  src='thumbs/$image'>";
        echo "</div>";

        echo "<ul>";

            echo "<li>$name</l1>";
            echo "<li>$about</l1>";
            echo "<li>$company</l1>";
            echo "<li><a href='$buy'>BUY</a></l1>";
            echo "<li>$tags</l1>";
        echo "</ul>";
    echo "</div>";


}/*SHOEDATA WHILE LOOP ENDS */

阅读 617

收藏
2020-05-17

共1个答案

一尘不染

for ($i = 0; $i < $numRecords; $i++)
{
 $className = "";
 if (($i % 3) == 0)
 {
  $className = "last"
 }

 ....
}

这里的关键部分是($i % 3) == 0

编辑: 以下是对您的评论的回应。

/* LOOP THROUGH SHOEDATA TABLE */

$results = mysql_query("SELECT * FROM shoeData");

$i = 0;
while($row = mysql_fetch_array($results)){
$i++;
$name = $row['name'];
$about = $row['about'];
$company = $row['company'];
$buy = $row['buy'];
$tags = $row['tags'];
$id = $row['id'];
$image = $row['image'];


/* ECHO THE SHOEDATA RESULTS */         
    $additionalClass = ($i % 3) == 0 ? " last" : "";
    echo "<div class='imageBorder span-8 column" . $additionalClass . "'>";
        echo "<div id='imageHeight'>";
        echo "<img  src='thumbs/$image'>";
        echo "</div>";

        echo "<ul>";

                echo "<li>$name</l1>";
                echo "<li>$about</l1>";
                echo "<li>$company</l1>";
                echo "<li><a href='$buy'>BUY</a></l1>";
                echo "<li>$tags</l1>";
        echo "</ul>";
    echo "</div>";


}/*SHOEDATA WHILE LOOP ENDS */
2020-05-17