Codeigniter日历问题

时间:2011-07-20 05:49:35

标签: php codeigniter calendar

我对日历类有疑问。我可以在日历中填充日期,数据库中的数据没有问题,当网址有年份和月份时,但是当您第一次加载带有日历的页面时,它没有年份和月份在网址中所以我我不知道如何弄清楚年月。

如果网址中没有年份和月份,我如何在页面加载时填充日历中的事件?

控制器

    /**
    * 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 个答案:

答案 0 :(得分:2)

我认为你最好使用参数和默认参数:

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
}

答案 1 :(得分:0)

做一些像

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

在您查看日历时将当前年份和月份加载为默认值