July 16, 2014

July 16, 2014
In this tutorial, we are going to discuss about How implement the calendar in CodeIgniter (CI). We use dates in many parts of our application. Consider an example where you have to display "Thoughts of the Day" in calendar format. When you click on a date, it takes you to a page where it display a thought. To do this using CodeIgniter is very simple. As I mentioned about libraries and the advantages of using them in my earlier posts, today I would like to explain about "Calendar" library.

Lets see how to use it. In order to use the library we have to first load it. The code to load the library is

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

And displaying the calendar is pretty simple. Just add the following line

echo $this->calendar->generate();

This two lines will display a calendar of the current year and month. If you want to display for a specific year and month, lets say 2010 march, we can do that by passing year and month as parameters to the generate method.

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

If in case, you wish to change the start day to Monday we can do that as well. We can customize it as we want. To make Monday as the first day make the following changes.

$prefs = array (
               'start_day'    => 'monday',
               'month_type'   => 'long',
               'day_type'     => 'short'
             );
$this->load->library('calendar', $prefs);
echo $this->calendar->generate();

If you wish to add next and previous link to the calendar, we can do that by

$prefs = array (
               'show_next_prev'  => TRUE,
               'next_prev_url'   => 'http://www.geeks.gallery/calendar/'
             );
$this->load->library('calendar', $prefs);
echo $this->calendar->generate($this->uri->segment(2), $this->uri->segment(3));

Coming to the important section, linking the dates to separate page, the code is

$data = array(
       3  => 'http://www.geeks.gallery/wordpress-theme-installation/',
       7  => 'http://www.geeks.gallery/how-to-remove-index-php-from-codeigniter-url/',
       13 => 'http://www.geeks.gallery/tabactivity-in-android/',
       26 => 'http://www.geeks.gallery/change-the-favicon-in-your-website/'
     );
echo $this->calendar->generate(date('Y'),date('m'),$data);

0 comments:

Post a Comment