Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Integrate jQuery fullcalendar in cakephp

    • 0
    • 0
    • 0
    • 0
    • 4
    • 0
    • 0
    • 0
    • 1.91k
    Comment on it

    This tutorial will help user learn how to integrate the jQuery fullcalendar in our web application blogs etc.Below are the steps to integrate the fullcalendar:


    Step 1:Write an action in an Eventcontroller let us say, event_calendar

    path: /app/Controller/EventsController.php

        public function event_calendar() {
    
            //fetch all the events from the database (say we have and Event table)
            // the default parameters that we use as a key for any array 
            // fetched by find quey are as ('title','user','start','end','backgroudColor','textColor')
    
    For Example:-
    $events = $this->Event->find('all',array(
                    'fields'=>array(
                    'eventname',
                    'Client.firstname', // if we find any association
                    'startdate',
                    'enddate','id')));
    $allevents=array();
    foreach($events as $event){
    
            $allevents[]=array(
                 'title'     =>$event['Event']['eventname'],
                 'user'      =>$event['Client']['firstname'],
                 'start'     =>$event['Event']['startdate'],
                 'end'       =>$event['Event']['enddate'],
                 'backgroundColor'=>$eventcolor,
                 'textColor' =>'#000000');
    
        }
      $this->set('allevents',$allevents);
    

    Step 2:Now we will create an event_calendar.ctp file inside the Event directory under the directory View

    path: /View/Event/event_calendar.ctp

    <?php
    echo  $this->Html->css(array('fullcalendar/fullcalendar.min','fullcalendar/fullcalendar.print')); 
    
    echo $this->Html->script(array('fullcalendar/moment.min','fullcalendar/jquery.min',
    'fullcalendar/fullcalendar.min'));
    ?>
    
    <script>
    
        $(document).ready(function() {
    
            var jData = $("#JSONdata").html();
            //var colors = eventColoralert(jData);
            $('#calendar').fullCalendar({
                header: {
                    left: 'prev,next today',
                    center: 'title',
                    right: 'month,basicWeek,basicDay'
                },
                events: jQuery.parseJSON(jData),
                defaultDate: '2015-02-12',
                editable: true,
                eventLimit: true, // allow "more" link when too many events
            });
        });
    </script>
    <style>
        body {
            margin: 40px 10px;
            padding: 0;
            font-family: "Lucida Grande",Helvetica,Arial,Verdana,sans-serif;
            font-size: 14px;
        }
        #calendar {
            max-width: 900px;
            margin: 0 auto;
        }
    </style>
    
    <?php
       $encodedData = json_encode($allevents);
    ?>
    
    <div id='JSONdata' style="display:none;"><?php echo $encodedData; ?></div>
    
    <div id="eventContent">
         <div id="eventInfo"></div>
    </div>
    <div id='calendar'></div>
    

    Note:

    Make sure we select the right JavaScript and CSS files also take care that the FullCalendar stylesheet, as well as the jQuery, FullCalendar, and Moment JavaScript files are included, in the head of your page.

    Example screenshot shown below-

    alt text

 4 Comment(s)

  • Hi Kronos,

    If you want to put the details (lets say you want to put the "description" of the event and for each event there will be different description that you will fetch from db and concatenate it with the event name) follow the steps as below:

    For example:

     $allevents = array( 
      'title' =>$event['Event']['eventname'].$event['Event']['description'],
      'user' =>$event['Client']['firstname'],
      'start' =>$event['Event']['startdate'], 
      'end' =>$event['Event']['enddate'], 
      'backgroundColor'=>$eventcolor, 
      'textColor' =>'#000000' ); 
    

    Thank you Pushpendra

  • Hi Kronos,

    As, I have explained in the example that you need to fetch all the required data from the database and create an array for that fetched result set. Make sure that you define the indexes of the array with names that are predefined properties of the jquery calendar. For example I created an array for the fetched result set as below:

     $allevents = array(
        'title' =>$event['Event']['eventname'],
        'user' =>$event['Client']['firstname'],
        'start' =>$event['Event']['startdate'],
        'end' =>$event['Event']['enddate'],
        'backgroundColor'=>$eventcolor,
        'textColor' =>'#000000'
    );
    

    indexes defined are 'title','user','start','end','backgroundColor','textColor' all are properties, that are predefined for jquery calendar in this way you can found more options.

    Now to see the output copy paste the code written in my file event_calendar.ctp and replace the array $allevents with your array and execute the code.

    Thank you.

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: