Indeed, modulo. You can test for ($listCount - 1) % 6 < 3
.
Alternate row color for every 3 items? [closed]
-
03-12-2021 - |
Frage
I have a unordered list with 3 items in every row (instead of the default 1). Now I want to alternate the row colors. How would I do that?
So basicially I want to do something like this:
if ($listCount == 1, 2, 3 OR 7, 8, 9 OR 13, 14, 15) {
$alternateRow = "rowOdd";
}
else {
$alternateRow = "rowEven";
}
(I know this is a syntax error, just trying to explain)
I think it's possible with modulus but to be honest I don't really understand how it works.
Lösung
Andere Tipps
How about doing it completely with css? http://css-tricks.com/how-nth-child-works/
Since the original poster was not clear on modulo I figure I would help explain how it works so that you can use it the next time something comes up that needs it. The Modulo operator (%) works by determing the remainder of a division between the two operands.
The rule for modulo is:
if left < right: left % right = left
if left > right: left % right = remainder of left / right
For example:
- 1 % 6 = 1 ( 1 < 6 so the answer is 1 )
- 7 % 6 = 1 ( 7/6 = 1 with remainder of 1 )
- 10 % 6 = 4 ( 10/6 = 1 remainder of 4 )
- 6 % 6 = 0 ( 6/6 = 1 with 0 remainder )
So in your case you would get 123 (for 1,2,3 % 6 respectively) and 4,5,6 % 3 would give you 450. Then 7,8,9 % 6 would yield you 123 again, 10,11,12 would be 450 again, and so on forever. So your code would:
// check that list count is less than three but not = 0
// (only true when mod yields 1,2, or 3)
if($listCount % 6 < 3 && $listCount % 6 == 0){
$alternateRow = "rowOdd";
} else {
$alternateRow = "rowEven";
}
int i = 0;
$Value = "rowEven";
while ()
{
while (i < 3)
{
$alternateRow = $value
i++;
}
i = 0;
if ($value == "rowOdd")
$Value = "rowEven";
else
$Value = "rowOdd"
}
Or use modulo like this:
<?php
$numRecords=9;
for ($i = 0; $i < $numRecords; $i++)
{
$className = "";
if (($i % 3) == 0)
{
$className = "third";
echo "i = " . $i;
}
else
{
$className = "other";
}
}
?>