Tuesday, March 22, 2016

Creating a Calendar using Codeigniter - Tutorial 1

Initializing the Class

Like most other classes in CodeIgniter, the Calendar class is initialized in your controller using the $this->load->library function:


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

Once loaded, the Calendar object will be available using:


$this->calendar


Displaying a Calendar


Here is a very simple example showing how you can display a calendar:


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

echo $this->calendar->generate();
The above code will generate a calendar for the current month/year based on your server time. To show a calendar for a specific month and year you will pass this information to the calendar generating function:

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

echo $this->calendar->generate(2016, 3);

The above code will generate a calendar showing the month of march in 2016. The first parameter specifies the year, the second parameter specifies the month.


Passing Data to your Calendar Cells


To add data to your calendar cells involves creating an associative array in which the keys correspond to the days you wish to populate and the array value contains the data. The array is passed to the third parameter of the calendar generating function. Consider this example:


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


$data = array(

        3  => 'http://example.com/news/article/2016/03/03/',
        7  => 'http://example.com/news/article/2016/03/07/',
        13 => 'http://example.com/news/article/2016/03/13/',
        26 => 'http://example.com/news/article/2016/03/26/'
);

echo $this->calendar->generate(2016, 3, $data);


Using the above example, day numbers 3, 7, 13, and 26 will become links pointing to the URLs you’ve provided.

No comments:

Post a Comment