Calendar Library in CodeIgniter

Code With Travel
4 min readAug 3, 2018

1.) Download Codeigniter From https://codeigniter.com/download
2.) Extact to one folder and run that folder to browser

http://localhost/ci3/

So welcome message will be displayed as below.

Welcome message screen

Now go to the application/config/autoload.php and make following changes.

$autoload['libraries'] = array('database');
$autoload['helper'] = array('url');

Go to application/config/config.php and make following change.

$config['index_page'] = '';

Now to go to the application/config/database.php and make database configuration.

Now make new controller Mycal.php in application/controllers folder.

<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Mycal extends CI_Controller {public function index()
{
$this->load->library('calendar'); // Load calender library
echo $this->calendar->generate();
}
}

If you want to change preference then make one array and pass it to load library function as below.

$prefs = array(
'start_day' => 'saturday',
'month_type' => 'long',
'day_type' => 'short'
);
$this->load->library('calendar',$prefs); // Load calender library

The above code will generate a calendar for the current month/year based on your server time. To show a calendar for a specific month and year you will pass this information to the calendar generating function:

public function index($year = NULL , $month = NULL)
{
$prefs = array(
'start_day' => 'saturday',
'month_type' => 'long',
'day_type' => 'short',
'show_next_prev' => TRUE,
'next_prev_url' => base_url().'/mycal/index/'
);
$this->load->library('calendar',$prefs); // Load calender library
echo $this->calendar->generate($year , $month);
}

Now let’s move this code in model So we can use proper MVC. For that make model application/models/Mycal_model.php

<?php

class Mycal_model extends CI_Model {
public $prefs;
public function __construct()
{
//parent::Model();
$this->prefs = array(
'start_day' => 'saturday',
'month_type' => 'long',
'day_type' => 'short',
'show_next_prev' => TRUE,
'next_prev_url' => base_url().'/mycal/index/'
);
}
public function getcalender($year , $month)
{
$this->load->library('calendar',$this->prefs); // Load calender library
$data = array(
3 => 'check',
7 => 'check1',
13 => 'bar',
26 => 'ytr'
);
return $this->calendar->generate($year , $month , $data);
}

No call model function from controller file.

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Mycal extends CI_Controller {

public function index($year = NULL , $month = NULL)
{
$this->load->model('Mycal_model');
$data['calender'] = $this->Mycal_model->getcalender($year , $month);
$this->load->view('calview', $data);
}
}

Now make view application/views/calview.php

<?php
defined('BASEPATH') OR exit('No direct script access allowed');
?><!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Welcome to CodeIgniter</title>

</head>
<body>

<div id="container">
<h1>Welcome to CodeIgniter!</h1>

<div id="body">
<?php echo $calender; ?>
</div>

<p class="footer">Page rendered in <strong>{elapsed_time}</strong> seconds. <?php echo (ENVIRONMENT === 'development') ? 'CodeIgniter Version <strong>' . CI_VERSION . '</strong>' : '' ?></p>
</div>

</body>
</html>

Creating a Calendar Template

For creating template just copy code which codeigniter provide by default.Here you get code .https://www.codeigniter.com/user_guide/libraries/calendar.html#creating-a-calendar-template

$this->prefs['template'] = '

{table_open}<table border="0" cellpadding="0" cellspacing="0">{/table_open}

{heading_row_start}<tr>{/heading_row_start}

{heading_previous_cell}<th><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><a href="{next_url}">&gt;&gt;</a></th>{/heading_next_cell}

{heading_row_end}</tr>{/heading_row_end}

{week_row_start}<tr>{/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_start_today}<td>{/cal_cell_start_today}
{cal_cell_start_other}<td class="other-month">{/cal_cell_start_other}

{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_no_content}{day}{/cal_cell_no_content}
{cal_cell_no_content_today}<div class="highlight">{day}</div>{/cal_cell_no_content_today}

{cal_cell_blank}&nbsp;{/cal_cell_blank}

{cal_cell_other}{day}{/cal_cel_other}

{cal_cell_end}</td>{/cal_cell_end}
{cal_cell_end_today}</td>{/cal_cell_end_today}
{cal_cell_end_other}</td>{/cal_cell_end_other}
{cal_row_end}</tr>{/cal_row_end}

{table_close}</table>{/table_close}
';

Now you can see calendar view with content. To make it user friendly add css.You can make new css file and include it or simply add in view file.

<style type="text/css">
table{
border: 15px solid #25BAE4;
border-collapse:collapse;
margin-top: 50px;
margin-left: 250px;
}
td{
width: 50px;
height: 50px;
text-align: center;
border: 1px solid #e2e0e0;
font-size: 18px;
font-weight: bold;
}
th{
height: 50px;
padding-bottom: 8px;
background:#25BAE4;
font-size: 20px;
}
.prev_sign a, .next_sign a{
color:white;
text-decoration: none;
}
tr.week_name{
font-size: 16px;
font-weight:400;
color:red;
width: 10px;
background-color: #efe8e8;
}
.highlight{
background-color:#25BAE4;
color:white;
height: 27px;
padding-top: 13px;
padding-bottom: 7px;
}
.calender .days td
{
width: 80px;
height: 80px;
}
.calender .hightlight
{
font-weight: 600px;
}
.calender .days td:hover
{
background-color: #DEF;
}
</style>

Now if you want to get data from database then go to the application/config/database.php and add database name and user , password.

Now import calendar.sql file from sql folder and add following code in model file.

public function get_calender_data($year , $month)
{
$query = $this->db->select('date,content')->from('calendar')->like('date',"$year-$month",'after')->get();
//echo $this->db->last_query();exit;
$cal_data = array();
foreach ($query->result() as $row) {
$calendar_date = date("Y-m-j", strtotime($row->date)); // to remove leading zero from day format
$cal_data[substr($calendar_date, 8,2)] = $row->content;
}

return $cal_data;
}

For code you can check Git repository https://github.com/stemword/codeigniter-calendar-library-example

Conclusion : Thanks for reading the complete post. Hope you have got the concept. Share your thoughts and Like my YouTube channel.

--

--