vote up 1 vote down star

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

67% accept rate

1 Answer

vote up 7 vote down check
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 */
link|flag
Hey Im pretty new to this, how would I implement it above, I added my code above – Anders Kitson Nov 27 at 3:49
Awesome Totally Worked – Anders Kitson Nov 27 at 4:16

Your Answer

Get an OpenID
or
never shown

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