每三个 div 的 php while 循环变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1806582/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 03:54:18  来源:igfitidea点击:

php while loop variable for every third div

phpmysqlwhile-loop

提问by Cool Guy Yo

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?

是他们在 while 循环中将变量分配给 div 中的类的一种方式,对于 while 循环中的每三个项目。我正在使用蓝图结构,第三个 div 在最后,我需要为每三个 div 添加一个“最后一个”类名,所以 3rd div 6th div 9th 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 */

回答by Jordan S. Jones

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

 ....
}

The key part here is the ($i % 3) == 0.

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

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

回答by tnypxl

If you want to do this on the client-side it can be done with CSS3 (add JS for older browsers [DOMAssistant + Selectivizr]).

如果您想在客户端执行此操作,则可以使用 CSS3 完成(为旧浏览器添加 JS [DOMAssistant + Selectivizr])。

CSS: div.imageBorder:nth-child(3n) { /* style attributes will be applied to every 3rd div */ }

CSS: div.imageBorder:nth-child(3n) { /* style attributes will be applied to every 3rd div */ }

回答by Abdelkader Soudani

$sql = "SELECT * FROM shoeData";
$results = mysql_query($sql);
while($row = mysql_fetch_array($results)) {

    // whatever code here 

}

It doesn't seem that MySQL is smart enough performing operations while comparing to true value you must specify $sql, $resultbecause it looks like $row = mysql_fetch_array($results)is TRUE so it gets stuck on true loading the first row of data forever.

与您必须指定的真值相比,MySQL 似乎不够聪明地执行操作$sql$result因为它看起来像$row = mysql_fetch_array($results)TRUE,所以它永远停留在真正加载第一行数据上。

回答by Henry

If the intention is to do something every X item use modulo. modulo is the remainder of the division and becomes zero if the division is exact an integer.

如果目的是做某事,每个 X 项都使用模数。modulo 是除法的余数,如果除法是整数,则取零。

if(!($counter%3)) {
    // this is 3 6 9 etc.
}
$counter++;

Of course you can do it with any number.

当然,你可以用任何数字来做到这一点。