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.
- 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>
- Now let us run the controller in the browser. For it open the browser and write the following address in its address bar:
- The output is shown below:
http://localhost/CodeIgniter/index.php/calendar_demo
- 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);
<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>
- 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>'; } } ?>
echo '<p style="margin-left:100px;">'.$this->calendar->generate(2015,8,$data).'</p>';
- 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:
- 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:
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. |
<?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>'; } } ?>
$pref=array('month_type'=>'short', 'day_type'=>'short', 'start_day'=>'monday');
$this->load->library('calendar',$pref);
- 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}"><<</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}">>></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} {/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); } } ?>
$this->load->library('calendar',$pref);
$data=array('year'=>$this->uri->segment(3), 'month'=>$this->uri->segment(4));
$this->load->view('show_calendar',$data);
<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>
<p class="shift"><?php echo $this->calendar->generate($year,$month);?></p>
http://localhost/CodeIgniter/index.php/calendar_demo/index/2015/03
Thus we studied how to generate and display calendar using calendar class in codeigniter in this Calendar in CodeIgniter tutorial.