Well, after I got my feet wet with a
Petals Around the Rose program, I decided it was time to move on to bigger and better things. So, I made an
Events Calendar. Feel free to check it out and add your own events. I will eventually delete your entries as they are for test purposes only.
I don't have a zip program here at work, so I am just going to include the source below. Let me know if you have any suggestions. Thanks.
EDIT:Check out the comments towards the bottom of the displayevent.php file that talk about *age. A pretty neat feature in my opinion. There is a lot of potential for other uses of this as well. Be creative!
ccalendar.php
<?php
/************************************************
* ccalendar.php *
* *
* Created by: Mr. Tie Dye *
* 5/11/05 *
* *
************************************************/
class ccalendar
{
var $month; //the month of the displayed calendar
var $numOfDays; //the number of days in the displayed month
var $startDay; //the day of the week the month starts on
var $endDay; //the day of the week the month ends on
var $year; //the year of the displayed calendar
var $numOfEvents; //the number of events in the mySQL database
var $events = array(); //An array to hold the events
/************************************************
* The constructor for ccalendar. This sets*
* up some of the initial variables. *
* ccalendar takes a single integer for its*
* argument. This specifies which month we *
* are dealing with. *
************************************************/
function ccalendar($m)
{
$this->month = $m; //assign the month
if(!$_POST['year']) //if a year has not been posted,
{ //then we must be dealing with the
//current year.
$this->year = date('Y');
}
else //if a year has been posted, then
{ //that is the year we need to deal
//with
$this->year = ($_POST['year']);
}
if($this->month == 13) //wrap around the month so we don't
{ //end up with a month larger than 12
$this->month = 1;
$this->year++;
}
if($this->month == 0) //wrap around the month so we don't
{ //end up with a month less than 1
$this->month = 12;
$this->year--;
}
$this->numOfDays = $this->getNumOfDays(); //get the number of days
$this->startDay = $this->findStartDay(); //find the start day
}
/****************************************
* Now that out ccalendar class is *
* initilized, we can draw it *
****************************************/
function drawCalendar()
{
?>
<table border=4> <?php //we want a thick border ?>
<tr>
<?php //This will display the name of the month at the top of the calendar ?>
<td colspan=7 align=center><font size=10> <?php echo $this->getCalMonth(); echo ' '; echo $this->year; ?> </font></td>
</tr>
<tr>
<?php //Now, display each week day ?>
<th width=14%><font size=5>Sunday</font></th>
<th width=14%><font size=5>Monday</font></th>
<th width=14%><font size=5>Tuesday</font></th>
<th width=14%><font size=5>Wednesday</font></th>
<th width=14%><font size=5>Thursday</font></th>
<th width=14%><font size=5>Friday</font></th>
<th width=14%><font size=5>Saturday</font></th>
</tr>
<tr>
<?php
$currentWeekDay = $this->startDay; //find out what day this calendar starts on
$dayOfMonth = date('j'); //find out what day it is currently
$this->extractEvents(); //get all the events for this month
//draw empty table squares until we get to the first day of the month
for($i=1; $i < $this->startDay; $i++)
{
?> <td></td> <?php
}
//now loop until we are at the end of the month
for($a=1; $a < ($this->numOfDays+1); $a++)
{
//If we are at the current day, highlight it in yellow
if($dayOfMonth == $a && $this->month == date('n') && $this->year == date('Y'))
{
?> <td align=right valign=top height=100 bgcolor="#ffff00"> <?php
}
else
{
//otherwise, leave it white
?> <td align=right valign=top height=100> <?php
}
//put in the date and make it a link to insert.php with the arguments for this day
//This will allow us to set the defaults in insert.php
?>
<a href="insert.php?date=<?php echo $a; echo '&month='; echo $this->month; echo '&year='; echo $this->year;?>"><?php echo $a; ?></a><br>
<?php
//loop through the events
for($b=0; $b < $this->numOfEvents; $b++)
{
//if the event belongs to this month and year
//or, the event belongs to this month and it is re-occuring
if($this->events[$b]['month'] == $this->month && $this->events[$b]['date'] == $a && ($this->year == $this->events[$b]['year'] || $this->events[$b]['occur'] == 1))
{
//make a link to displayevent.php with the event id
//as the argument
?>
<a href="displayevent.php?id=<?php echo $this->events[$b]['id'];
?>" target="new"> <?php
echo $this->events[$b]['title']; ?> </a><br><?php
}
}
?> </td><?php
//We are done with that day, so increment to the next day
$currentWeekDay ++;
//wrap around the week day so we don't wind up with
//a week day larger then 7
if($currentWeekDay == 8)
{
?> </tr> <tr> <?php
$currentWeekDay = 1;
}
}
//By now, we are at the end of the month, so remember
//which week day this month ended on
$this->endDay = $currentWeekDay;
//wrap around the week day so we don't wind up with
//a week day larger then 7
if($this->endDay == 8)
{
$this->endDay = 1;
}
if($this->startDay == 8)
{
$this->startDay = 1;
}
?>
</tr>
<tr>
<td align=left>
<?php //add a button on the bottom left for the previous month ?>
<form action="index.php" method="post">
<input type="hidden" name="month" value="<?php echo $this->month-1; ?>">
<input type="hidden" name="year" value="<?php echo $this->year; ?>">
<input type="hidden" name="endDay" value="<?php echo $this->findPrevStartDay(); ?>">
<input type="submit" value="Previous Month">
</form>
</td>
<td>
</td>
<td>
</td>
<td>
</td>
<td>
</td>
<td>
</td>
<td align=right>
<?php //add a button on the bottom right for the next month ?>
<form action="index.php" method="post">
<input type="hidden" name="month" value="<?php echo $this->month+1; ?>">
<input type="hidden" name="year" value="<?php echo $this->year; ?>">
<input type="hidden" name="endDay" value="<?php echo $this->endDay; ?>">
<input type="submit" value="Next Month">
</form>
</td>
</tr>
</table>
<?php
}
/************************************************
* Since integers are easier to deal *
* with, we pass the month as an *
* integer. But sometimes, we want it *
* converted to a string for *
* displaying *
************************************************/
function getCalMonth()
{
//switch the month to determine with month we are talking about
switch($this->month)
{
case 1:
$currMonth = 'January';
break;
case 2:
$currMonth = 'February';
break;
case 3:
$currMonth = 'March';
break;
case 4:
$currMonth = 'April';
break;
case 5:
$currMonth = 'May';
break;
case 6:
$currMonth = 'June';
break;
case 7:
$currMonth = 'July';
break;
case 8:
$currMonth = 'August';
break;
case 9:
$currMonth = 'September';
break;
case 10:
$currMonth = 'October';
break;
case 11:
$currMonth = 'November';
break;
case 12:
$currMonth = 'December';
break;
}
return $currMonth; //return the string
}
/************************************************
* We need to know how many days are *
* in a given month. Since this varies *
* from month to month... *
*************************************************/
function getNumOfDays()
{
switch($this->month)
{
case 1:
$numDays = 31;
break;
case 2:
$numDays = 28;
break;
case 3:
$numDays = 31;
break;
case 4:
$numDays = 30;
break;
case 5:
$numDays = 31;
break;
case 6:
$numDays = 30;
break;
case 7:
$numDays = 31;
break;
case 8:
$numDays = 31;
break;
case 9:
$numDays = 30;
break;
case 10:
$numDays = 31;
break;
case 11:
$numDays = 30;
break;
case 12:
$numDays = 31;
break;
}
return $numDays; //return the number of days in this month
}
/****************************************
* We need to know what day of the *
* week the month starts on. *
****************************************/
function findStartDay()
{
//if we are dealing with the current month...
if($this->month == date('n'))
{
//find the current day of the month
$currDate = date('j',time());
//find the current day of the week
$currDay = date('D',time());
$day = 0;
//Since integers are easier to deal with, we need
//to convert the current day of the week to an integer
switch ($currDay)
{
case "Sun":
$day = 1;
break;
case 'Mon':
$day = 2;
break;
case 'Tue':
$day = 3;
break;
case 'Wed':
$day = 4;
break;
case 'Thu':
$day = 5;
break;
case 'Fri':
$day = 6;
break;
case 'Sat':
$day = 7;
break;
}
//loop through the entire month
for($i = 0; $i < $this->numOfDays; $i++)
{
//if we are at the start of the month, stop looping
if($currDate == 1)
{
break;
}
//since we are trying to get to the beginning of the month,
//decrement the day.
$day --;
//wrap around the day so we don't end up with a week day less
//then 0.
if($day == 0)
{
$day = 7;
}
//decrement the date as well
$currDate --;
}
return($day); //return the starting day
}
else
{
//if we are not dealing with the current month, then someone
//clicked on the 'next month' button. Use
//the value submitted then for the first day of this month
return($_POST['endDay']);
}
}
/************************************************
* This function will find the start *
* day of the month if the user *
* clicked on 'previous month' button *
************************************************/
function findPrevStartDay()
{
$day = $this->startDay; //find the first day
$this->month --; //decrement the month
$num1OfDays = $this->getNumOfDays(); //get the number of days
$this->month ++; //set the month back to what it was
$currDate = $num1OfDays; //find the end of the month
//loop through the entire month
for($i = 0; $i < $num1OfDays+1; $i++)
{
//if we are at the start of the month, we are done
if($currDate == 1)
{
break;
}
//since we are trying to get to the beginning of the month,
//decrement the day.
$day --;
//wrap around the day so we don't end up with a week day less
//then 0
if($day == 0)
{
$day = 7;
}
//decremnt the date as well
$currDate --;
}
return($day); //return the starting day
}
/************************************************
* This calendar uses mySQL to store *
* events. We need some way of getting *
* those events out so we can display *
* them. *
************************************************/
function extractEvents()
{
include("database.php"); //contains log-in info
//select the table called calendar and query it
$query="SELECT * FROM calendar";
$result=mysql_query($query);
//get the number of entires in the table
$num=mysql_numrows($result);
//close the connection to mySQL
mysql_close();
$i=0;
$this->numOfEvents = 1;
//loop through every entry in the database
while ($i < $num)
{
//is we are at an entry that belongs on this calendar month, then add
//it to the $events array.
if(($this->month == mysql_result($result,$i,"month")) && (($this->year == mysql_result($result,$i,"year")) || (mysql_result($result,$i,"reoccur") == 1)))
{
$this->events[$this->numOfEvents]['id']=mysql_result($result,$i,"id");
$this->events[$this->numOfEvents]['date']=mysql_result($result,$i,"date");
$this->events[$this->numOfEvents]['month']=mysql_result($result,$i,"month");
$this->events[$this->numOfEvents]['year']=mysql_result($result,$i,"year");
$this->events[$this->numOfEvents]['occur']=mysql_result($result,$i,"reoccur");
$this->events[$this->numOfEvents]['title']=mysql_result($result,$i,"title");
$this->events[$this->numOfEvents]['desc']=mysql_result($result,$i,"desc");
$this->events[$this->numOfEvents]['usr']=mysql_result($result,$i,"usr");
$this->numOfEvents ++; //we have found an event that belongs to this month
//so we need to count it
}
$i++;
}
}
}
?>
database.php
<?php
/********************************************************************************
* You will need to set up a table in a mySQL database for the calendar. *
* This table will need to be called 'calendar'. You will need the *
* following fields in the table. *
* *
* Field Type Length Auto-increment *
* *
* id int 6 yes *
* date int 2 *
* month int 2 *
* year int 4 *
* reoccur int 1 *
* title varchar 30 *
* desc varchar 255 *
* usr varchar 15 *
* *
* Also, you will need to adjust the following variables to match the *
* information for your particualar database. *
* *
******************************************************************************/
$username="Your_username";
$password="your_password";
$database="your_database";
mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
?>
displayevent.php
<html>
<?php
include("database.php"); //contains log-in info
//select the calendar table and query it
$query="SELECT * FROM calendar";
$result=mysql_query($query);
//get the number of entries in the table
$num=mysql_numrows($result);
//close the connection to mySQL
mysql_close();
$i=0;
//loop through all entries
while ($i < $num)
{
//if the id of the entry matches the id submitted,
//then we need to hang on to all the info for that
//particular entry.
if(mysql_result($result,$i,"id") == $_GET['id'])
{
$id = mysql_result($result,$i,"id");
$date = mysql_result($result,$i,"date");
$month = mysql_result($result,$i,"month");
$year = mysql_result($result,$i,"year");
$occur = mysql_result($result,$i,"reoccur");
$title = mysql_result($result,$i,"title");
$desc = mysql_result($result,$i,"desc");
$usr = mysql_result($result,$i,"usr");
break;
}
$i++;
}
?>
<head>
<title>Event for <?php echo $month; echo '/'; echo $date; echo '/'; echo $year; ?> </title>
</head>
<body>
<p>The following event was added by <?php echo $usr; ?> </p>
<p>Date added: <?php echo $month; echo '/'; echo $date; echo '/'; echo $year; ?> </p>
<h3>Title: <?php echo $title; ?> </h3>
<h3>Description:
<?php
/********************************************************
* This loop will go through the description and *
* find out if the the spicifier *age was entered *
* into the description. If it was, then *age will *
* be replaced with a numerical representation of *
* the difference between the current year and the *
* year entered for the event. So, you could put *
* something like "Happy Birthday. You are *age *
* years old!" in the description. But when you *
* view the event, you might see something like *
* "Happy Birthday. You are 27 years old!" *
********************************************************/
//Note that this only uses the year. I might get around
//to figuring this down to the day, but for now, it only
//works with the year
for($a = 0; $a < strlen($desc); $a++)
{
if($desc[$a] == '*')
{
if(($desc[$a +1] == 'a' || $desc[$a +1] == 'A') && ($desc[$a +2] == 'g' || $desc[$a +2] == 'G') && ($desc[$a +3] == 'e' || $desc[$a +3] == 'E'))
{
$age = date('Y') - $year;
echo $age;
$a += 4;
}
}
echo $desc[$a];
}
?>
</h3>
</body>
</html>
index.php
<html>
<head>
<title>Events Calendar</title>
</head>
<body>
<?php
include 'ccalendar.php'; //contains pretty much everything you need
//to display the calendar.
//if the month, year, and endday have been posted, then
//we need to use those values to create the calendar.
if($_POST['month'] && $_POST['year'] && $_POST['endDay'])
{
//if the posted month is the same as the current month,
//use the current month to create the calendar.
if($_POST['month'] == date('n'))
{
$tester = new ccalendar(date('n'), date('j'));
$tester->drawCalendar(); //draw the calendar
}
//otherwise, use the posted month to create the calendar
else
{
$tester = new ccalendar($_POST['month']);
$tester->drawCalendar(); //draw the calendar
}
}
//if the month, year, and endday have not been posted, then we need to
//display the current month. So, create a calendar based on the current
//date and month.
else
{
$tester = new ccalendar(date('n'), date('j'));
$tester->drawCalendar();
}
?>
</body>
</html>
insert.php
<html>
<head>
<title>Add an event</title>
</head>
<body>
<?php
if(!(isset($_POST['month'])))
{
?>
<form action="insert.php" method="post">
<table border="0">
<tr>
<td>
Month: </td><td><select name="month">
<option value="1" <?php if($_GET['month'] == 1){ echo selected; } ?> >Jan</option>
<option value="2" <?php if($_GET['month'] == 2){ echo selected; } ?> >Feb</option>
<option value="3" <?php if($_GET['month'] == 3){ echo selected; } ?> >March</option>
<option value="4" <?php if($_GET['month'] == 4){ echo selected; } ?> >April</option>
<option value="5" <?php if($_GET['month'] == 5){ echo selected; } ?> >May</option>
<option value="6" <?php if($_GET['month'] == 6){ echo selected; } ?> >June</option>
<option value="7" <?php if($_GET['month'] == 7){ echo selected; } ?> >July</option>
<option value="8" <?php if($_GET['month'] == 8){ echo selected; } ?> >Aug</option>
<option value="9" <?php if($_GET['month'] == 9){ echo selected; } ?> >Sept</option>
<option value="10" <?php if($_GET['month'] == 10){ echo selected; } ?> >Oct</option>
<option value="11" <?php if($_GET['month'] == 11){ echo selected; } ?> >Nov</option>
<option value="12" <?php if($_GET['month'] == 12){ echo selected; } ?> >Dec</option>
</select>
</td>
</tr>
<tr>
<td>
Date: </td><td><select name="date">
<option value="1" <?php if($_GET['date'] == 1){ echo selected; } ?> >01</option>
<option value="2" <?php if($_GET['date'] == 2){ echo selected; } ?> >02</option>
<option value="3" <?php if($_GET['date'] == 3){ echo selected; } ?> >03</option>
<option value="4" <?php if($_GET['date'] == 4){ echo selected; } ?> >04</option>
<option value="5" <?php if($_GET['date'] == 5){ echo selected; } ?> >05</option>
<option value="6" <?php if($_GET['date'] == 6){ echo selected; } ?> >06</option>
<option value="7" <?php if($_GET['date'] == 7){ echo selected; } ?> >07</option>
<option value="8" <?php if($_GET['date'] == 8){ echo selected; } ?> >08</option>
<option value="9" <?php if($_GET['date'] == 9){ echo selected; } ?> >09</option>
<option value="10" <?php if($_GET['date'] == 10){ echo selected; } ?> >10</option>
<option value="11" <?php if($_GET['date'] == 11){ echo selected; } ?> >11</option>
<option value="12" <?php if($_GET['date'] == 12){ echo selected; } ?> >12</option>
<option value="13" <?php if($_GET['date'] == 13){ echo selected; } ?> >13</option>
<option value="14" <?php if($_GET['date'] == 14){ echo selected; } ?> >14</option>
<option value="15" <?php if($_GET['date'] == 15){ echo selected; } ?> >15</option>
<option value="16" <?php if($_GET['date'] == 16){ echo selected; } ?> >16</option>
<option value="17" <?php if($_GET['date'] == 17){ echo selected; } ?> >17</option>
<option value="18" <?php if($_GET['date'] == 18){ echo selected; } ?> >18</option>
<option value="19" <?php if($_GET['date'] == 19){ echo selected; } ?> >19</option>
<option value="20" <?php if($_GET['date'] == 20){ echo selected; } ?> >20</option>
<option value="21" <?php if($_GET['date'] == 21){ echo selected; } ?> >21</option>
<option value="22" <?php if($_GET['date'] == 22){ echo selected; } ?> >22</option>
<option value="23" <?php if($_GET['date'] == 23){ echo selected; } ?> >23</option>
<option value="24" <?php if($_GET['date'] == 24){ echo selected; } ?> >24</option>
<option value="25" <?php if($_GET['date'] == 25){ echo selected; } ?> >25</option>
<option value="26" <?php if($_GET['date'] == 26){ echo selected; } ?> >26</option>
<option value="27" <?php if($_GET['date'] == 27){ echo selected; } ?> >27</option>
<option value="28" <?php if($_GET['date'] == 28){ echo selected; } ?> >28</option>
<option value="29" <?php if($_GET['date'] == 29){ echo selected; } ?> >29</option>
<option value="30" <?php if($_GET['date'] == 30){ echo selected; } ?> >30</option>
<option value="31" <?php if($_GET['date'] == 31){ echo selected; } ?> >31</option>
</select>
</td>
</tr>
<tr>
<td>
Year: </td><td><input type="text" name="year" size="6" maxlength="4" value="<?php echo $_GET['year']; ?>">
</td>
</tr>
<tr>
<td>
Re-Occuring?: </td><td><select name="occur">
<option value="1">Yes</option>
<option value="0" selected>No</option>
</select>
</td>
</tr>
<tr>
<td>
Title:</td><td> <input type="text" name="title" size="32" maxlength="30" value="<?php echo $_GET['title']; ?>">
</td>
</tr>
<tr>
<td>
Description: </td><td> <textarea name="desc" cols="35" rows="4" maxlength="20" wrap="virtual"><?php echo $_GET['desc']; ?></textarea>
</td>
</tr>
<tr>
<td>
Your Name: </td><td><input type="text" name="usr" size="18" maxlength="15" value="<?php echo $_GET['usr']; ?>">
</td>
</tr>
</table>
<input type="Submit" value="Submit">
</form>
<?php
}
if(isset($_POST['month']))
{
//if the user did not enter a year, warn them
if(!$_POST['year'])
{
?> <p>You did not enter a year </p> <?php
$invalid = 1;
}
//if the user did not enter a title, warn them
if(!$_POST['title'])
{
?> <p>You did not enter a title </p> <?php
$invalid = 1;
}
//if the user did not enter a description, warn them
if(!$_POST['desc'])
{
?> <p>You did not enter a descrpition </p> <?php
$invalid = 1;
}
//if the user did not enter a name, warn them
if(!$_POST['usr'])
{
?> <p>You did not enter your name </p> <?php
$invalid = 1;
}
//if any of the above warnings apply, refer the user back
//to the entry form
if($invalid)
{
?> <p>Please click <a href="insert.php?date=<?php echo $_POST['date']; echo '&month='; echo $_POST['month']; echo '&year='; echo $_POST['year']; echo '&title='; echo $_POST['title']; echo '&desc='; echo $_POST['desc']; echo '&usr='; echo $_POST['usr']; ?>">here</a> to enter the required fields.</p> <?php
}
else
{
//if they did not recieve any warnings, enter the info into the database
include("database.php");
$date=$_POST['date'];
$month=$_POST['month'];
$year=$_POST['year'];
$occur=$_POST['occur'];
$title=$_POST['title'];
$desc=$_POST['desc'];
$usr=$_POST['usr'];
$query = "INSERT INTO calendar VALUES ('','$date','$month','$year','$occur','$title','$desc','$usr')";
mysql_query($query);
mysql_close();
?><p>Your new event has been added. Please click <a href="index.php">here</a> to return to the calendar.</p><?php
}
}
?>
</body>
</html>
[Edited by - mrtie_dye on May 11, 2005 3:54:58 AM]