8.04.2013

Calendar Library with Ajax

Today, we are going to create a simple calendar application using Codeigniter Framework with Ajax.

 Objective:
 -Generate a calendar using Calendar Library.
 -Make the Calendar works dynamically using AJAX.
 So let's get started.

Here i'm working with Version 2.1.3

1. Base url configuration. file: ci_calendar\application\config\config.php
$config['base_url'] = 'http://localhost/xampp/ci_calendar';
2. Default controller configuration. file: ci_calendar\application\config\routes.php
$route['default_controller'] = "site";
3. Autoload configuration. file: ci_calendar\application\config\autoload.php
$autoload['helper'] = array('url');
4. Controller. Create a file site.php: ci_calendar\application\controllers\site.php
<?php
class Site extends CI_Controller{
 function __construct(){
  parent::__construct();
 }
 
 function index($year = NULL, $month = NULL){
  $this->load->model('calendar_model');
  $data['calendar'] = $this->calendar_model->get_calendar($year,$month);
  $this->load->view('calendar',$data);   
 }
 
 function ajax_calendar($year = NULL , $month = NULL){
  $this->load->model('calendar_model');
  $_html = $this->calendar_model->get_calendar($year,$month);
  echo $_html;
 }
}
5. Model. Create a file calendar_model.php: ci_calendar\application\models\calendar_model.php 
<?php
class Calendar_model extends CI_Model{
 var $config;
 function __construct(){
  parent::__construct();
  $this->config = array(
    'start_day' => 'monday', //you could specify whatever day you'd prefer
    'show_next_prev' => true, //this is to show the Next and Previous buttons which we are going to work with ajax
    'next_prev_url' => base_url() . 'index.php/site/ajax_calendar'
  );
  //here is the template for your calendar
  $this->config['template'] = 
  
 }
 
 function get_calendar($year,$month){
  $this->load->library('calendar',$this->config);
  return $this->calendar->generate($year,$month);
 }
 
 
}
Note: Get the value for the highlighted line section here.
6. View. Create a file calendar.php: ci_calendar\application\views\calendar.php

My Calendar

<?php echo $calendar; ?>
Download CI_Calendar