Php while loop variable for every third div

Is their way in a while loop assigning a variable to a class in a div for every third element in a while loop. I use a drawing structure and the third div is at the end, and I need to bind the "last" class name to every third div, so what is the third div of the 6th div of the 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 */ 
+6
php mysql while-loop
source share
4 answers
 for ($i = 0; $i < $numRecords; $i++) { $className = ""; if (($i % 3) == 0) { $className = "last" } .... } 

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

EDIT: In response to your comment, the following will be indicated.

 /* 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 */ 
+27
source share

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

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

+2
source share
 $sql = "SELECT * FROM shoeData"; $results = mysql_query($sql); while($row = mysql_fetch_array($results)) { // whatever code here } 

It doesn't seem like MySQL is smart enough, it does the operations, comparing with the true value you should specify $sql , $result , because it looks like $row = mysql_fetch_array($results) TRUE, so it stuck on the true loading of the first row of data forever.

+1
source share

If the intention is to do something, each element of X uses modulo. modulo is the remainder of division and vanishes if division is an exact integer.

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

Of course, you can do this with any number.

0
source share

All Articles