2015-12-19 52 views
1

我目前工作的CodeIgniter 3日曆圖書館,在那裏我可以在一個月笨日曆如何讓每天多次事件

每天添加新的信息行數據庫如果每天有插入2行我應該能夠在當天看到兩個日曆事件。

2015-12-25具有如圖所示的行。

我希望能夠展示,而不是僅僅展示一個活動,而是要展示那天發生的事情。

問:我怎麼能拿get_calendar_data功能能夠得到那些日曆的那一天行。目前在2015年12月25日它只顯示一行。

enter image description here

enter image description here

獲取數據功能

public function get_calendar_data($year, $month) { 

    $cell_data = array(); 

    $query = $this->db->get('calendar'); 

    if ($query->num_rows() > 0) { 

     $row = $query->row(); 

     $this->db->select('*'); 
     $this->db->from('calendar'); 
     $this->db->where('year', $year); 
     $this->db->where('month', $month); 
     $this->db->where('day', $row->day); 


     $query = $this->db->get(); 

     foreach ($query->result() as $result) { 
      $cell_data[$result->day] = $result->data; 
     } 

    return $cell_data; 

    } 
} 

控制器

<?php 

class Calendar extends MX_Controller { 

    public function __construct() { 
     parent::__construct(); 
     $this->load->model('dashboard/model_calendar'); 
     $this->load->library('calendar'); 
    } 

    public function index() { 

     if ($this->uri->segment(3) == FALSE) { 
      $year = date('Y'); 
     } else { 
      $year = $this->uri->segment(3); 
     } 

     $data['year'] = $year; 

     if ($this->uri->segment(4) == FALSE) { 
      $month = date('m'); 
     } else { 
      $month = $this->uri->segment(4); 
     } 

     $data['month'] = $month; 



     $prefs = array(
      'start_day' => 'monday', 
      'show_next_prev' => true, 
      'day_type' => 'long', 
      'next_prev_url' => base_url('dashboard/calendar') 
     ); 

     $prefs['template'] = ' 
      {table_open}<div class="table-responsive"><table border="0" cellpadding="0" cellspacing="0" class="table table-striped table-bordered calendar">{/table_open} 

      {heading_row_start}<tr>{/heading_row_start} 

      {heading_previous_cell}<th><a href="{previous_url}"><i class="fa fa-chevron-left fa-2x "></i></a></th>{/heading_previous_cell} 
      {heading_title_cell}<th class="text-center" colspan="{colspan}">{heading}</th>{/heading_title_cell} 
      {heading_next_cell}<th class="text-right"><a href="{next_url}"><i class="fa fa-chevron-right fa-2x"></i></a></th>{/heading_next_cell} 

      {heading_row_end}</tr>{/heading_row_end} 

      {week_row_start}<tr>{/week_row_start} 
      {week_day_cell}<td>{week_day}</td>{/week_day_cell} 
      {week_row_end}</tr>{/week_row_end} 

      {cal_row_start}<tr class="days">{/cal_row_start} 
      {cal_cell_start}<td class="day">{/cal_cell_start} 

      {cal_cell_content} 
       <div class="day_number">{day}</div> 
       <div class="content" style="margin-top: 10px;">{content}</div> 
      {/cal_cell_content} 
      {cal_cell_content_today} 
       <div class="day_number highlight">{day}</div> 
       <div class="content" style="margin-top: 10px;">{content}</div> 
      {/cal_cell_content_today} 

      {cal_cell_no_content} 
      <div class="day_number">{day}</div> 
      {/cal_cell_no_content} 
      {cal_cell_no_content_today} 
      <div class="day_number highlight">{day}</div> 
      {/cal_cell_no_content_today} 
      {cal_cell_blank}&nbsp;{/cal_cell_blank} 

      {cal_cell_end}</td>{/cal_cell_end} 
      {cal_row_end}</tr>{/cal_row_end} 

      {table_close}</table></div>{/table_close} 
     '; 

     $this->calendar->initialize($prefs); 

     $this->delete_calendar_event(); 

     if ($this->input->post('day')) { 

      //if ($this->check_calendar_event($year, $month, $this->input->post('day'))) { 
      // $this->update_calendar_event($year, $month); 
      //} else { 
       $this->add_calendar_event($year, $month); 
      //} 

     } 

     $data = $this->get_calendar_data($year, $month); 

     $data['calendar'] = $this->calendar->generate($year, $month, $data); 

     if ($this->uri->segment(3) == TRUE) { 
      $data['view_more'] = site_url('report/events/'. $year .'/'. $month); 
     } else { 
      $data['view_more'] = site_url('report/events'); 
     } 

     $date = date('Y-m-d', mktime(0, 0, 0, date('m'), date('d') + 3, date('Y'))); 

     //echo $date; 

     // echo '<br/>'; 

     //echo $this->test(); 

     $this->load->view('dashboard/calender_view', $data); 
    } 

    public function test() { 
     $date = date('Y-m-d', mktime(0, 0, 0, date('m'), date('d') + 3, date('Y'))); 
     $this->db->where('date <', $date); 
     $query = $this->db->get('calendar'); 
     return $query->num_rows(); 
    } 

    public function add_calendar_event($year, $month) { 
     $date = $year .'-'. $month .'-'. $this->input->post('day'); 

     $calendar = array(
      'year' => $year, 
      'month' => $month, 
      'day' => $this->input->post('day', TRUE), 
      'date' => $date, 
      'data' => $this->input->post('event_data') 
     ); 

     $this->db->insert('calendar', $calendar); 
    } 

    public function update_calendar_event($year, $month) { 
     $date = $year .'-'. $month .'-'. $this->input->post('day'); 

     $calendar = array(
      'year' => $year, 
      'month' => $month, 
      'day' => $this->input->post('day', TRUE), 
      'data' => $this->input->post('event_data') 
     ); 

     $this->db->where('date', $date); 
     $this->db->update('calendar', $calendar); 
    } 

    public function delete_calendar_event() { 
     $this->db->where("date <", date('Y-m-d')); 
     $this->db->or_where('data', ''); 
     $this->db->delete('calendar'); 
    } 

    public function get_calendar_data($year, $month) { 

     $cell_data = array(); 

     $query = $this->db->get('calendar'); 

     if ($query->num_rows() > 0) { 

      $row = $query->row(); 

      $this->db->select('*'); 
      $this->db->from('calendar'); 
      $this->db->where('year', $year); 
      $this->db->where('month', $month); 
      $this->db->where('day', $row->day); 


      $query = $this->db->get(); 

      foreach ($query->result() as $result) { 
       $cell_data[$result->day] = $result->data; 
      } 

     return $cell_data; 

     } 


    } 

    public function check_calendar_event($year, $month, $day) { 
     $date = $year .'-'. $month .'-'. $day; 
     $this->db->select('year, month, day'); 
     $this->db->from('calendar'); 
     $this->db->where('date', $date); 
     $results = $this->db->count_all_results(); 

     return $results; 
    } 

    public function check_calendar_event_date() { 

    } 
} 
+0

我認爲這是問前:檢查:http://stackoverflow.com/questions/30977437/how-to-modify-codeigniter-calendar-to-handle-multiple-events-per-day或本:http://stackoverflow.com/questions/34016931/codeigniter-calendar-to-add-multiple-events-per-day – Vickel

+0

@Vickel沒有幫助我使用的代碼。 – user4419336

+0

@Vickel我能解決它,如我的回答 – user4419336

回答

3

解決了!

經過了很長時間的搜索網絡和嘗試新的東西,我發現這個代碼函數array_key_exists下面讓我得到每一天的多個事件。在圖像工作此處顯示

public function get_events($year, $month) { 
    $calendar = array(); 

    $this->db->where('year', $year); 
    $this->db->where('month', $month); 
    $query = $this->db->get('calendar'); 

    $results = $query->result_array(); 

    foreach ($results as $event) { 

     if (array_key_exists($event['day'], $calendar)) { 

      $calendar[$event['day']] = $calendar[$event['day']] .'<ul class="list-unstyled"><li>'. anchor(base_url() .'report/events/'. $year .'/'. $month .'/'. $event['calendar_id'], $event['data']) . '</li></ul>'; 

     } else { 

      $calendar[$event['day']] = '<ul class="list-unstyled"><li>' . anchor(base_url() .'report/events/'. $year .'/'. $month .'/'. $event['calendar_id'], $event['data']) . '</li></ul>'; 
     } 
    } 

    return $calendar; 
} 

灰分:

enter image description here

控制器

<?php 

class Calendar extends MX_Controller { 

    public function __construct() { 
     parent::__construct(); 
     $this->load->model('dashboard/model_calendar'); 
     $this->load->library('calendar'); 
    } 

    public function index() { 

     if ($this->uri->segment(3) == FALSE) { 
      $year = date('Y'); 
     } else { 
      $year = $this->uri->segment(3); 
     } 

     $data['year'] = $year; 

     if ($this->uri->segment(4) == FALSE) { 
      $month = date('m'); 
     } else { 
      $month = $this->uri->segment(4); 
     } 

     $data['month'] = $month; 

     $prefs = array(
      'start_day' => 'monday', 
      'show_next_prev' => true, 
      'day_type' => 'long', 
      'next_prev_url' => base_url('dashboard/calendar') 
     ); 

     $prefs['template'] = ' 
      {table_open}<div class="table-responsive"><table border="0" cellpadding="0" cellspacing="0" class="table table-striped table-bordered calendar">{/table_open} 

      {heading_row_start}<tr>{/heading_row_start} 

      {heading_previous_cell}<th><a href="{previous_url}"><i class="fa fa-chevron-left fa-2x "></i></a></th>{/heading_previous_cell} 
      {heading_title_cell}<th class="text-center" colspan="{colspan}">{heading}</th>{/heading_title_cell} 
      {heading_next_cell}<th class="text-right"><a href="{next_url}"><i class="fa fa-chevron-right fa-2x"></i></a></th>{/heading_next_cell} 

      {heading_row_end}</tr>{/heading_row_end} 

      {week_row_start}<tr>{/week_row_start} 
      {week_day_cell}<td>{week_day}</td>{/week_day_cell} 
      {week_row_end}</tr>{/week_row_end} 

      {cal_row_start}<tr class="days">{/cal_row_start} 
      {cal_cell_start}<td class="day">{/cal_cell_start} 

      {cal_cell_content} 
       <div class="day_number">{day}</div> 
       <div class="content" style="margin-top: 10px;">{content}</div> 
      {/cal_cell_content} 
      {cal_cell_content_today} 
       <div class="day_number highlight">{day}</div> 
       <div class="content" style="margin-top: 10px;">{content}</div> 
      {/cal_cell_content_today} 

      {cal_cell_no_content} 
      <div class="day_number">{day}</div> 
      {/cal_cell_no_content} 
      {cal_cell_no_content_today} 
      <div class="day_number highlight">{day}</div> 
      {/cal_cell_no_content_today} 
      {cal_cell_blank}&nbsp;{/cal_cell_blank} 

      {cal_cell_end}</td>{/cal_cell_end} 
      {cal_row_end}</tr>{/cal_row_end} 

      {table_close}</table></div>{/table_close} 
     '; 

     $this->calendar->initialize($prefs); 

     $this->model_calendar->delete_calendar_event($year, $month); 

     if ($this->input->post('day')) { 

      if ($this->model_calendar->check_calendar_event($year, $month, $this->input->post('day'))) { 
       $this->model_calendar->update_calendar_event($year, $month); 
      } else { 
       $this->vadd_calendar_event($year, $month); 
      } 

     } 

     $events = $this->model_calendar->get_events($year, $month); 

     $data['calendar'] = $this->calendar->generate($year, $month, $events); 

     if ($this->uri->segment(3) == TRUE) { 
      $data['view_more'] = site_url('report/events/'. $year .'/'. $month); 
     } else { 
      $data['view_more'] = site_url('report/events'); 
     } 

     $this->load->view('dashboard/calender_view', $data); 
    } 

} 

型號

<?php 

class Model_calendar extends CI_Model { 

    public function test() { 
     $date = date('Y-m-d', mktime(0, 0, 0, date('m'), date('d') + 3, date('Y'))); 
     $this->db->where('date <', $date); 
     $query = $this->db->get('calendar'); 
     return $query->num_rows(); 
    } 

    public function add_calendar_event($year, $month) { 
     $date = $year .'-'. $month .'-'. $this->input->post('day'); 

     $calendar = array(
      'year' => $year, 
      'month' => $month, 
      'day' => $this->input->post('day', TRUE), 
      'date' => $date, 
      'data' => $this->input->post('event_data') 
     ); 

     $this->db->insert('calendar', $calendar); 
    } 

    public function update_calendar_event($year, $month) { 
     $date = $year .'-'. $month .'-'. $this->input->post('day'); 

     $calendar = array(
      'year' => $year, 
      'month' => $month, 
      'day' => $this->input->post('day', TRUE), 
      'data' => $this->input->post('event_data') 
     ); 

     $this->db->where('date', $date); 
     $this->db->update('calendar', $calendar); 
    } 

    public function delete_calendar_event() { 
     $this->db->where("date <", date('Y-m-d')); 
     $this->db->or_where('data', ''); 
     $this->db->delete('calendar'); 
    } 



    public function get_events($year, $month) { 
     $calendar = array(); 

     $this->db->where('year', $year); 
     $this->db->where('month', $month); 
     $query = $this->db->get('calendar'); 

     $results = $query->result_array(); 

     foreach ($results as $event) { 

      if (array_key_exists($event['day'], $calendar)) { 

       $calendar[$event['day']] = $calendar[$event['day']] .'<ul class="list-unstyled"><li>'. anchor(base_url() .'report/events/'. $year .'/'. $month .'/'. $event['calendar_id'], $event['data']) . '</li></ul>'; 

      } else { 

       $calendar[$event['day']] = '<ul class="list-unstyled"><li>' . anchor(base_url() .'report/events/'. $year .'/'. $month .'/'. $event['calendar_id'], $event['data']) . '</li></ul>'; 
      } 
     } 

     return $calendar; 
    } 

    public function check_calendar_event($year, $month, $day) { 
     $date = $year .'-'. $month .'-'. $day; 
     $this->db->select('year, month, day'); 
     $this->db->from('calendar'); 
     $this->db->where('date', $date); 
     $results = $this->db->count_all_results(); 

     return $results; 
    } 

    public function check_calendar_event_date() { 

    } 

} 
+0

嘿@ wolfgang1983你可以提供這個源代碼... –

0

這是一個偉大的答案,我はs試圖修改codeigniter日曆製作MY_calendar,但這是好得多。 我還沒有添加鏈接到事件。

public function get_datos_calendario($year, $month) 
    { 

    $this->db->like('fecha', "$year-$month", 'after'); 
    $query = $this->db->get('tareas'); 

    $datos_calendario = array(); 

    $results = $query->result_array(); 

    foreach ($results as $event) { 
     $day = ltrim(substr($event['fecha'], 8, 2), '0'); 
     if (array_key_exists($day, $datos_calendario)) { 

      $datos_calendario[$day] = $datos_calendario[$day] .'<ul class="list-unstyled"><li>'. $event["descripcion"] . '</li></ul>'; 

     } else { 

      $datos_calendario[$day] = '<ul class="list-unstyled"><li>' . $event["descripcion"] . '</li></ul>'; 
     } 
    }