wikedawsum Posted February 25, 2008 Share Posted February 25, 2008 Hello all. I am attempting to create an events calendar. I have my calendar setup with PHP and it pulls my events from a MySQL database. The events are stored with day, month, year, name, and desc. Everything is working great except for one part. I would like for table cells to be highlighted in a different color for days with events. I am assuming that this needs to be done with some sort of array, but my knowledge of arrays is pretty much nothing. I attempted to create one from an example I found, but I'm pretty sure I have botched it up. To create the calendar I followed some tutorials I found online. As I said, everything works great except for this one part. My calendar displays nicely, the current day is highlighted in a different color, and when I click on a day with an event, the events are displayed below the calendar. I just need to get those days with events to be highlighted. Below is my entire calendar script with the array I attempted to create (beginning of array is marked with //array). <?php $conn = mysql_connect("localhost", "dbcontent", "bc5106") or die($msg_no_connect); mysql_select_db("content") or die(mysql_error()); // Get values from query string $day = $_GET["day"]; $month = $_GET["month"]; $year = $_GET["year"]; $sel = $_GET["sel"]; if($day == "") $day = date("d"); if($month == "") $month = date("n"); if($year == "") $year = date("Y"); $currentTimeStamp = strtotime("$year-$month-$day"); $monthName = date("F", $currentTimeStamp); $counter = 0; $events = "SELECT * FROM events WHERE day = '$day' and month = '$month' and year = '$year'"; $res = mysql_query($events); if(!$res){ $err=mysql_error(); echo $err; exit; } //Here we generate the first day of the month $first_day = mktime(0,0,0,$month, 1, $year) ; //This gets us the month name $title = date('F', $first_day) ; //Here we find out what day of the week the first day of the month falls on $day_of_week = date('D', $first_day) ; //Once we know what day of the week it falls on, we know how many blank days occure before it. If the first day of the week is a Sunday then it would be zero switch($day_of_week){ case "Sun": $blank = 0; break; case "Mon": $blank = 1; break; case "Tue": $blank = 2; break; case "Wed": $blank = 3; break; case "Thu": $blank = 4; break; case "Fri": $blank = 5; break; case "Sat": $blank = 6; break; } //We then determine how many days are in the current month $days_in_month = cal_days_in_month(0, $month, $year) ; //Here we start building the table heads echo "<table border=0 width=350 cellpadding=2 cellspacing=0 bgcolor=#FEF0FB>"; echo "<tr class='header'><td width=42>S</td><td width=42>M</td><td width=42>T</td><td width=42>W</td><td width=42>T</td><td width=42>F</td><td width=42>S</td></tr>"; //This counts the days in the week, up to 7 $day_count = 1; echo "<tr>"; //first we take care of those blank days while ( $blank > 0 ) { echo "<td></td>"; $blank = $blank-1; $day_count++; } //sets the first day of the month to 1 $day_num = 1; //array function ReadEvents($Month) { // We will get all of the events for this month into an associative // array and that array will then be returned $query = "SELECT * FROM events"; $result = mysql_query($query); $theEvents = mysql_fetch_array($result); $eventCounter = 0; // Seperate the data into line-seperated arrays $arrEvents = explode("\r\n", $theEvents); // Loop through the results and pick the arrays // that match the selected month for($i = 0; $i < sizeof($arrEvents); $i+=3) { // Get each part of the events date as an index // of an array $arrEventDate = explode(" ", $arrEvents[$i]); // If the month is the selected month the grab // the details of this event if((int)$arrEventDate[0] == (int)$Month) { $theEvents[$eventCounter++] = array("day" => $arrEventDate[1], "name" => $arrEvents[$i+1], "desc" => $arrEvents[$i+2]); } } return $theEvents; foreach($arrEvents as $eventEntry) { if($eventEntry["day"] == $i) { // We have at least one event for the day $hasEvent = true; $numEventsThisMonth++; } } } //count up the days, until we've done all of them in the month while ( $day_num <= $days_in_month ) { if($day_num == $day && $month == date("n") && $year == date("Y") && $sel == 1) echo "<td class='selected'><center>$day_num</center></td>"; else if($day_num == $day && $sel == 1) echo "<td class='selected'><center>$day_num</center></td>"; else if($hasEvent == true) echo "<td class='event'><center><a href='events2.php?sel=1&day=$day_num&month=$month&year=$year'>$day_num</a></center></td>"; else if ($day_count == 1 || $day_count == 7) { echo "<td><center><a href='events2.php?sel=1&day=$day_num&month=$month&year=$year' class='weekend'>$day_num</a></center></td>"; } else if ($day_num == date("d") && $month == date("n") && $year == date("Y")) { echo "<td class='today'><center><a href='events2.php?sel=1&day=$day_num&month=$month&year=$year'>$day_num</a></center></td>"; } else { echo "<td class='normal'></center><a href='events2.php?sel=1&day=$day_num&month=$month&year=$year'>$day_num</a></center></td>"; } $day_num++; $day_count++; //Make sure we start a new row every week if ($day_count > 7) { echo "</tr><tr>"; $day_count = 1; } } //Finaly we finish out the table with some blank details if needed while ( $day_count >1 && $day_count <=7 ) { echo "<td> </td>"; $day_count++; } ?> </tr> <tr> <td width='50' colspan='1'> <input type='image' src='../images/previous.png' value=' < ' onClick='goLastMonth(<?php echo $month . ", " . $year; ?>)'> </td> <td width='250' colspan='5' class='cal'> <span class='title'><?php echo $monthName . " " . $year; ?><br> </td> <td width='50' colspan='1' align='right'> <input type='image' src='../images/next.png' value=' > ' onClick='goNextMonth(<?php echo $month . ", " . $year; ?>)'> </td> </tr> <?php if (mysql_num_rows($res) > 0) { echo "<tr>"; echo "<table width='350' cellpadding='2' cellspacing='0'>"; echo "<tr>"; echo " <th>"; echo " <hr size='1' color='#CACACA' noshade>"; echo " <span class='title'>Today's Events</span><br><br>"; echo " </th>"; echo " </tr>"; while ($row = mysql_fetch_assoc($res)) { echo "<tr>"; echo "<td>"; echo "<h2>{$row['name']}</h2> {$row['desc']}<br /><br />"; echo "<hr size='1' color='#CACACA' noshade>"; echo " </td>"; echo "</tr>"; } echo "</table>"; } else { echo "<tr>"; echo "<table width='350' cellpadding='2' cellspacing='0'>"; echo "<tr>"; echo " <th>"; echo " <hr size='1' color='#CACACA' noshade>"; echo " <span class='title'>There are no events for today.</span><br><br>"; echo " </th>"; echo " </tr>"; echo " </table>"; } ?> </table> If anybody can point me in the right direction on how to achieve this I would greatly appreciate it. My calendar would work fine as it is, but it would be a lot easier for users to know what days have events if they were highlighted. Quote Link to comment Share on other sites More sharing options...
wikedawsum Posted March 10, 2008 Author Share Posted March 10, 2008 I have been working on this event calendar some more, and I have gotten to the point where ONE event from each month is highlighted.. but ONLY one event. Below is the portion of my code that selects the events and displays the dates. $hasEvent = array(); $sql = mysql_query("SELECT day FROM events WHERE month = '$month' AND year = '$year' ORDER BY day"); if (mysql_num_rows($sql) > 0) { while ($row = mysql_fetch_array($sql)) { $hasEvent[$row['day']] = $row['name']; $today=$row['day']; } } //count up the days, until we've done all of them in the month while ( $day_num <= $days_in_month ) { if($day_num == $day && $month == date("n") && $year == date("Y") && $sel == 1) echo "<td class='selected'><center>$day_num</center></td>"; else if($day_num == $day && $sel == 1) echo "<td class='selected'><center>$day_num</center></td>"; else if($day_num == $today) echo "<td class='event'><center><a href='cal2.php?sel=1&day=$day_num&month=$month&year=$year'>$day_num</a></center></td>"; else if ($day_count == 1 || $day_count == 7) { echo "<td><center><a href='cal2.php?sel=1&day=$day_num&month=$month&year=$year' class='weekend'>$day_num</a></center></td>"; } else if ($day_num == date("d") && $month == date("n") && $year == date("Y")) { echo "<td class='today'><center><a href='cal2.php?sel=1&day=$day_num&month=$month&year=$year'>$day_num</a></center></td>"; } else { echo "<td class='normal'></center><a href='cal2.php?sel=1&day=$day_num&month=$month&year=$year'>$day_num</a></center></td>"; } $day_num++; $day_count++; You can view the calendar here: http://design.wikedawsum.com/test/pinkcompass/2/pages/cal2.php For March 2008, it has the 22nd highlighted, which is correct because there IS an event on that day. But there are also events on the 10th and the 11th that are NOT highlighted. What am I missing here? If anyone has any suggestions I would really appreciate it. Note: $day_num is defined earlier in the script and is working correctly.. so, there is either something wrong with the query to select the events, or my if statement to display dates with events is not correct. Currently it is the else if ($day_num == $today) portion. Thanks, wikedawsum Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.