Dman8568 Posted June 27, 2006 Posted June 27, 2006 I'm developing a Calendar of Events for a friend's site, and came across a roadblock. What I'm wanting the script to do is loop thought the Events (loaded from a Database), and for every even record output a background color. So the resulting list will be: Non-Color Color Non-Color Color Here is how I have coded so far: ># $Events is where the Database was loaded into # $i is the row number # $row is the row's data # $RETURN is the code that will be returned out of the function # Please Note: I have not debugged this yet, so there are probably errors foreach($Events as $i => $row){ $RETURN.= "<TR>\n"; $RETURN.= " <TD>\n"; $RETURN.= " <strong>".$row[event]."</strong><br>\n"; if (date("F", $row['start']) != date("F", $row['end']) // Different Months $RETURN.= " <em><font size=1>".date("l j, Y", $row['start'])." - ".date("l j, Y", $row['end'])." <br> ".$row[location]."</font></em>\n"; } elseif (date("d", $row['start']) != date("d", $row['end']) // Different Days $RETURN.= " <em><font size=1>".date("l j".date("-j", $row['end']).", Y", $row[start])." <br> ".$row[location]."</font></em>\n"; } else $RETURN.= " <em><font size=1>".date("l j, Y", $row[start])." <br> ".$row[location]."</font></em>\n"; } } Is there any easy way to determine if $i is even or odd? Quote
TCH-Bruce Posted June 27, 2006 Posted June 27, 2006 You can set a variable just before your loop such as >$odd = '-odd'; Then within your loop you can add >if("-odd" == $odd) $odd = ""; else $odd="-odd"; If $i is odd $odd will have a value of "-odd" if even it will be empty. Hope that helps. Quote
nortk Posted June 27, 2006 Posted June 27, 2006 In general, you can determine whether a variable is even or odd using the modulus operator: $i % 2 will have a value of 0 if $i is even, and 1 if $i is odd. Quote
TCH-Don Posted June 27, 2006 Posted June 27, 2006 (edited) How about using the modulus operator % // if i% divided by 2 has no remainder, then i$ is even if ( i$ % 2 ) { $background_color = white } else { $background_color = gray } Guess I am slow today Edited June 27, 2006 by TCH-Don Quote
Dman8568 Posted June 28, 2006 Author Posted June 28, 2006 WOW! Thanks for all the responses. I now have the function up-and-running. (By the way, did I mention that: ) Thanks again Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.