2
votes

Hi guys thanks for taking the time to read my question.

I am trying to create an Event Calendar where user can input data into the calendar.

How do I show two months calendar on the same page (e.g. show December 2012 and January 2013 side by side rather than through a link.)

I have gotten my codes from http://net.tutsplus.com/tutorials/php/codeigniter-from-scratch-the-calendar-library/

Any help is very much appreciated. =)

//CONTROLLER

class Mycal extends CI_Controller {

public function index() {
    $this->display();
}

function display($year = null, $month = null){

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

    $this->load->model('Mycal_model');

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

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

    $this->load->view('mycal_view', $data); 
}

}

//MODEL

class Mycal_model extends CI_Model{

function get_calendar_data($year, $month){

    $query = $this->db->select('date, data')->from('calendar')
        ->like('date', "$year-$month", 'after')->get();

    $cal_data = array();

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

    return $cal_data;
}

function add_calendar_data($date, $data){

    if($this->db->select('date')->from('calendar')
        ->where('date', $date)->count_all_results()) {

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

    }else{

        $this->db->insert('calendar', array(
            'date'=>$date,
            'data'=>$data
        ));
    }
}

function generate ($year, $month){

    $this->load->library('calendar', $this->conf);
    $cal_data = $this->get_calendar_data($year, $month);    
    return $this->calendar->generate($year, $month, $cal_data)      
}

function generate ($year, $month){
// to generate 2nd calendar for the next month?     
    $this->load->library('calendar', $this->conf);
    $cal_data = $this->get_calendar_data($year, $month+1);  
    return $this->calendar->generate($year, $month+1, $cal_data)        
}

//VIEW

<?php echo $calendar; ?>

// to generate 2nd calendar for the next month? 
<?php echo $calendar2; ?>

<script type="text/javascript">
$(document).ready(function() {
    $('.calendar .day').click(function() {
        day_num = $(this).find('.day_num').html();
        day_data = prompt('Enter Stuff', $(this).find('.content').html());

        if(day_data !=null){

            $.ajax({
                url: window.location,
                type: 'POST',
                data: {
                    day: day_num,
                    data: day_data
                },
                success: function(msg){
                    location.reload();
                }
            });

        }

    });
});
</script>
1
Edit your question to include your code and you will get an answerSwiftD
Hi thanks webweaverD! I have just uploaded my codes.Lynnie

1 Answers

1
votes

I havent used the ci calendar before, but it should be pretty straight forward to get two months to a page.

You will need to create a two page layout for your calandar, something like:

VIEW two_page_calendar.php

<div id="month1">
<?=$month1; ?>
</div>

<div id="month2">
<?=$month2; ?>
</div>

I would then alter your display function to return the display as data. e.g.

function build_display($year = null, $month = null){

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

    $this->load->model('Mycal_model');

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

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

    $calendar_view = $this->load->view('mycal_view', $data, true);
    return $calendar_view;
}

Now you can call build display for each month and load it to your combined view:

public function index() {
    $month2_year = date("Y",strtotime("+1 month"));
    $month2_month = date("m",strtotime("+1 month"));
    $cal_data['month1'] = $this->build_display();
    $cal_data['month2'] = $this->build_display($month2_year, $month2_month);
    $this->load->view('two_page_calendar', $cal_data);
}

I haven't tried this but it should work, you will have to alter some methods to get them to work properly, e.g calendar pagination.

NOTE You should never duplicate code like that in a model, and you certainly cant have two methods with the same name in any class (delete duplicate generate method), the whole purpose is having year and month arguments passed to that method is so that it can be reused for any year/month