Calendar of events

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
acpjax
Forum Newbie
Posts: 12
Joined: Fri Nov 10, 2006 11:02 am

Calendar of events

Post by acpjax »

First of thankyou for looking at this post. Got a quick question.....
Currently i have been trying to add an event calendar to my website so i can show when certian things are availible. Beyond that i need to make the event calendar unique for each item....

Each item or listing has a unique id $listingid and what i want to do is place a link on each listing that opens a popup that displays the event calendar. the link will pass the value of the $listingid to the pop up event calendar.

What im trying to do is add in another criteria to the basic event calendar tutorial... right now it seems to pull any day that has an event on it. what i need it to do is pull any day that has an event on it AND is = to $listing id...

table has the following records
event_id
event_date
event_avalibility
event_listingid

So i need to dispay any day that has an event AND WHERE event_listingid = $listingid
For the life of me i cant get it to work.. i can either get it to display all events ... or none.. but not just events that match $listingid
Any help would be greatly appreciated

Code: Select all

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>

<body>
<?php 
// Database connection variables 
// Be sure to set these to your own connection 
$HOST = '************'; 
$USER = '**********'; 
$PASS = '**********'; 
$NAME = '*********';  

// Connect to MySQL database 
$con = mysql_connect($HOST, $USER, $PASS); 
if (!$con) { 
  // Since the entire script depends on connection, die if connection fails 
  die("Error connecting to MySQL database!"); 
} 
mysql_select_db($NAME, $con); 

function getEventDays($month, $year) { 
  $days = array(); 
  $sql = mysql_query("SELECT DAY(event_date) AS day, COUNT(event_id) FROM calendar_events WHERE MONTH(event_date) = '$month' AND YEAR(event_date) = '$year' GROUP BY day"); 

  if (mysql_num_rows($sql) > 0) { 
  while ($row = mysql_fetch_array($sql)) $days[] = $row['day']; 
  } 

  return $days; 
} 

function drawCalendar($month, $year) { 
  // set variables we will need to help with layouts 
  $first       = mktime(0,0,0,$month,1,$year); // timestamp for first of the month 
  $offset      = date('w', $first); // what day of the week we start counting on 
  $daysInMonth = date('t', $first); 
  $monthName   = date('F', $first); 
  $weekDays    = array('Su', 'M', 'Tu', 'W', 'Th', 'F', 'Sa'); 
  $eventDays   = getEventDays($month, $year); 
   
  // Start drawing calendar 
  $out  = "<table id=\"myCalendar\" border =\'1'>\n"; 
  $out .= "<tr><th colspan=\"7\">$monthName $year</th></tr>\n"; 
  $out .= "<tr>\n"; 
  foreach ($weekDays as $wd) $out .= "<td class=\"weekDays\">$wd</td>\n"; 
   
  $i = 0; 
  for ($d = (1 - $offset); $d <= $daysInMonth; $d++) { 
    if ($i % 7 == 0) $out .= "<tr>\n"; // Start new row 
    if ($d < 1) $out .= "<td class=\"nonMonthDay\">&nbsp;</td>\n"; 
    else { 
      if (in_array($d, $eventDays)) { 
        $out .= "<td class=\"monthDay\" td bgcolor=\"red\">\n"; 
        $out .= "<a href=\"?year=$year&month=$month&day=$d\">$d</a>\n"; 
        $out .= "</td>\n"; 
      } else $out .= "<td class=\"monthDay\">$d</td>\n"; 
    } 
    ++$i; // Increment position counter 
    if ($i % 7 == 0) $out .= "</tr>\n"; // End row on the 7th day 
  } 
   
  // Round out last row if we don't have a full week 
  if ($i % 7 != 0) { 
    for ($j = 0; $j < (7 - ($i % 7)); $j++) { 
      $out .= "<td class=\"nonMonthDay\">&nbsp;</td>\n"; 
    } 
    $out .= "</tr>\n"; 
  } 
   
  $out .= "</table>\n"; 
     
    return $out; 
} 

$year  = isset($_GET['year']) ? $_GET['year'] : date('Y'); 
$month = isset($_GET['month']) ? $_GET['month'] : date('m'); 

echo drawCalendar($month, $year); 


// Previous month link 
$prevTS = strtotime("$year-$month-01 -1 month"); // timestamp of the first of last month 
$pMax = date('t', $prevTS); 
$pDay = ($day > $pMax) ? $pMax : $day; 
list($y, $m) = explode('-', date('Y-m', $prevTS)); 
echo "<p>\n"; 
echo "<a href=\"?year=$year&month=$m&day=$pDay\">&laquo; Prev</a> |\n"; 

// Next month link 
$nextTS = strtotime("$year-$month-01 +1 month"); 
$nMax = date('t', $nextTS); 
$nDay = ($day > $nMax) ? $nMax : $day; 
list($y, $m) = explode('-', date('Y-m', $nextTS)); 
echo "<a href=\"?year=$y&month=$m&day=$nDay\">Next &raquo;</a>\n"; 
echo "</p>\n"; 


// Calendar is done, let's list events for selected day 
$sql = mysql_query(" 
SELECT * 
FROM calendar_events 
WHERE event_date = '".$year."-".$month."-".$day."' 
AND event_listingid = ".$listingid." 
"); 
if (mysql_num_rows($sql) > 0) { 
  while ($e = mysql_fetch_array($sql)) { 
    echo "<p>$e[event_avalibility]</p>\n"; 
  } 
} else { 
  echo "<p>No events today</p>\n"; 
}
?>
</body>
</html>
Post Reply