[ACCEPTED]-php while loop variable for every third div-while-loop
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 1 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 */
If you want to do this on the client-side 2 it can be done with CSS3 (add JS for older 1 browsers [DOMAssistant + Selectivizr]).
CSS: div.imageBorder:nth-child(3n) { /* style attributes will be applied to every 3rd div */ }
If the intention is to do something every 4 X item use modulo. modulo is the remainder 3 of the division and becomes zero if the 2 division is exact an integer.
if(!($counter%3)) {
// this is 3 6 9 etc.
}
$counter++;
Of course you 1 can do it with any number.
$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 4 performing operations while comparing to 3 true value
you must specify $sql
, $result
because it 2 looks like $row = mysql_fetch_array($results)
is TRUE so it gets stuck on 1 true loading the first row of data forever.
More Related questions
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.