Well, here is something that I am sure is easy, but I am stuck...
What I have so far is a calendar which utilizes mysql to schedule events on a day. It also highlights the current day in another color and highlights the selected day in another color.
Everything is going smoothly so far, (with the help of some of you) but the problem I am having is when I want to compare the date value for each cell in any given month to the dates stored in my dates table. If the cells date value is equal to any date from the dates table, then do something special to let you know that there is something scheduled for that day.
The problem that keeps happening is when i try to compare using a while statement that loops through the values of the dates table and compares to the cell date value, it will echo all the cells "x" times, where "x" is the number of records in the dates table. Otherwise, with no while loop, it will only indicate "something special" in the cell whose date value is equal to the first date in the dates table.
So how the hell do I do this correctly????
Here is my code so that you can see what I'm doing....Its not as pretty w/o my css file and the javascript popup wont work either.
<?
/*-----------------------------------------------------------|
| My PHP Calendar |
| Written by Amy Parscal |
|-----------------------------------------------------------
///////////////////////////////////////////////////////////////////////////////
#
# Table structure for table 'Dates'
#
CREATE TABLE /*!32300 IF NOT EXISTS*/ Dates (
DateID int(11) NOT NULL auto_increment,
thisdate date DEFAULT '0000-00-00' ,
PRIMARY KEY (DateID)
);
#
# Table structure for table 'Events'
#
CREATE TABLE /*!32300 IF NOT EXISTS*/ Events (
EventID int(11) NOT NULL auto_increment,
ItemID int(11) ,
UserID int(11) ,
stimestamp int(10) ,
etimestamp int(10) ,
description text ,
PRIMARY KEY (EventID)
);
#
# Table structure for table 'Items'
#
CREATE TABLE /*!32300 IF NOT EXISTS*/ Items (
ItemID int(11) NOT NULL auto_increment,
item varchar(30) ,
PRIMARY KEY (ItemID)
);
//////////////////////////////////////////////////////////////////////////////
*/
require('require.php');
/*Lets the script know to grab todays date if none has been selected
*NOTE* I NEED THIS CODE FOR THE next/prev TO WORK CORRECTLY!!!!!!!*/
$month = (!$month) ? date("n",mktime()) : "$month"; /*if $month is not set, get current time
otherwise, use $month value*/
$year = (!$year) ? date("Y",mktime()) : "$year"; /*if $year is not set, get current time
otherwise, use $year value*/
/*These variables are defined to select the class*/
$thisday = date("d");
$thismonth = date("m");
$thisyear = date("Y");
/*determines the blank cell offset based on the 1st of the month*/
$offset = date("w", mktime(0, 0, 0, $month, 1, $year));
/*Gets the titles for each month*/
$month_title = date("F", mktime(0, 0, 0, $month, 1, $year));
/*Gets the total number of days for each month*/
$month_days = date("t", mktime(0, 0, 0, $month, 1, $year));
/*An array of all the weeks by name*/
$day_titles = array('S', 'M', 'T', 'W', 'T', 'F', 'S');
?>
<html>
<head>
<title>My Calendar<? echo ' - ' . $month_title . ' ' . $year ?></title>
<meta http-equiv="content-type" content="text/html;charset=iso-8859-1">
<link rel="stylesheet" href="assets/style.css" type="text/css">
<script src="assets/functions.js"></script>
</head>
<body class="body">
<strong>My PHP Calendar</strong><br>
<br>
<table class="normal" border="0" cellpadding="3" cellspacing="0">
<tr>
<td class="current" align="left" style="cursor: pointer; cursor: hand" onClick="document.location='<? echo $SCRIPT_NAME ?>?month=<? $prev_month = (($month - 1) < 1) ? 12 : $month - 1; echo $prev_month ?>&year=<? $prev_year = (($month - 1) < 1) ? $year - 1 : $year; echo $prev_year ?>'"><<</td>
<td class="current" colspan="5" align="center"><? echo $month_title . ' ' . $year ?></td>
<td class="current" align="right" style="cursor: pointer; cursor: hand" onClick="document.location='<? echo $SCRIPT_NAME ?>?month=<? $next_month = (($month + 1) > 12) ? 1 : $month + 1; echo $next_month ?>&year=<? $next_year = (($month + 1) > 12) ? $year + 1 : $year; echo $next_year ?>'">>></td>
</tr>
<tr>
<?
for($i = 0; $i < 7; $i++){
echo '<th>' . $day_titles["$i"] . '</th>';
}
//begin printing blank offset cells
echo '<tr>';
if($offset > 0)
echo str_repeat('<td class="blank"> </td>', $offset);
//begin printing days
for($day = 1; $day <= $month_days; $day++){
$month_count = substr_count($month,"0");
if($month_count < 1){
if($month < 10)
$month = '0' . $month;
}
$day_count = substr_count($day,"0");
if($day_count < 1){
if($day < 10)
$day = '0' . $day;
}
if($thisdate == ($year . '-' . $month . '-' . $day)){
$class = 'selected';
}
else{
if(($day == $thisday) && ($month == $thismonth && ($year == $thisyear))){
$class = 'current';
}
else{
$class = 'normal';
}
}
///////////////////////////////////////////////////////////////////////
//selects the dates from the Dates table so we can compare whats there with the calendar cells values
$select_dates = mysql_query("SELECT * FROM Dates");
$display_dates = mysql_fetch_array($select_dates);
$strip = strtotime("$display_dates[thisdate]");
$dbday = strftime('%d', $strip);
$dbmonth = strftime('%m', $strip);
$dbyear = strftime('%Y', $strip);
///////////////////////////////////////////////////////////////////////
?>
<td class="<? echo $class ?>" width="50" height="50" align="left" valign="top" style="cursor: pointer; cursor: hand" onClick="document.location='<? echo $SCRIPT_NAME . '?month=' . $month . '&year=' . $year . '&thisdate=' . date("Y-m-d", mktime(0, 0, 0, $month, $day, $year)) ?>'">
<?
///////////////////////////////////////////////////////////////////////
//echo x in the cell that has the same This is what decides if the month-day-year that are echoed are the same as any in the table 'Dates', and if so,
if($day == $dbday & $month == $dbmonth & $year == $dbyear)
echo 'x';
///////////////////////////////////////////////////////////////////////
if($day < 10){
$preg_day = preg_replace("'0'", "", $day);
echo $preg_day .'</td>';
}
else{
echo $day . '</td>';
}
$offset++;
/*end first row, begin rest of rows*/
if($offset > 6){
$offset = 0;
echo '</tr>';
if($day > $month_days)
echo '<tr>';
}
}
/*begin printing blank cells at end of table*/
if($offset > 0){
$offset = 7 - $offset;
if($offset >= 1){
echo str_repeat( '<td class="blank"> </td>', $offset );
}
}
/* end the table */
/*mysql queries*/
$thisdate = (!$thisdate) ? date("Y-m-d", mktime(0, 0, 0, $month, $thisday, $year)) : "$thisdate";
$string = "SELECT * FROM Dates WHERE ThisDate='";
if (!$thisdate) {
if(!$day){
$string .= date("Y-m-d", mktime(0, 0, 0, $month, $thisday, $year));
}
else{
$string .= date("Y-m-d", mktime(0, 0, 0, $month, $day, $year));
}
}
else{
$string .= $thisdate;
}
$string .="'";
$date = mysql_query($string);
if(!$date){
echo("Can't retrieve data Mysql error message for query " . mysql_error());
exit();
}
$date_info = mysql_fetch_array($date);
$event = mysql_query("SELECT Dates.DateID, Events.EventID, Events.description, Events.stimestamp, Events.etimestamp FROM Events, Dates, RelEventDate WHERE Dates.DateID='$date_info[DateID]' AND Dates.DateID = RelEventDate.DateID AND Events.EventID = RelEventDate.EventID ORDER BY stimestamp");
if(!$event){
echo("Can't retrieve data Mysql error message for query " . mysql_error());
exit();
}
?>
</tr>
</table>
<a target="_blank" href="new.php?thisdate=<? echo $thisdate ?>">Schedule Time</a><br>
<table width="100%" border="0" cellpadding="0" cellspacing="0">
<tr>
<th>Start Time</th>
<th>End Time</th>
<th>Description</th>
</tr>
<?
/*Grabs the event info for the day selected*/
while($event_info = mysql_fetch_array($event)){
if($class == 'row1') $class = 'row2';
else $class = 'row1';
/*Converts the start/end timestamps to a readable "time" format*/
$stimestamp = strftime( '%I:%M %p', $event_info["stimestamp"]);
$etimestamp = strftime( '%I:%M %p', $event_info["etimestamp"]);
/*Converts the start/end timestamps to a readable "date" format*/
$sdatestamp = strftime( '%Y-%m-%d', $event_info["stimestamp"]);
$edatestamp = strftime( '%Y-%m-%d', $event_info["etimestamp"]);
?>
<tr>
<td class="<? echo $class ?>" style="cursor: pointer; cursor: hand" onClick="popUpWindow('change.php?EventID=<? echo $event_info["EventID"] . '&thisdate=' . $thisdate . '&stimestamp=' . $event_info["stimestamp"] . '&etimestamp=' . $event_info["etimestamp"] ?>', 600, 0, 400, 300)"><? echo $stimestamp . ' - ' . $sdatestamp ?></td>
<td class="<? echo $class ?>" style="cursor: pointer; cursor: hand" onClick="popUpWindow('change.php?EventID=<? echo $event_info["EventID"] . '&thisdate=' . $thisdate . '&stimestamp=' . $event_info["stimestamp"] . '&etimestamp=' . $event_info["etimestamp"] ?>', 600, 0, 400, 300)"><? echo $etimestamp . ' - ' . $edatestamp ?></td>
<td class="<? echo $class ?>" style="cursor: pointer; cursor: hand" onClick="popUpWindow('change.php?EventID=<? echo $event_info["EventID"] . '&thisdate=' . $thisdate . '&stimestamp=' . $event_info["stimestamp"] . '&etimestamp=' . $event_info["etimestamp"] ?>', 600, 0, 400, 300)"><? echo $event_info["description"] ?></td>
</tr>
<? } ?>
</table>
</body>
</html>