Web Programming TutorialsCalendar in CodeIgniter

Calendar in CodeIgniter

Today we will be learning the Calendar class in codeigniter and we will even demonstrate an example which will help us to use it in our codeigniter projects in this Calendar in CodeIgniter tutorial.

  • Calendar class enables us to dynamically create calendars.
  • We can format our calendars according to our requirement using the calendar template and thus get full control over it.
  • We can even pass data to our calendar cells.
  • The Calendar class is located in the system\libraries folder inside your CodeIgniter
  • Let us start learning about it along with demonstration.

 

  • Step 1: Initializing calendar class in your controller:
    • Like most of the classes in codeigniter the Calendar class is also initialized in controller.
    • Syntax for initializing Calendar is shown below:
    • $this->load->library('calendar');
    •  So to initialize it let us create a controller class in the application/controllers folder inside the CodeIgniter
    • So open a new notepad++ file and save it as php in the application/controllers folder.
    • Write the following code inside the calendar_demo.php file:
      <?php
      	class Calendar_demo extends CI_Controller
      	{
      		public function __construct()
      		{
      			parent::__construct();
      		}
      		
      		public function index()
      		{
      			$this->load->library('calendar');
      			
      			$this->load->view('show_calendar');
      		}
      	}
      ?>
      

       

    •   In the above code, we have defined a class Calendar_demo which inherits from CI_Controller
    • A constructor is defined and a method index() is defined.
    • The calendar is initialized in the index() method using the statement
      $this->load->library('calendar');

       

    •   After initialization (loading) the calendar object will be available using $this->calendar.
    • We have displayed the calendar in a view file namedphp.
    • This view file is also loaded in the index() method of controller, so that it is visible to us in browser using statement:
      $this->load->view('show_calendar');

       

    •  The calendar is displayed using the statement:
      $this->calendar->generate();

       

    •  This is done in the view file. So let us see how the view file is coded:
    • The view files are created and stored in the application/views folder inside the CodeIgniter
    • We know the name of our view file is php as specified in the controller program.
  • Step 2: Creating the view file:
    • Let us see how the view file show_calendar.php is created:
    • After creating the controller program, open a new notepad++ file and save it as show_calendar.php in the application/views folder.
    • Write the following code in the show_calendar.php file:
      <html>
      <head>
      <title>Calendar</title>
      <style>
      	.shift{
      		margin-left:120px;
      	}
      </style>
      </head>
      <body>
      <h1>Current month/year calendar</h1>
      
      <p class="shift"><?php echo $this->calendar->generate();?></p>
      </body>
      </html>
      

       

    • Here we have displayed a heading Current month/year calendar and then the calendar using the statement:
    • <p class="shift"><?php echo $this->calendar->generate();?></p>
    • Here we now know that after loading the calendar object is available as $this->calendar, hence in the view file it is used to display the calendar in the statement echo $this->calendar->generate(); This statement displays the current month/year calendar.
    • We have provided some styling to shift the calendar and place it at the centre.
  • Step 3: Run the controller:
    • Now let us run the controller in the browser. For it open the browser and write the following address in its address bar:
    • http://localhost/CodeIgniter/index.php/calendar_demo

    • The output is shown below:
    • calendar_1
      fig 1

  • To generate a specific month/year calendar:
    • To generate a specific month/year calendar, we need to specify the particular year and month in the generate function while displaying the calendar as shown below:
    • Echo $this->calendar->generate(2010,3);
    • The first parameter in the generate function is year and second parameter in is the month to be displayed.
    • The controller program will be same as shown above. Let us see the slight modifications done in view file:
    • The modified view file show_calendar.php code to display a particular month in a particular year is shown below:
    • <html>
      <head>
      <title>Calendar</title>
      <style>
      	.shift{
      		margin-left:120px;
      	}
      </style>
      </head>
      <body>
      <h1> Particular month/year calendar</h1>
      
      <p class="shift"><?php echo $this->calendar->generate(2010,3);?></p>
      </body>
      </html>
      
    • Rest all things are same.
    • Just refresh the browser, you will see the following output:
    • calendar_2
      fig 2

  • Passing data to Calendar Cells:
    • To pass values to calendar cells, we need to create an associative array in which the array keys refer to the days in calendar and values of the keys refer to the data.
    • This array is then passed as the third parameter in the calendar generate() function.
    • Let us see an example:
    • Modify your calendar_demo.php file containing the controller class as shown below:
    • <?php
      	class Calendar_demo extends CI_Controller
      	{
      		public function __construct()
      		{
      			parent::__construct();
      		}
      		
      		public function index()
      		{
      		
      			$this->load->library('calendar');
      			
      			$data=array(
      					15=>'http://loacalhost/CodeIgniter/index.php/calendar_demo/index/2015/15',
      					21=>'http://loacalhost/CodeIgniter/index.php/calendar_demo/index/2015/21');
      				
      			
      			echo '<p style="margin-left:100px;">'.$this->calendar->generate(2015,8,$data).'</p>';			
      	
      		}
      	
      	}
      ?>
      
    • Here, after loading the calendar we have just provided an array named $data. It consists of the day as keys which will be treated as links.
    • Next we have generated the calendar of month of August of year 2015 with the array $data passed as the third parameter as shown in the following statement:
    • echo '<p style="margin-left:100px;">'.$this->calendar->generate(2015,8,$data).'</p>';
    • We have generated the calendar by placing it inside the html paragraph tag, so that we could shift it at the centre of the browser by applying some styling.
    • After refreshing the browser, you will get the following output with days 15 and 21 treated as hyperlinks:
    • calendar_3
      fig 3

  • Setting display preferences to the calendar:
    • There are 7 preferences which will allow us to control various aspects of calendar.
    • These can be set by creating a preference array and assigning it as a parameter to the calendar loading function.
    • The preferences are shown below:
    • Sr.No. Preference Default Value Options Description
      1 template None None A string containing the calendar template. (I mean you can decide the appearance of your calendar by your own by creating a template i.e. format of the calendar)
      2 local_time time() None A Unix timestamp corresponding to the current time.
      3 start_day sunday Any week day (sunday, monday, tuesday, etc.) Sets the day of the week the calendar should start on.
      4 month_type long long, short Determines what version of the month name to use in the header.eg. long = January, short = Jan.
      5 day_type abr long, short, abr Determines what version of the weekday names to use in the column headers.eg. long = Sunday, short = Sun, abr = Su.
      6 show_next_prev FALSE TRUE/FALSE (boolean) Determines whether to display links allowing you to toggle to next/previous months.
      7 next_prev_url None A URL Sets the basepath used in the next/previous calendar links.
    • Let us see an example by displaying a calendar by passing preferences in our controller file calendar_demo.php:
    • The modified code is given below:
    • <?php
      	class Calendar_demo extends CI_Controller
      	{
      		public function __construct()
      		{
      			parent::__construct();
      		}
      		
      		public function index()
      		{
      			$pref=array('month_type'=>'short',
      			            'day_type'=>'short',
      						'start_day'=>'monday');
      			
      			$this->load->library('calendar',$pref);
      			
      			$data=array(
      					15=>'http://loacalhost/CodeIgniter/index.php/calendar_demo/index/2015/15',
      					21=>'http://loacalhost/CodeIgniter/index.php/calendar_demo/index/2015/21');
      		
      			
      			echo '<p style="margin-left:80px;">'.$this->calendar->generate(2015,8,$data).'</p>';
      			
      						
      		}
      	
      	}
      ?>
      
    • Here in the above code we have just added an array named $pref as shown below:
    • $pref=array('month_type'=>'short',
      		 'day_type'=>'short',
      		 'start_day'=>'monday');
    • In the array we specified the month_type, day_type and start_day preferences to short, short and monday respectively.
    • This array is then passed to the calendar load function as a second parameter as shown below:
    • $this->load->library('calendar',$pref);
    • Due to this the specified preferences are then applied to the calendar that is displayed later.
    • The output of calendar is shown below:
    • calendar_4
      fig 4

    • You can compare calendars in fig 3 and fig 4 and conclude how the preferences are changed, taking help of the preference table. (Note that the calendar in fig 3 has default preferences).
  • Creating a calendar template:
    • By creating a calendar template we can have complete control over the design of our calendar.
    • The components of our calendar will be placed within a pair of pseudo-code to create a template.
    • Let us demonstrate it by creating a calendar template:
    • We will create the calendar template in the controller class and send it as parameter to the view file where it will be displayed.
    • For time being we have commented the previous code in the index() function of our controller class.
    • So modify your controller class Calendar_demo written in calendar_demo.php file as shown below:
    • <?php
      	class Calendar_demo extends CI_Controller
      	{
      		public function __construct()
      		{
      			parent::__construct();
      		}
      		
      		public function index()
      		{
      			
      			$pref['template']='
      				{table_open}<table border="1" cellpadding="1" cellspacing="2">{/table_open}
      				
      				{heading_row_start}<tr>{/heading_row_start}
      				{heading_previous_cell}<th class="prevcell"><a href="{previous_url}">&lt;&lt;</a></th>{/heading_previous_cell}
      				{heading_title_cell}<th colspan="{colspan}">{heading}</th>{/heading_title_cell}
      				{heading_next_cell}<th class="nextcell"><a href="{next_url}">&gt;&gt;</a></th>{/heading_next_cell}
      				{heading_row_end}</tr>{/heading_row_end}
      				
      				{week_row_start}<tr class="wk_nm">{/week_row_start}
      				{week_day_cell}<td>{week_day}</td>{/week_day_cell}
      				{week_row_end}</tr>{/week_row_end}
      				
      				
      				{cal_row_start}<tr>{/cal_row_start}
      				{cal_cell_start}<td>{/cal_cell_start}
      				{cal_cell_content}<a href="{content}">{day}</a>{/cal_cell_content}
      				{cal_cell_content_today}<div class="highlight"><a href="{content}">{day}</a></div>{/cal_cell_content_today}
      				
      				{cal_cell_content}{day}{/cal_cell_content}
      				{cal_cell_content_today}<div class="highlight">{day}</div>{/call_cell_content_today}
      				
      				{cal_cell_blank}&nbsp;{/cal_cell_blank}
      				
      				{cal_cell_end}</td>{/cal_cell_end}
      				{cal_row_end}</tr>{/cal_cell_end}
      				
      				{table_close}</table>{/table_close}
      			';
      			
      			$pref['start_day']='monday';
      			$pref['day_type']='short';
      			$pref['show_next_prev']=true;
      			
      		$this->load->library('calendar',$pref);
      			
      			$data=array('year'=>$this->uri->segment(3),
      			            'month'=>$this->uri->segment(4));
      			
      			$this->load->view('show_calendar',$data);
      		}
      	
      	}
      ?>
      
    • Here in the index() function of Calendar_demo class we have defined a template and assigned it to the preference template of $pref array.
    • Next we have also provided preferences like start_day, day_type and show_next_prev in the $pref array.
    • The calendar is then loaded along with the preferences as shown in the statement below:
    • $this->load->library('calendar',$pref);
    • Then a $data array with year and month is defined. The year and month will be provided in the URL while running the page and will be passed while loading the view file which will then display the provided month/year calendar.
    • The $data array is shown below:
    • $data=array('year'=>$this->uri->segment(3),
                     'month'=>$this->uri->segment(4));
    • The year will be fetched from the third segment of the URL and month will be fetched from the fourth segment of URL.
    • After this the view file is loaded as shown below:
    • $this->load->view('show_calendar',$data);
    • The $data array is passed to the view file show_calendar.php.
    • Now let us see the code in view file show_calendar.php :
    • <html>
      <head>
      <title>Calendar with template</title>
      <style>
      	.shift{
      		margin-left:100px;
      	}
      	
      	table{
      		border:15px solid #FD5196;
      		margin-top:20px;
      		
      		
      	}
      	td{
      		text-align:center;
      		border:2px solid #E6C1EB;
      		font-size:18px;
      		font-weight:bold;
      	}
      	th{
      		background:#FD5196;
      		font-size:20px;
      		color:white;
      	}
      	.prevcell a, .nextcell a{
      		color:white;
      		text-decoration:none;
      	}
      	
      	tr.wk_nm{
      		background:#E6C1EB;
      		color:#AB08BD;
      		font-size:17px;
      		font-weight:bold;
      		width:10px;
      		padding:5px;
      	}
      	
      	.highlight{
      		background:#FD5196;
      		color:white;
      		padding:10px;
      	}
      	
      </style>
      </head>
      <body>
      <h1>Particular month/year calendar</h1>
      
      <p class="shift"><?php echo $this->calendar->generate($year,$month);?></p>
      
      </body>
      </html>
      
    • In the code in the view file we have displayed the calendar using the year and month passed in array $data.
    • The calendar is displayed using the following statement:
    • <p class="shift"><?php echo $this->calendar->generate($year,$month);?></p>
    • The year and month key names in $data array are treated as variables here.
    • Some styling is done to the calendar in the head section.
    • Let us see the output of the calendar with template. For that type the following URL in the address bar of the browser:
    • http://localhost/CodeIgniter/index.php/calendar_demo/index/2015/03

    • It will display calendar for March 2015, since it is specified in the URL after the segment for method i.e. index.
    • The output is shown below:
    • calendar_5
      fig 5

Thus we studied how to generate and display calendar using calendar class in codeigniter in this Calendar in CodeIgniter tutorial.

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Exclusive content

- Advertisement -

Latest article

21,501FansLike
4,106FollowersFollow
106,000SubscribersSubscribe

More article

- Advertisement -