2011-07-20 178 views
0

我有一個關於Calendar類的問題。我可以使用數據庫中的數據填充日曆中的日期,當網址中包含年份和月份時沒有問題,但是當您首次使用日曆加載頁面時,它沒有url中的年份和月份,所以我我不知道如何計算出年和月。笨日曆問題

我怎麼能填充在頁面加載日曆時,有沒有年份和月份在url中的事件?

控制器

/** 
    * memberCalendar - displays the member calendar page 
    */ 
    function memberCalendar() 
    { 
     $prefs = array (
       'start_day'  => 'sunday', 
       'month_type'  => 'long', 
       'day_type'  => 'short', 
       'show_next_prev' => TRUE, 
       'next_prev_url' => site_url('member/memberCalendar') 
      ); 

     $year = $this->uri->segment(3); 
     $month = $this->uri->segment(4); 

     $this->load->library('calendar', $prefs); 
     $this->load->model('member_model'); 

     $events = $this->member_model->get_member_events($year, $month); 

     $calendarEvents = array(); 

     foreach($events as $event) { 

      $day = substr($event->event_date, 8, 2); // get the day 

      // if the first digit is a 0, such as in 03, remove the 0 so it becomes 3 
      if(substr($day, 0, 1) == 0) { 
       $day = substr($day, 1, 1); 
      } 

      $calendarEvents[$day] = 'http://example.com/news/article/'.$event->event_title; 
     } 

     $content_data['calendar'] = $this->calendar->generate($this->uri->segment(3), $this->uri->segment(4), $calendarEvents); 
     $content_data['firstName'] = $this->session->userdata('firstName'); 
     $data['sess'] = $this->session; 
     $data['content'] = $this->load->view('member/memberCalendar', $content_data, true); 
     $this->load->view('template/admin', $data); 
    } 

型號

/* 
    * function which gets the member calendar events 
    */ 
    function get_member_events($year, $month) 
    { 
     $query = $this->db->select('event_date, event_title, event_content')->from('member_calendar')->like('event_date', "$year-$month", 'after')->get(); 

     // if there is more than one row 
     if($query->num_rows > 0) 
     { 
      return $query->result(); 
     } else { 
      return false; 
     } 
    } 

回答

2

我將當前的年份和月份爲默認THI NK你會更好使用的參數和默認參數:

public function memberCalendar($year = FALSE, $month = FALSE) 
{ 
    if($year === FALSE) $year = date('Y'); 
    if($month === FALSE) $month = date('m'); 

    // continue as normal until calendar->generate 
    $content_data['calendar'] = 
        $this->calendar->generate($year, $month, $calendarEvents); 

    // continue as normal 
} 
0

就做這樣的事情

if ($this->uri->total_segments() > 2) 
{ 
     $year = $this->uri->segment(3); 
     $month = $this->uri->segment(4); 
} 
else 
{ 
     $year = date('Y'); 
     $month = date('m'); 
} 

當您查看日曆