Hey,
Ok I'm using a piece of code I found somewhere a long time ago, can't remember where, I have removed the copyright and notes because it takes up a lot of room in here, I've kept it on my file.
Below is the code to produce the calendar and also to list events taken from a database.
Unfortunately, it seems I have to use the return "info"; feature instead of print, these is causing headaches as I need to use a while() statement just incase there is more than one event on a day.
Using echo just outputs the details at the very top of the page, it doesn't put it in the correct cell.
Here is the code:
<?
//
// Configuration options for calendar function
//
// This script is written for a mysql database. I'm too busy (and ignorant)
// to write a wrapper for it. If you don't want to use the database features
// of this script, just leave the $db_host variable blank.
//
$SQLhost="localhost";
$SQLuser="";
$SQLpass="";
$SQLdb="";
$db_host = "localhost";
$db_name = "name";
$db_username = "user";
$db_passwd = "pass";
$db_table_name = "MEETINGS";
$db_column_name = "date_start";
$db_type = 1; // 0=simple 1=complex
//
// How many months across to display?
//
$num_columns_static = 3;
//
// Formatting variables -- change to your heart's content!
//
$month_table_header = "\n<table border=0 cellspacing=12>";
$month_row_header = "\n<tr>";
$month_td_tag = "<td valign=top>";
$cal_table_tag = "\n<table width=100% height=100% 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=left valign=top bgcolor=#efefef>";
$cal_day_closetd = "</td>";
$cal_event_opentd = "<td align=center bgcolor=#FFFFFF valign=top>";
$cal_event_closetd = "</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 = "\t\t<th bgcolor=#D3DCE3>\n";
$event_th_closetag = "\t\t</th>\n";
$event_td_opentag = "\t\t<td bgcolor=#CCCCCC>\n";
$event_td_closetag = "\t\t</td>\n";
//
// Connection to the database and selection of the correct db.
//
$db_link = mysql_connect($db_host, $db_username, $db_passwd);
$db = mysql_select_db($db_name, $db_link);
//
// Function fixlen($string) adds a zero to the front of the string if the
// string length does not equal 2
//
function fixlen($fixit) {
if (strlen($fixit)<2) {
return "0$fixit";
} else {
return $fixit;
}
}
//
// Function getcal($year) will generate a calendar for the given year.
//
function getcal($year) {
global $num_columns_static,$month_table_header,$month_row_header,$month_td_tag;
$num_of_months = 12;
$count_up = 1;
echo $month_table_header;
while ($count_up<=$num_of_months) {
echo $month_row_header;
$num_columns = $num_columns_static;
while (($num_columns>0) && ($count_up<=$num_of_months)) {
echo $month_td_tag;
echo gen_month($year,$count_up);
echo "</td>";
$count_up++;
$num_columns--;
}
echo "</tr>";
}
echo "\n</table>";
}
//
// Function gen_month($year,$month) will generate a month for
// the year and month given.
//
function gen_month($year,$month) {
global $cal_table_tag,$cal_mon_tr_tag,$cal_dayname_tr_tag,$cal_day_tr_tag;
global $cal_event_opentd, $cal_event_closetd;
global $cal_day_opentd, $cal_day_closetd;
$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 $cal_table_tag;
echo $cal_mon_tr_tag . "<td colspan=7 align=center><b>$mon_name $year</b></td></tr>";
echo $cal_dayname_tr_tag . "<td width=14% align=center>Sun</td><td width=14% align=center>Mon</td><td width=14% align=center>Tue</td><td width=14% align=center>Wed</td><td width=14% align=center>Thu</td><td width=14% align=center>Fri</td><td width=14% align=center>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> </td>";
$sb_cnt_td--;
}
while ($sb_cnt<$sb_max) {
//
// The $urldisp is what shows the number of the
// day. the gen_day_url() function makes the
// number linkable if there is data in a
// certain table for that day.
//
//
// $find_data is used in the SQL query in gen_day_url()
// If there is any occurrence of $find_data in
// $db_column_name, then a link is generated.
//
$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 is used in the SQL query in gen_day_url()
// If there is any occurrence of $find_data in
// $db_column_name, then a link is generated.
//
$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 is used in the SQL query in gen_day_url()
// If there is any occurrence of $find_data in
// $db_column_name, then a link is generated.
//
$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> </td>";
$sb_cnt_td--;
}
echo "$p_line</tr>";
echo "</table>";
}
//
// Function gen_day_url($day,$month,$year) makes either a
// a plain text day number, or a linked number, depending on if a record
// exists for that day.
//
function gen_day_url($data,$day) {
global $db_table_name,$db_column_name,$db_host, $db_link;
if (strlen($db_host)>0) {
$qrsql = "SELECT
meetingid,
title
FROM $db_table_name
WHERE $db_column_name='$data'";
$qr_ret = mysql_query($qrsql, $db_link);
$qr_num=mysql_num_rows($qr_ret);
if ($qr_num>0) {
//
// Here is where you put the link to the action
// you want to perform if there is data in the
// table.
//
while ($row=mysql_fetch_array($qr_ret)) {
$m_title = $row['title'];
$m_id = $row['meetingid'];
return "<p align=left>$day</p><p><a href=meetings.php?Section=viewmeeting&TableID=$m_id><font size=1 face=Verdana, Arial, Helvetica, sans-serif>$m_title</font></a></p>";
}
} else {
//
// Returns just the number of the day if there
// is no data in the table for that date
//
return $day;
}
} else {
//
// This returns just the number of the day, because
// the $db_host variable is empty (the user does not
// want to use the db functions of the snippet.
//
return $day;
}
}
?>
<?
// Here is the call!!!
gen_month($year,$month)
// getcal($year);
?>
Anyone have any suggestions, I know there is a lot of code above but I thought I should post it all.
Or is there a simpler alternative that will create a calendar for me and allow me to easily pull my data?
Cheers,
Chris