Jump to content

PHP Calendar is working now, but will not do previous/next month correctly


dmirsch

Recommended Posts

In a previous posting I was having a problem with a calendar filling in with events. That is now working. However, I cannot get the Previous/Next month selections to work correctly. Here is the coding for that portion of the calendar:

/* date settings */
$month = (int) ($_POST['month'] ? $_POST['month'] 
                                : ($_GET['month'] ? $_GET['month']  
                                                  : date('m'))); 

$year= (int) ($_POST['year'] ? $_POST['year'] 
                               : ($_GET['year'] ? $_GET['year']  
                                                  : date('Y'))); 

/* "previous month" control */
$previous_month_link = '<a href="?month='.($month != 1 ? $month - 1 : 12).'&year='.($month != 1 ? $year : $year - 1).'" class="control"><<   Previous Month</a>';

/* select month control */
$select_month_control = '<select name="month" id="month">';
for($x = 1; $x <= 12; $x++) {
  $select_month_control.= '<option value="'.$x.'"'.($x != $month ? '' : ' selected="selected"').'>'.date('F',mktime(0,0,0,$x,1,$year)).'</option>';
}
$select_month_control.= '</select>'."\n";

/* select year control */
$year_range = 4;
$select_year_control = '<select name="year" id="year">';
for($x = ($year-floor($year_range/2)); $x <= ($year+floor($year_range/2)); $x++) {
  $select_year_control.= '<option value="'.$x.'"'.($x != $year ? '' : ' selected="selected"').'>'.$x.'</option>';
}
$select_year_control.= '</select>'."\n";

/* "next month" control */
$next_month_link = '<a href="?month='.($month != 12 ? $month + 1 : 1).'&year='.($month != 12 ? $year : $year + 1).'" class="control">Next Month >></a>';

/* bringing the controls together */
$controls = '<form method="get">'.$previous_month_link.' '.$select_month_control.$select_year_control.' <input type="submit" name="submit" value="Go" /> '.$next_month_link.' </form>';

//CODING REMOVED
echo draw_calendar($month,$year,$events);

Here is the SQL query if that helps:

SELECT Events.EventTitle, DATE_FORMAT(Performance.startDateTime, '%Y-%m-%e') AS event_date,
DATE_FORMAT(Performance.startDateTime, '%h:%i %p') AS start_time, Events.EventID, Events.ShoWareEventLink, Events.group_id, Performance.category_id
FROM Events LEFT JOIN Performance ON Events.EventID = Performance.EventID WHERE Events.group_id=1 AND Performance.category_id!=5 AND Performance.category_id!=7
AND Performance.category_id!=8 AND  DATE_FORMAT(Performance.startDateTime,'%Y')=$year AND DATE_FORMAT(Performance.startDateTime,'%m')=$month

 

What currently happens with my calendar is that it will list events for THIS month and the next three months. After that it hits next year (January 2012) and the calendar goes blank. It will also go blank if you choose Previous month (even though there are events that happened in September 2011 and before. You can see this if you go to http://www.myalaskacenter.com/calendars/calendarSample3.php.

 

Does anyone know what should be done to the code to fix this?

Link to comment
Share on other sites

It appears that the ARRAY is working, it's just not plugging into the calendar grid. Here's the code where it is supposed to display items in the calendar grid:

  /* keep going with days.... */
  for($list_day = 1; $list_day <= $days_in_month; $list_day++){
    $calendar.= '<td class="calendar-day" valign="top"><div style="position:relative;height:auto;min-height:80px;">';
      /* add in the day number */
      $calendar.= '<div class="day-number">'.$list_day.'</div><br />';
      
      $event_day = $year.'-'.$month.'-'.$list_day;
      if(isset($events[$event_day])) {
        foreach($events[$event_day] as $event) {
          if($event['category_id'] !=2){ 
		  $calendar.= '<div class="events">♦ <a href="' . $event['ShoWareEventLink'] . '">'.$event['start_time'].' - '.$event['EventTitle'].'</a></div>';
		//^this is where we put the performance info
  }
        }
      } else {
        $calendar.= str_repeat('<p> </p>',2);
		//^this is where we have blanks if an event_day <> list_day
      }

 

To see the fact that the ARRAY is working, check out http://www.myalaskacenter.com/calendars/calendarBroken.php?month=1&year=2012. As you can see by the URL it is putting the variables into the URL correctly, but the code that is supposed to complete the calendar grid is apparently not reading the variables.

 

Does anyone have any ideas?

Link to comment
Share on other sites

Aww, I just wrote out a post to point you in the right direction. I knew what the issue was as soon as I saw the page with the array displayed.

 

As for

My problem was PHP date formatting is different from MySQL date formatting

 

Just to clarify, it wasn't that the date format in PHP was different - it was that you built it to be different. But, I'm happy to see you actually worked on the problem and found the solution yourself. Far too many people come here without putting in an ounce of effort.

 

EDIT: By the way, echoing the array to the page was a good debugging step. The next step should have been to look to the if() and foreach() statements that echo's the events to the page.

      if(isset($events[$event_day])) {
        foreach($events[$event_day] as $event) {

 

I would have done something as follows

      if(isset($events[$event_day])) {
        #### DEBUG LINE ####
        $calendar.= "events[$event_day] IS set. Count: " . count($events[$event_day]) . "<br>\n";
        foreach($events[$event_day] as $event) {
          if($event['category_id'] !=2){ 
		  $calendar.= '<div class="events">♦ <a href="' . $event['ShoWareEventLink'] . '">'.$event['start_time'].' - '.$event['EventTitle'].'</a></div>';
		//^this is where we put the performance info
  }
        }
      } else {
        #### DEBUG LINE ####
        $calendar.= "events[$event_day] NOT set<br>\n";
        $calendar.= str_repeat('<p> </p>',2);
		//^this is where we have blanks if an event_day <> list_day
      }

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.