Forum Moderators: coopster

Message Too Old, No Replies

Plan of Attack

Ideas on how to make a calendar where BG is changeable?

         

inveni0

5:51 pm on Jan 7, 2006 (gmt 0)

10+ Year Member



I'm trying to create a calendar that uses mySQL to set the background color for each date. For instance, I could make the background of weekdays one color and the background of weekends another. I'd use MySQL to change the color of a specific day or group of days if need be.

At present, I'm using:
<?
function calendar($date)
{
//If no parameter is passed use the current date.
if($date == null)
$date = getDate();

$day = $date["mday"];
$month = $date["mon"];
$month_name = $date["month"];
$year = $date["year"];

$this_month = getDate(mktime(0, 0, 0, $month, 1, $year));
$next_month = getDate(mktime(0, 0, 0, $month + 1, 1, $year));

//Find out when this month starts and ends.
$first_week_day = $this_month["wday"];
$days_in_this_month = round(($next_month[0] - $this_month[0]) / (60 * 60 * 24));

$calendar_html = "<table style=\"background-color:666699; color:ffffff;\">";

$calendar_html .= "<tr><td colspan=\"7\" align=\"center\" style=\"background-color:9999cc; color:000000;\">" .
$month_name . " " . $year . "</td></tr>";

$calendar_html .= "<tr>";

//Fill the first week of the month with the appropriate number of blanks.
for($week_day = 0; $week_day < $first_week_day; $week_day++)
{
$calendar_html .= "<td style=\"background-color:9999cc; color:000000;\"> </td>";
}

$week_day = $first_week_day;
for($day_counter = 1; $day_counter <= $days_in_this_month; $day_counter++)
{
$week_day %= 7;

if($week_day == 0)
$calendar_html .= "</tr><tr>";

//Do something different for the current day.
if($day == $day_counter)
$calendar_html .= "<td align=\"center\"><b>" . $day_counter . "</b></td>";
else
$calendar_html .= "<td align=\"center\" style=\"background-color:9999cc; color:000000;\">&nbsp;" .
$day_counter . " </td>";

$week_day++;
}

$calendar_html .= "</tr>";
$calendar_html .= "</table>";

return($calendar_html);
}
?>

To show my calendar. All I need to do is create a database that will store what dates should be what color and display it on this calendar.

Any tips on how I should pull this off? I'm stumped. Or, perhaps you know of a better starting place. I don't need one that handles events and all that jazz...just color changing.

AjiNIMC

3:09 am on Jan 8, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Try webcalendar, it is an open source application. You will get a lot of ideas from there.

inveni0

3:24 pm on Jan 9, 2006 (gmt 0)

10+ Year Member



I've looked at WebCalendar, but it has way too much code to wade through. Like I said, I'd like to only be able to control the background color on specific dates. It seems like it would be more work to delete all of the other code in WebCalendar than to just write some fresh code. However, if WebCalendar is the only way, I guess I'll have to do it that way.

Does anyone have any other suggestions?

coopster

5:15 pm on Jan 9, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Why not just add markup style to the <td> for weekends (or weekdays) much like you are doing for "today" in the code? Check to see if the date being retrieved is a weekend and if so add your style to it.

inveni0

5:25 pm on Jan 9, 2006 (gmt 0)

10+ Year Member



Because it may not be just weekends and weekdays. I'd like to be able to say "January 14th should be red" and "January 23rd should be white." The idea is that the colors correspond to a chart. Days of certain colors have certain rules. Similar to how a themepark may have a calendar that uses colors to show what days are open during what hours. I'm just not sure what method would be the easiest to implement and change this information.

coopster

5:44 pm on Jan 9, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



The same concepts apply to the formatting -- we need to do some type of comparison and then apply formatting based on our "rules" or "theme".

First, I have found it much, much easier to use a class and then modify the markup in an external CSS file. Easy to change markup later if desired and less clutter in the code too. Plus you can double-up on markup easier if necessary. Example:

<td class="sunday">08</td> 
<td class="monday today">09</td>
<td class="tuesday">10</td>

Now, as far as the theme-based stuff, you might want to store that information. Use whatever works best for you, an array defined in your script, a text file, a database table, etc. Then, during the looping process compare the date to your "ruleset" or "theme" information to see if you need to apply markup. Here is a very rough idea:

$myThemeDates = (
'0110' => 'Jan10'
);
while (looping) {
$class = ''; // initialize
if (in_array($mmdd, $myThemeDates)) {
$class = $myThemeDates[$mmdd];
}
print "<td$class>$dayNumber</td>";
}