php while循环变量,每三个div [英] php while loop variable for every third div

查看:57
本文介绍了php while循环变量,每三个div的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Is their a way in a while loop to assign a variable to a class in a div, for every third item in a while loop. I am using the blueprint structure and the third div is at the end and i need to attacht a "last" class name to every third div so 3rd div 6th div 9th div and so on?

/* 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 */

推荐答案

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

 ....
}

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

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

The following is in response to your comment.

/* 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 */

这篇关于php while循环变量,每三个div的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆