Codeigniter& HMVC日历没有插入正确的日期?

时间:2015-12-14 09:07:51

标签: codeigniter codeigniter-3 codeigniter-hmvc

我正在使用CodeIgniter日历class,我可以点击任何一天并输入文字但是当我点击2016年1月的日期时,插入数据就不会输入正确的日期。

它插入2015-12-01并且应该插入日期2016-1-1如果在明年就不能插入日期。

  

问题如果插入数据是在新的一年,那就是没有向数据库插入正确的日期2016年,只会插入年份日期2015我该如何解决这个问题?

注意:当我查看2016年1月的月份时,url说

http://localhost/project/admin/common/dashboard/2016/01

控制器

class Calendar extends MX_Controller {

    public function index() {

        $year = ($this->uri->segment(4)) ? $this->uri->segment(4) : 0;

        if (!$year) {
            $year = date('Y');
        }

        $month = ($this->uri->segment(5)) ? $this->uri->segment(5) : 0;

        if (!$month) {
            $month = date('m');
        }

        $this->load->model('admin/dashboard/model_calendar');

        $this->model_calendar->delete();

        if ($day = $this->input->post('day')) {
            $this->model_calendar->add_calendar_data("$year-$month-$day", $this->input->post('data'));
        }

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

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

}

模型

<?php

class Model_calendar extends CI_Model {

    var $prefs;

    public function __construct() {
        parent::__construct();

        $this->prefs = array(
            'start_day' => 'monday',
            'show_next_prev' => true,
            'next_prev_url' => base_url('admin/common/dashboard')
        );

        $this->prefs['template'] = '
            {table_open}<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_num">{day}<span class="label label-danger" style="margin-left: 10px;">Notice</span></div>
                <div class="content">{content}</div>
            {/cal_cell_content}
            {cal_cell_content_today}
                <div class="day_num highlight">{day}</div>
                <div class="content">{content}</div>
            {/cal_cell_content_today}

            {cal_cell_no_content}<div class="day_num">{day}</div>{/cal_cell_no_content}
            {cal_cell_no_content_today}<div class="day_num highlight">{day} <span class="label label-success">Current Day</span></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>{/table_close}
        ';
    }

    public function add_calendar_data($date, $data) {
        $this->db->select('date');
        $this->db->from($this->db->dbprefix . 'calendar');
        $this->db->where('date', $date);
        $check = $this->db->count_all_results();

        if (empty($data)) {
            $this->db->where('data', $data);
            $this->db->delete($this->db->dbprefix . 'calendar');
        }

        if ($check) {

            $this->db->where('date', $date);
            $this->db->update($this->db->dbprefix . 'calendar', array('date' => $date, 'data' => $data));

        } else {

            $calendar_data = array(
                'date' => $date,
                'data' => $data
            );

            $this->db->insert($this->db->dbprefix . 'calendar', $calendar_data);

        }


    }

    public function delete() {
        $this->db->where('data', " ");
        $this->db->delete($this->db->dbprefix . 'calendar');

    }

    public function get_calendar_data($year, $month) {
        $this->db->select('date, data');
        $this->db->from($this->db->dbprefix . 'calendar');
        $this->db->like('date', "$year-$month", 'after');
        $query = $this->db->get();

        $cel_data = array();

        foreach ($query->result() as $row) {
            $cel_data[substr($row->date, 8,2)] = $row->data;
        }

        return $cel_data;
    }

    public function generate($year, $month) {
        $cel_data = $this->get_calendar_data($year, $month);

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

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

Image December

enter image description here

1 个答案:

答案 0 :(得分:1)

Codeigniter Calendaring Class有一个名为adjust_date($month, $year)的函数,可以确保您有一个有效的月/年。例如,如果您按月提交13,则年份将增加,月份将变为1月。

在您的情况下需要2016-1-1(http://localhost/project/admin/common/dashboard/2016/-1/-1),返回2015年12月1日