Jump to content

Recommended Posts

Posted

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?

Posted

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.

Posted

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.

Posted (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 :angry:

Edited by TCH-Don
Posted

WOW!

 

Thanks for all the responses.

 

I now have the function up-and-running.

 

 

(By the way, did I mention that: :))

 

 

Thanks again

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...