i'm making a simple calendar for an events guide, which lists 7 days in advance, from the current date, and will eventually have a whole lot of other viewing modes...
each day in the calendar becomes a <tr>, and i want to check each day to see if there are events listed for that day... if there are, the calendar should output a colored box, and a link to the events, if not, output a basic grey box...
i don't want to query the database for each day, looping database queries is messy....
i've setup a query that pulls out an array of events for the entire calendar date range, and then i'm testing each day against the events array, using the in_array function...
appears this isn't working at all... not sure why. array_search isn't supported on my version of php...
if anyone could suggest a better way to do this, or a clean way of finding out if a date exists in a database result array, i'd be extremely greatful...
here is the script (note that $data_pipe->fetch_array() is just an object wrapper for the mysql fetch array func)
// setup vars
$startday = mktime(0,0,0,date('m'),date('d'),date('Y'));
$endday = mktime(0,0,0,date('m'),date('d')+6,date('Y'));
$startymd = date('Y-n-d', $startday);
$endymd = date('Y-n-d', $endday);
echo "<table>\n\n";
echo "<tr>\n\n";
// get events data for calendar range
$data_pipe = new data_pipe();
$sql = "select events.id as event_id,events_dates.date as date from events,events_dates where events.id=events_dates.event_id and events_dates.date>='$startymd' and events_dates.date<='$endymd''";
$result = $data_pipe->query($sql);
$events_data = $data_pipe->fetch_array($result);
for ($currentday = $startday; $currentday <= $endday; $currentday+=86400 )
{
$date = getdate($currentday);
$ymd = date('Y-n-d', $currentday);
printf("<td bgcolor=#ff6600><b class=\"calendar\">%s</b></td>\n\n", $date['weekday']);
if ( in_array($ymd, $events_data) )
// event exists for this day
{
echo "<td bgcolor=\"#ff9900\" valign=\"top\">";
printf("<b class=\"calendar\"><a href=\"output.php?module=events&mode=view&ymd=%s\" target=\"delta\">%s %s</a></b></td>\n\n", $ymd, $date['mday'], $date['month']);
}
else
// no event for this day
{
echo "<td bgcolor=\"#999999\" valign=\"top\">";
printf("<b class=\"calendar\">%s %s</b><input type=\"checkbox\" name=\"date_tag[]\" value=\"%s\"></td>\n\n", $date['mday'], $date['month'], $currentday);
}
echo "</tr>";
}
echo "</table>\n\n";