Forum Moderators: coopster

Message Too Old, No Replies

how? change bgcolor every N rows

this is an easy one

         

httpwebwitch

7:58 pm on Jan 26, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



this is a math question.

How do I change a value every N times in a loop? I know there's probably a simple formula using modulus, but I can't get my head around it today.

for instance, what condition do I put in the "if" in the situation below?


count=0;
while (whatever){
if ( what do i put here? ){
$bgcolor='#ffffff';
}else{
$bgcolor='#ffcccc';
}
print("<TR><TD bgcolor='".$bgcolor."'> lorem ipsum </TD></TR>");
$count++;
}

I want the result to look like: N rows of one color, then N rows of a second color, then N rows of the first color again, etc.

Timotheos

8:31 pm on Jan 26, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



$count & 1

lorax

8:33 pm on Jan 26, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Well given that modulus returns true if a remainder (any remainder) exists then:

0 % 2 // 0 // false
1 % 2 // 1 // true
2 % 2 // 0 // 2 goes into 2 once, none remain // false
3 % 2 // 1 // 2 goes into 3 once, then 1 remains // true
4 % 2 // 0 // 2 goes into 4 twice, none remain // false

coopster

9:35 pm on Jan 26, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Tip (code snippet included): [webmasterworld.com...]

I'll be willing to bet that Timotheos' bitwise operator is faster though...