I'm developing a Calendar of Events for a friend's site, and came across a roadblock.<br /><br />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:<br /><br />Non-Color<br />Color<br />Non-Color<br />Color<br /><br /><br />Here is how I have coded so far:<br /><!--c1--><div class='codetop'>CODE</div><div class='codemain'><!--ec1--># $Events is where the Database was loaded into<br /># $i is the row number<br /># $row is the row's data<br /># $RETURN is the code that will be returned out of the function<br /># Please Note: I have not debugged this yet, so there are probably errors<br /><br />Â Â foreach($Events as $i => $row){<br />Â Â $RETURN.= "<TR>\n";<br />Â Â $RETURN.= " <TD>\n";<br />Â Â $RETURN.= "Â Â <strong>".$row[event]."</strong><br>\n";<br />Â Â if (date("F", $row['start']) != date("F", $row['end'])Â Â // Different Months<br />Â Â Â Â $RETURN.= "Â Â <em><font size=1>".date("l j, Y", $row['start'])." - ".date("l j, Y", $row['end'])." <br> ".$row[location]."</font></em>\n";<br />Â Â }Â Â Â Â Â Â <br />Â Â elseif (date("d", $row['start']) != date("d", $row['end'])Â Â // Different Days<br />Â Â Â Â $RETURN.= "Â Â <em><font size=1>".date("l j".date("-j", $row['end']).", Y", $row[start])." <br> ".$row[location]."</font></em>\n";<br />Â Â }Â Â Â Â <br />Â Â else<br />Â Â Â Â $RETURN.= "Â Â <em><font size=1>".date("l j, Y", $row[start])." <br> ".$row[location]."</font></em>\n";<br />Â Â }<br />Â Â }<!--c2--></div><!--ec2--><br /><br /><br />Is there any easy way to determine if $i is even or odd?<!--content-->
You can set a variable just before your loop such as<br /><br /><!--c1--><div class='codetop'>CODE</div><div class='codemain'><!--ec1-->$odd = '-odd';<!--c2--></div><!--ec2--><br /><br />Then within your loop you can add<br /><br /><!--c1--><div class='codetop'>CODE</div><div class='codemain'><!--ec1-->if("-odd" == $odd) $odd = "";<br />Â Â Â Â Â Â Â Â Â Â Â Â else $odd="-odd";<!--c2--></div><!--ec2--><br /><br />If $i is odd $odd will have a value of "-odd" if even it will be empty.<br /><br />Hope that helps.<!--content-->
In general, you can determine whether a variable is even or odd using the modulus operator:<br /><br />$i % 2<br /><br />will have a value of 0 if $i is even, and 1 if $i is odd.<!--content-->
How about using the modulus operator % <br /><br />// if i% divided by 2 has no remainder, then i$ is even<br />if ( i$ % 2 ) <br />{ $background_color = white }<br />else <br />{ $background_color = gray }<br /><br /><br />Guess I am slow today <img src="http://www.totalchoicehosting.com/forums/style_emoticons/default/smile.gif" style="vertical-align:middle" emoid="" border="0" alt="smile.gif" /><!--content-->
There you go, multiple solutions for you. <img src="http://www.totalchoicehosting.com/forums/style_emoticons/default/wink.gif" style="vertical-align:middle" emoid="" border="0" alt="wink.gif" /><!--content-->
WOW!<br /><br />Thanks for all the responses.<br /><br />I now have the function up-and-running.<br /><br /><br />(By the way, did I mention that: <img src="http://www.totalchoicehosting.com/forums/style_emoticons/default/tchrocks!.gif" style="vertical-align:middle" emoid=":tchrocks!:" border="0" alt="tchrocks!.gif" />)<br /><br /><br />Thanks again<!--content-->
You can set a variable just before your loop such as<br /><br /><!--c1--><div class='codetop'>CODE</div><div class='codemain'><!--ec1-->$odd = '-odd';<!--c2--></div><!--ec2--><br /><br />Then within your loop you can add<br /><br /><!--c1--><div class='codetop'>CODE</div><div class='codemain'><!--ec1-->if("-odd" == $odd) $odd = "";<br />Â Â Â Â Â Â Â Â Â Â Â Â else $odd="-odd";<!--c2--></div><!--ec2--><br /><br />If $i is odd $odd will have a value of "-odd" if even it will be empty.<br /><br />Hope that helps.<!--content-->
In general, you can determine whether a variable is even or odd using the modulus operator:<br /><br />$i % 2<br /><br />will have a value of 0 if $i is even, and 1 if $i is odd.<!--content-->
How about using the modulus operator % <br /><br />// if i% divided by 2 has no remainder, then i$ is even<br />if ( i$ % 2 ) <br />{ $background_color = white }<br />else <br />{ $background_color = gray }<br /><br /><br />Guess I am slow today <img src="http://www.totalchoicehosting.com/forums/style_emoticons/default/smile.gif" style="vertical-align:middle" emoid="" border="0" alt="smile.gif" /><!--content-->
There you go, multiple solutions for you. <img src="http://www.totalchoicehosting.com/forums/style_emoticons/default/wink.gif" style="vertical-align:middle" emoid="" border="0" alt="wink.gif" /><!--content-->
WOW!<br /><br />Thanks for all the responses.<br /><br />I now have the function up-and-running.<br /><br /><br />(By the way, did I mention that: <img src="http://www.totalchoicehosting.com/forums/style_emoticons/default/tchrocks!.gif" style="vertical-align:middle" emoid=":tchrocks!:" border="0" alt="tchrocks!.gif" />)<br /><br /><br />Thanks again<!--content-->