Hi all,
I posted yesterday regarding a problem I had (that was sorted when I actually opened my eyes and noticed my silly mistake - like using the wrong database d'oh :eek: ) but now I am moving to a new stage on my little project.
I am writing a calender app that will allow people in my department to know when we are all off on hols. So the plan was to display the dates vertically and to display the names of the people down the side. Now, it was to be colour coded so that if someone is off then in the box where the column for the date and the row for their name meets, will be turned green. Fair enough. Also all weekends are shown a different color.
I am kinda new to PHP and have got it so far to display all the months, to check for the weekends and color code those squares and to also display the name of the employee.
So here is my problem, right now I have this information in the database (MySQL sorry should have mentioned that)
Attend_ID - int auto increment
Date_From - Date
Date_To - Date
Duration - int
Employee - int
Mode - int
My basic plan would be this:
To take the Date_From and to compare that the the month shown
IF Date_From falls into the current month
set a counter to 0
wait until the table is being drawn and then change the colour of the square
increment a counter
compare counter to duration
if not equal colour next box.
I know this is a little hazy and wont take into account the weekends but I am looking for how I could possibly implement this into my code.
Any and all suggestions gratefully recieved.
Joe.
<?
//require("config.php");
/***
* This are the contents of the config file
*/
$link = mysql_connect("localhost", "team_lead_1", "")
or die("Could not connect: ". mysql_error());
$query = "SELECT * FROM tbl_attend";
$result = mysql_db_query("holiday", $query);
/***
*Set up variables to hold the month, date and year.
*/
if(!$currYear) { $currYear = date("Y"); }
if(!$currMonth) { $currMonth = date("n"); }
if(!$currDay) { $currDay = date("j"); }
/***
*Set up variables to display previous and next months correctly
*/
/***
*Defaults for previous month
*/
$prevMonth = $currMonth-1;
$prevYear = $currYear;
// if January, decrement year and set month to December
if ($prevMonth < 1)
{
$prevMonth=12;
$prevYear--;
}
/***
*Defaults for next month
*/
$nextMonth = $currMonth+1;
$nextYear = $currYear;
// if December, increment year and set month to January
if ($nextMonth > 12)
{
$nextMonth=1;
$nextYear++;
}
/***
*Arrays to hold the days and months and total days
*/
$days = array("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday");
$months = array("", "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
$totalDays = array(0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
/***
*Check for a leap year
*/
if (date("L", mktime(0,0,0,$currMonth,1,$currYear)))
{
$totalDays[2] = 29;
}
/***
*Find the First Day of the Month
*/
$firstDayOfMonth = date("w", mktime(0,0,0,$currMonth,1,$currYear));
/***
* Find if we are at a weekend or a weekday
*/
if (($days ==0)||($days ==6)) //Sunday is 0, Saturday is 6.
{
$color = "#99FFFF";
}
else
{
$color = "#FFFFFF";
}
/***
*Begin building the calender display
*/
echo "<table border='1' cellpadding='2' cellspacing='1'>";
echo "<tr>";
$gap = ($totalDays[$currMonth] - 10); // The length for the middle column of the table.
echo "<td colspan=". ($totalDays[$currMonth] + 1 )." align=center>Attendance</td></tr>";
?>
<tr><td colspan=5 align=left><a href="<? echo $PHP_SELF; ?>?currMonth=<? echo $prevMonth; ?>&currYear=<? echo $prevYear; ?>"><font face="Arial"size="-2"><<</font></a></td>
<?
echo "<td colspan=".$gap." align=center>". $months[$currMonth] . ", " . $currYear . "</td>";
?>
<td colspan=5 align=right><a href="<? echo $PHP_SELF; ?>?currMonth=<? echo $nextMonth; ?>&currYear=<? echo $nextYear; ?>"><font face="Arial"size="-2">>></font></font></a></td>
<?
echo "<td></td></tr>";
echo "<tr>";
for ($x=0,$y=$firstDayOfMonth; $x<$totalDays[$currMonth]; $x++,$y++)
{
if (($y == 0)||($y == 6)) //Sunday is 0, Saturday is 6.
{
$color = "#99FFFF";
}
else
{
$color = "#FFFFFF";
}
echo "<td bgcolor=".$color."><font face=Arial size=-2>" . substr($days[$y],0,3) . "</font></td>";
if ($y == 6)
{
$y = -1;
}
}
echo "<td></td>";
echo "</tr>";
echo "<tr>";
for ($x=1, $y=$firstDayOfMonth; $x<=$totalDays[$currMonth]; $x++,$y++)
{
if (($y == 0)||($y == 6)) //Sunday is 0, Saturday is 6.
{
$color = "#99FFFF";
}
else
{
$color = "#FFFFFF";
}
echo "<td bgcolor=".$color."><font face=Arial size=-2>" .$x ."</font></td>";
if ($y == 6)
{
$y = -1;
}
}
echo "<td><font face=Arial size=-2>Employee Name</font></td>";
echo "</tr>";
// Now begin to look for information to populate the calender
if ($result)
{
while ($r = mysql_fetch_array($result))
{
$Attend_ID = $r["Attend_ID"];
$Date_From = $r["Date_From"];
$Date_To = $r["Date_To"];
$Duration = $r["Duration"];
$Employee = $r["Employee"];
$Mode = $r["Mode"];
// Find the Employee Name from the ID stored in tbl_attend.
$query2 = "SELECT Employee_Name FROM tbl_employee WHERE Employee_ID=".$Employee;
$E_Name = mysql_db_query("holiday", $query2);
$row = mysql_fetch_assoc($E_Name);
extract($row);
// Add information to the table
for ($x=1, $y=$firstDayOfMonth; $x<=$totalDays[$currMonth]; $x++,$y++)
{
if (($y == 0)||($y == 6)) //Sunday is 0, Saturday is 1. But you knew that already :p
{
$color = "#99FFFF";
}
else
{
$color = "#FFFFFF";
}
echo "<td bgcolor=".$color." align=center><font face=Arial size=-2 color=white>.</font></td>";
if ($y == 6)
{
$y = -1;
}
}
echo "<td><font face=Arial size=-2>".$Employee_Name."</td></font>";
// echo "</td>";
echo "</tr>";
}
}
echo "</table>";
?>