CodeIgniter日历每天添加多个事件?

时间:2015-12-01 09:30:29

标签: php codeigniter codeigniter-3

我正在使用CodeIgniter日历库创建日历视图。 我每天都有多个活动,但日历每天只显示一个活动。

如果当天有多个活动,则仅显示最后一个活动。

如果有人可以帮我修改下面的编码,我们将不胜感激。

祝你好运

控制器

function calendar($year=null, $month=null)
{
    $calendar = $this->timesheet_model->get_calendar($year, $month);

    $data = array('title'       => 'Calendar | Onsys',
              'main_content'    => 'view_timesheet_calendar',
              'calendar'    => $calendar
        );
    $this->load->view('template', $data);
}

模型(没有日历模板)

    function get_calendar($year, $month)
{
    $calendar_settings = array( 'start_day'     => 'monday',
                                'show_next_prev'    => true,
                                'next_prev_url' => base_url('timesheet/calendar')  );

    $calendar_settings['template'] = '';

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

    $this->load->library('calendar', $calendar_settings);

    return $this->calendar->generate($year, $month, $events);
}




function get_tasks($year, $month)
{
$events = array();

$this->db->select('projecttasksid,taskname,taskstartdate')->from('projecttasks')->like('taskstartdate', "$year-$month");
$sql_stmt = $this->db->get();
$sql_stmt = $sql_stmt->result();

foreach( $sql_stmt as $row )
{
    $day = (int)substr($row->taskstartdate, 8,2);
    $events[$day] = $row->taskname;
}

return $events;
}

2 个答案:

答案 0 :(得分:0)

  1. 在扩展标准日历库的库中创建MY_Calendar.php
  2. 从原始库添加generate()方法并替换第273行 $out .= str_replace(array('{content}', '{day}'), array($data[$day], $day), $temp);
  3. 使用:

    if (isset($data[$day]))
    {
        // Cells with content
        $temp = ($is_current_month === TRUE && $day == $cur_day) ?
        $this->temp['cal_cell_content_today'] : $this->temp['cal_cell_content'];
        if(is_array($data[$day]))
        {
            foreach ($data[$day] as $item_key => $item_value) {
                $addition .= '<a href="'.$item_value.'">'.$item_key.'</a>';
            }
            $out .= str_replace(array('<a href="{content}">{day}</a>'), $addition, $temp);  
        }
        else
            $out .= str_replace(array('{content}', '{day}'), array($data[$day], $day), $temp);
    }
    

    if (isset($data[$day])) { // Cells with content $temp = ($is_current_month === TRUE && $day == $cur_day) ? $this->temp['cal_cell_content_today'] : $this->temp['cal_cell_content']; if(is_array($data[$day])) { foreach ($data[$day] as $item_key => $item_value) { $addition .= '<a href="'.$item_value.'">'.$item_key.'</a>'; } $out .= str_replace(array('<a href="{content}">{day}</a>'), $addition, $temp); } else $out .= str_replace(array('{content}', '{day}'), array($data[$day], $day), $temp); } 3.现在您可以使用嵌套数组传递数据,日历将为您添加的每个项目添加链接;

    例如:

    $data = array(
            3  => 'http://example.com/news/article/2006/03/',
            7  => 'http://example.com/news/article/2006/07/',
            13 => 'http://example.com/news/article/2006/13/',
            26 => array(
                'http://example.com/news/article/1/26/',
                'http://example.com/news/article/2/26/'
                'http://example.com/news/article/3/26/'
                )
    );
    

答案 1 :(得分:0)

修改日历库的另一种方法是..

从system / libraries / calendar.php编辑此行&#39;第#301行&#39;

// Cells with no content
                    $temp = ($is_current_month === TRUE && $day == $cur_day) ?
                            $this->replacements['cal_cell_no_content_today'] : $this->replacements['cal_cell_no_content'];
                    $out .= str_replace('{day}', $day, $temp);

$temp = ($is_current_month === TRUE && $day == $cur_day) ?
                            $this->replacements['cal_cell_content_today'] : $this->replacements['cal_cell_content'];

                    if (count($data[$day]) > 1) {//edited open
                        $addition ='';
                        foreach ($data[$day] as $item_key => $item_value) {
                            $addition .= '<a href="'.site_url('events/info/'.$item_value['id']).'">'.$item_value['content'].'</a><br>';                             
                        }
                        $out .= str_replace(array('{content}', '{day}'), array($addition, $day), $temp);

                    }else{
                        $event_url = '<a href="'.site_url('events/info/'.$data[$day][0]['id']).'">'.$data[$day][0]['content'].'</a>';
                        $out .= str_replace(array('{content}', '{day}'), array($event_url, $day), $temp);
                    }

以下更改将使用锚点包装数据。我是出于特定原因这样做的。

然后像这样创建你的数据

    $cal_data = array();

    $dateData = array(
            'year' => $year,
            'month' => $month
        );

    $result = $this->model_events->getDate($dateData);

    if ($result !== NULL) {
        foreach ($result as $key => $value) {
            $name = $value->cus_fn.' '.$value->cus_mn.' '.$value->cus_ln;
            $day = substr($value->sched_date, strlen($value->sched_date) - 2, 2);

            if (substr($day, 0, 1) == '0') $day = substr($day, 1, 1);

            if(isset($cal_data[$day])){
                array_push($cal_data[$day], array('id'=>$value->reservation_id, 'content'=>$name));
            }else{

                $cal_data[$day] = array(array(
                        'id' => $value->reservation_id,
                        'content' => $name
                    ));
            }

        }
    }

$ data [&#39; cal&#39;] = $ this-&gt; calendar-&gt; generate($ year,$ month,$ cal_data);

就是这样。