7

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

4 Answers 4

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

 ....
}

The key part here is the ($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 */
1
  • Hey Im pretty new to this, how would I implement it above, I added my code above Nov 27, 2009 at 3:49
2

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

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

2

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.

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

Of course you can do it with any number.

1
  • 1
    this right here is super neat... deserves many votes. Oct 4, 2022 at 8:47
1
$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, $result because it looks like $row = mysql_fetch_array($results) is TRUE so it gets stuck on true loading the first row of data forever.

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.