Jump to content

Calendar Help - Getting Friday to Friday alternating different styles


jdubwelch

Recommended Posts

I'm making a event calendar for a family vacation home.  It alternates between sides of the family who gets a crack at staying there.   Well, I need my calendar to show that.  I'm able to get alternating rows to be displayed differently by:
[code]
if ($currentRow % 2 == 0) {
$row = "evenrow";
} else {
$row = "oddrow";
}
[/code]

i have 2 styles (evenrow & oddrow obviously) that will make the date a different color.

That's relatively easy, but what I'm wanting to do is a little more complex i think. The "weeks" for the cabin are friday-thursday.  And I basically just want the styling that I had for the rows to be for the cabin's weeks.

Any ideas?  I'm pretty lost on this one.

you can view the calendar at http://oc.jwelchdesign.com

here's my entire calendar code:

[code]<link href="css/first.css" rel="stylesheet" type="text/css">

<?php
function readCalendarData ($month, $year) {

// OPEN DATABASE CONNECTION
$dbLink = mysql_connect ($dbHost, $dbUser, $dbPass);
if (!$dbLink) {
die ("Database: Couldn't connect to the MySQL Server");
}
mysql_select_db ($dbName, $dbLink) or die ("Database: Couldn't open Database");

// CREATE TIMESTAMPS TO SEARCH WITH
$firstDayTimestamp = mktime (0,0,0, $month, 1, $year);
$daysInMonth = date ("t", $firstDayTimestamp);
$lastDayTimestamp = mktime (23, 59, 59, $month, $daysInMonth, $year);

// CREATE SQL
$sql = "SELECT id, UNIX_TIMESTAMP(dateField) AS timestampField ";
$sql .= "FROM ocCalendar ";
$sql .= "WHERE UNIX_TIMESTAMP(dateField) >= " . $firstDayTimestamp . " AND UNIX_TIMESTAMP(dateField) <= " . $lastDayTimestamp . " ";
$sql .= "ORDER BY timestampField ASC";

// READ IN DATA
$dbResult = mysql_query($sql, $dbLink) or die ("MYSQL Error: " . mysql_error() );
$numRecords = mysql_num_rows ($dbResult);
$eventsArray[] = "";
for ($i=0; $i < $numRecords; $i++) {
$row = mysql_fetch_assoc ($dbResult);
$day = date ("j", $row['timestampField']);

// CHECK DATE ISN'T ALREADY IN $eventsArray
if(!in_array($day, $eventsArray)) {
$eventsArray[] = $day;
}
}

// CLOSE DB CONNECTION
mysql_close($dbLink);

// RETURN eventsArray TO CODE THAT CALLED FUNCTION
return $eventsArray;

}

if ($submit) {

// Subtract one from month for prev and add 1 for next
if ($submit == "submit") {

$month_now = $_POST['month'];
$year_now = $_POST['year'];

} elseif ($submit == "Prev") {
$month_now--;
} else {
$month_now++;
}

$date = getdate(mktime(0,0,0,$month_now, 1, $year_now));

} else {

$date = getdate();

}

$month = $date["mon"];
$monthName = $date["month"];
$year = $date["year"];

$wday = $date["wday"];

$eventsArray = readCalendarData($month, $year);

$firstDay = mktime (0,1,0, $month, 1, $year);
$firstDay = date ("w", $firstDay);
$daysInMonth = date ("t", $firstDay);


echo "
<div id=\"beachcal\">
<form id=\"calendar\" name=\"calendar\" method=\"post\" action=\"index.php\">
<table width=\"600\" border=\"0\" cellpadding=\"3\" cellspacing=\"0\">
<tr><input type=\"hidden\" name=\"month_now\" value=\"$month\" /><input type=\"hidden\" name=\"year_now\" value=\"$year\" />
    <td colspan=\"7\" class=\"headRow\"><input type=\"submit\" name=\"submit\" value=\"Prev\" /><span class=\"theMonth\">$monthName $year</span><input type=\"submit\" name=\"submit\" value=\"Next\" /></td>
</tr>
<tr class=\"headerDays\">
<td>sun</td>
<td>mon</td>
<td>tue</td>
<td>wed</td>
<td>thur</td>
<td>fri</td>
<td>sat</td>
</tr>\n";

#CALCULATE NUMBER OF ROWS
$totalCells = $firstDay + $daysInMonth;
if ($totalCells < 36) {
$rowNumber = 5;
} else {
$rowNumber = 6;
}
$dayNumber = 1;

for ($currentRow=1; $currentRow <= $rowNumber; $currentRow++) {
if ($currentRow % 2 == 0) {
$row = "evenrow";
} else {
$row = "oddrow";
}

if ($currentRow == 1) {

#CREATE FIRST ROW
echo "<tr class=\"$row\">\n";
for ($currentCell = 0; $currentCell < 7; $currentCell++) {

// CHECK IF IT'S THE FIRST DAY OF THE MONTH
if ($currentCell == $firstDay) {
$day = $dayNumber . "/" . $month . "/" . $year;
$link = "<a href=\"details.php?day=$day\">$dayNumber</a>";
$alink = "<a href=\"add_event.php?day=$day\">$dayNumber</a>";

if (in_array($dayNumber, $eventsArray)) {
echo "<td class=\"reserved\"><div id=\"day\">$link</div><div id=\"event\"></div></td>\n";
} else {
echo "<td><div id=\"day\">$alink</div><div id=\"event\"></div></td>\n";
}
$dayNumber++;
} else {

// IF THE FIRST DAY IS PASSED OUTPUT THE DATE
if ($dayNumber > 1) {
$day = $dayNumber . "/" . $month . "/" . $year;
$link = "<a href=\"details.php?day=$day\">$dayNumber</a>";
$alink = "<a href=\"add_event.php?day=$day\">$dayNumber</a>";

if (in_array($dayNumber, $eventsArray)) {
echo "<td class=\"reserved\"><div id=\"day\">$link</div><div id=\"event\"></div></td>\n";
} else {
echo "<td><div id=\"day\">$alink</div><div id=\"event\"></div></td>\n";
}
$dayNumber++;
} else { // FIRST DAY NOT REACHED SO DISPLAY A BLANK CELL
echo "<td class=\"otherMonth\">&nbsp;</td>\n";
}
}
}
echo "</tr>\n";
} else {

#CREATE THE REMAINING ROWS
echo "<tr class=\"$row\">\n";
for ($currentCell=0; $currentCell < 7; $currentCell++) {
$day = $dayNumber . "/" . $month . "/" . $year;
$link = "<a href=\"details.php?day=$day\">$dayNumber</a>";
$alink = "<a href=\"add_event.php?day=$day\">$dayNumber</a>";

// IF THE DAYS IN THE MONTH ARE EXCEEDED DISPLAY A BLANK CELL
if ($dayNumber > $daysInMonth) {
echo "<td class=\"otherMonth\">&nbsp;</td>\n";
} else {
if (in_array($dayNumber, $eventsArray)) {
echo "<td class=\"reserved\"><div id=\"day\">$link</div><div id=\"event\"></div></td>\n";
} else {
echo "<td><div id=\"day\">$alink</div><div id=\"event\"></div></td>\n";
}
$dayNumber++;
}
}
echo "</tr>\n";
}

}
echo "</table>";
$month_array = array("January" => 1, "February" => 2, "March" => 3, "April" => 4, "May" => 5, "June" => 6, "July" => 7, "August" => 8, "September" => 9, "October" => 10, "November" => 11, "December" => 12);

echo "<select name=\"month\">";

foreach ($month_array as $m => $key) {
if ($monthName == $m) {
echo "<option value=\"$key\" selected=\"selected\">$m</option>\n";
} else {
echo "<option value=\"$key\">$m</option>\n";
}

}
echo "</select>";

echo "<select name=\"year\">";
for ($i=$year; $i<=$year+2; $i++) {
echo "<option value=\"$i\">$i</option>\n";
}
echo "</select>
<input type=\"submit\" name=\"submit\" value=\"submit\" />
</form>
</div>";
?>

[/code]
Link to comment
Share on other sites

Well, if I am understanding this correctly you want the alternating row colors to run from Friday to Thursday instead of Sunday to Saturday.  If that is the case then you simply need to test $currentRow plus 1 if the day is Friday or saturday. Then Friday and Saturday are always treated as teh next week.

Something like this:
[code]
if ($day=="Friday" || $day=="Saturday") {
  $weekRow = $currentRow + 1;
} else {
  $weekRow = $currentRow;
}

if ($weekRow % 2 == 0) {
$row = "evenrow";
} else {
$row = "oddrow";
}
[/code]
Link to comment
Share on other sites

Awesome!  That worked... for the individual month.  But it doesn't carry over to the next month.

you can go to my calendar and see
http://oc.jwelchdesign.com

but between Dec 2006 & Jan 2007, the last week of December is green.  The first week of Jan. '07 is part of that last week, but it's blue.

Is there a way to fix that?

Thanks for the help so far though, that was way easier than I thought.
Link to comment
Share on other sites

yes, I realized that when I posted, but I wanted to keep it simple. Here is another method. Assuming the following variables are present:
$day - day of the month
$month - numeric representation of month (1 = jan)
$year - 4 digit year

[code]
//Gets the current week of the year based upon the $day, $month, $year
//Add 3 to the day to adjust for a Friday start of week
$week  = date("W", mktime(0, 0, 0, $month, $day+3, $year) );
//Set value of row based upon wether $week has a remainder when
//divided by 2
$row = ($week % 2)? "evenrow" : "oddrow";
[/code]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

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

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  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...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.