#native_company# #native_desc#
#native_cta#

Scrollable monthly calendar w/events from db

By Alex Autrey
on September 19, 2005

Version: 3.0

Type: Full Script

Category: Calendars/Dates

License: Other

Description: A twist on the calendar w/links to db posted by inferno, this program displays one month at a time with a list of events (from the db) below the calendar. Prev and Next links let you scroll thru any number of months and years.

<?php
/*
	calendar.php version 2.0

	Written by Aline Bernstein, [email protected], June 2002
	Adapted from the work of Joseph Cunningham, [email protected]
	Copyright: None

	Version 2.0 enhancements by John Krische, [email protected]
	- Changed to php long tags
	- Put single quotes around array subscript constants
	- Put an isset around the starting $yyyymm param
	
	Version 3.0 Security Risks fixed by Alex Autrey, [email protected]
	* Changed to short php tags
	* Now uses sessions without globals
	* Which helps elivate all of the glorious hacks we recieve.

	Displays a one-month calendar and a list of events for the month,
	retrieved from a database. Prev and Next links let you navigate
	to the next month. The mktime() function is used to roll through
	the months continuously and cross into the next year. Called with
	no parameters, it starts with the current month.

	CREATE TABLE calendar (
		id int(10) unsigned NOT NULL auto_increment,
		date date NOT NULL default '0000-00-00',
		description varchar(128) NOT NULL,
		PRIMARY KEY  (id)
	);

*/


// Edit the following variables for your environment

$hostname = "HostName";
$mysql_user = "Username";
$mysql_paswd = "Password";
$dbname = "DBName";

$db = mysql_connect($hostname,$mysql_user,$mysql_paswd);
mysql_select_db($dbname,$db);

$db_table_name =  'TableName';
$db_column_name =  'ColumnNameHoldingDate';
$pgm = $_SERVER['PHP_SELF'];

$cal_table_tag =  "n<table width=140 border=0 bgcolor=#DDDDDD>";
$cal_mon_tr_tag =  "n<tr bgcolor=#D3DCE3>";
$cal_dayname_tr_tag =  "n<tr bgcolor=#CCCCCC>";
$cal_day_tr_tag =  "n<tr>";
$cal_day_opentd = "<td align=center>";
$cal_day_closetd = "</td>";
$cal_event_opentd = "<td align=center bgcolor=#c0c0ff><b>";
$cal_event_closetd = "</b></td>";
$event_table_opentag =  "<table cellspacing=0 cellpadding=4 border=0>n";
$event_table_closetag =  "</table>n";
$event_tr_opentag =  "t<tr>n";
$event_tr_closetag =  "t</tr>n";
$event_th_opentag =  "tt<th bgcolor=#D3DCE3>n";
$event_th_closetag =  "tt</th>n";
$event_td_opentag =  "tt<td bgcolor=#CCCCCC>n";
$event_td_closetag =  "tt</td>n";

//Added for version 3.0 which gives better security.
//So you no longer have to have globals on.
session_start();
$_SESSION["db"] = $db;
$_SESSION["db_table_name"] = $db_table_name;
$_SESSION["db_column_name"] = $db_column_name;
$_SESSION["pgm"] = $pgm;
$_SESSION["cal_table_tag"] = $cal_table_tag;
$_SESSION["cal_mon_tr_tag"] = $cal_mon_tr_tag;
$_SESSION["cal_dayname_tr_tag"] = $cal_dayname_tr_tag;
$_SESSION["cal_day_tr_tag"] = $cal_day_tr_tag;
$_SESSION["cal_day_opentd"] = $cal_day_opentd;
$_SESSION["cal_day_closetd"] = $cal_day_closetd;
$_SESSION["cal_event_opentd"] = $cal_event_opentd;
$_SESSION["cal_event_closetd"] = $cal_event_closetd;
$_SESSION["event_table_opentag"] = $event_table_opentag;
$_SESSION["event_table_closetag"] = $event_table_closetag;
$_SESSION["event_tr_opentag"] = $event_tr_opentag;
$_SESSION["event_tr_closetag"] = $event_tr_closetag;
$_SESSION["event_th_opentag"] = $event_th_opentag;
$_SESSION["event_th_closetag"] = $event_th_closetag;
$_SESSION["event_td_opentag"] = $event_td_opentag;
$_SESSION["event_td_closetag"] = $event_td_closetag;


function fixlen($fixit) {
        if (strlen($fixit)<2) {
                return  "0$fixit";
        } else {
                return $fixit;
        }
}

function gen_month($year,$month) {
//Globals were removed from this section and replaced by Sessions.
$pgm = $_SESSION["pgm"];
$cal_table_tag = $_SESSION["cal_table_tag"];
$cal_mon_tr_tag = $_SESSION["cal_mon_tr_tag"];
$cal_dayname_tr_tag = $_SESSION["cal_dayname_tr_tag"];
$cal_day_tr_tag = $_SESSION["cal_day_tr_tag"];
$cal_day_opentd = $_SESSION["cal_day_opentd"];
$cal_day_closetd = $_SESSION["cal_day_closetd"];
$cal_event_opentd = $_SESSION["cal_event_opentd"];
$cal_event_closetd = $_SESSION["cal_event_closetd"];
$event_table_opentag = $_SESSION["event_table_opentag"];
$event_table_closetag = $_SESSION["event_table_closetag"];
$event_tr_opentag = $_SESSION["event_tr_opentag"];
$event_tr_closetag = $_SESSION["event_tr_closetag"];
$event_th_opentag = $_SESSION["event_th_opentag"];
$event_th_closetag = $_SESSION["event_th_closetag"];
$event_td_opentag = $_SESSION["event_td_opentag"];
$event_td_closetag = $_SESSION["event_td_closetag"];


    $mon_date = getdate(mktime(0,0,0,$month,1,$year));
    $mon_name = $mon_date['month'];
    $mon_total_days = strftime( "%d",mktime(0,0,0,$month+1,0,$year));
    echo "<a name="top"></a>";
    echo $cal_table_tag;
    $prev_date = getdate(mktime(0,0,0,$month-1,1,$year));
    $prev = $prev_date['year'] . fixlen($prev_date['mon']);
    $next_date = getdate(mktime(0,0,0,$month+1,1,$year));
    $next = $next_date['year'] . fixlen($next_date['mon']);
    echo $cal_mon_tr_tag . "<td align=left><a href=$pgm?yyyymm=$prev>Prev</a></td>";
    echo "<td colspan=5 align=center><b>$mon_name $year</b></td>";
    echo "<td align=right><a href=$pgm?yyyymm=$next>Next</a></td></tr>";
    echo $cal_dayname_tr_tag .  "<td>Sun</td><td>Mon</td><td>Tue</td><td>Wed</td><td>Thu</td><td>Fri</td><td>Sat</td></tr>";
    $day_count = 1;
    while ($day_count<=($mon_total_days-7)) {
        $fweek = getdate(mktime(0,0,0,$month,$day_count,$year));
        if ($day_count<=7) {
            $sb_cnt_td = $fweek['wday'];
            $sb_cnt = $fweek['wday'];
            $sb_max = 7;
            $p_line = $cal_day_tr_tag;
            while ($sb_cnt_td>=1) {
                $p_line .=  "n<td>&nbsp;</td>";
                $sb_cnt_td--;
            }
            while ($sb_cnt<$sb_max) {
                $find_data = $fweek['year'] ."-". fixlen($fweek['mon']) ."-". fixlen($day_count);
                $urldisp = gen_day_url($find_data,$day_count);
                if (strlen($urldisp) > 2)
                    $p_line .=  $cal_event_opentd . $urldisp . $cal_event_closetd;
                else
                    $p_line .=  $cal_day_opentd . $urldisp . $cal_day_closetd;
                $sb_cnt++;
                $day_count++;
            }
            echo  "$p_line</tr>";
        } else {
            $weekday_count = 1;
            $p_line = $cal_day_tr_tag;
            while ($weekday_count<=7) {
                $find_data = $fweek['year'] ."-". fixlen($fweek['mon']) ."-". fixlen($day_count);
                $urldisp = gen_day_url($find_data,$day_count);
                if (strlen($urldisp) > 2)
                    $p_line .=  $cal_event_opentd . $urldisp . $cal_event_closetd;
                else
                    $p_line .=  $cal_day_opentd . $urldisp . $cal_day_closetd;
                $weekday_count++;
                $day_count++;
            }
            $p_line .= "</tr>";
            echo $p_line;
        }
    }
    $sb_cnt_td = 7-($mon_total_days - $day_count);
    $p_line = $cal_day_tr_tag;
    while ($day_count<=$mon_total_days) {
        $find_data = $fweek['year'] ."-". fixlen($fweek['mon']) ."-". fixlen($day_count);
        $urldisp = gen_day_url($find_data,$day_count);
        if (strlen($urldisp) > 2)
            $p_line .=  $cal_event_opentd . $urldisp . $cal_event_closetd;
        else
            $p_line .=  $cal_day_opentd . $urldisp . $cal_day_closetd;
        $day_count++;
    }
    while ($sb_cnt_td>1) {
        $p_line .=  "n<td>&nbsp;</td>";
        $sb_cnt_td--;
    }
    echo  "$p_line</tr>";
    echo  "</table>";
}

function gen_day_url($data,$day) {
	$db_table_name = $_SESSION["db_table_name"];
	$db_column_name = $_SESSION["db_column_name"];
	$db = $_SESSION["db"];

    $qrsql =  "SELECT * FROM $db_table_name WHERE ($db_column_name='$data')";
    $qr_ret = mysql_query($qrsql, $db) or exit(mysql_error());
        $qr_num=mysql_num_rows($qr_ret);
        if ($qr_num>0) {
			$row=mysql_fetch_array($qr_ret);
			return  "<a href="#$day">$day</a>";
        } else {
            return $day;
        }
}

function show_events($year,$month) {
session_start();
$db_table_name = $_SESSION["db_table_name"];
$db_column_name = $_SESSION["db_column_name"];
$db = $_SESSION["db"];
$pgm = $_SESSION["pgm"];

      $date1 = date("Y-m-d", mktime(0,0,0,$month,1,$year));
      $date2 = date("Y-m-d", mktime(0,0,0,$month+1,1,$year));

      $qrsql =  "SELECT * FROM $db_table_name WHERE ($db_column_name>='$date1' " .
		"and $db_column_name<'$date2') ORDER BY $db_column_name";
      $qr_ret = mysql_query($qrsql, $db) or exit(mysql_error());
      $qr_num=mysql_num_rows($qr_ret);
	$lastday = 0;
	while($row=mysql_fetch_array($qr_ret)) {           		
		$event_date = $row[$db_column_name];
		$event_year = substr($event_date,0,4);
		$event_month = substr($event_date,5,2);
		$event_day = substr($event_date,8,2);
		$event_getdate = getdate(mktime(0,0,0,$event_month,$event_day,$event_year));
		$day_of_week = $event_getdate['weekday'];
		if ($lastday != $event_day) {
			if ($lastday <> 0) {
				echo "<br><a href="#top">Top</a>";
			}
			echo "<p><hr width=200 align="left"><a name="$event_day"><b>" .
			$event_getdate['weekday'] . ", " . $event_getdate['month'] . " " .
			$event_getdate['mday'] . "</b></a>";
		}
		$lastday = $event_day;
		echo "<br>" . $row['description'];
	}
	if ($lastday <> 0) {
		echo "<br><a href="#top">Top</a>";
	}
}


?>

<?php
  // Main
  $yyyymm = $_GET["yyyymm"];
  
  if (isset($yyyymm)) {
	if(strlen($yyyymm) == 6) {
     	$yyyy = substr($yyyymm, 0, 4);
     	$mm = substr($yyyymm, 4, 2);
  	}
  } else {
    $today = getdate();
    $yyyy = $today['year'];
    $mm = $today['mon'];
  }

  gen_month($yyyy, $mm);
?>