Calendar in CodeIgniter

0
21721

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:
    •  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:

       
    •   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

       
    •   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:

       
    •  The calendar is displayed using the statement:

       
    •  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:

       
    • Here we have displayed a heading Current month/year calendar and then the calendar using the statement:
    • 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:
    • 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:
    • 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:
    • 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:
    • 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:
    • Here in the above code we have just added an array named $pref as shown below:
    • 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:
    • 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:
    • 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:
    • 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:
    • 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:
    • The $data array is passed to the view file show_calendar.php.
    • Now let us see the code in view file show_calendar.php :
    • 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:
    • 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