Welcome Guest, Not a member yet? Register   Sign In
Adding multiple variables to calendar days
#1

Hello;

I am using Codeigniter calendar3 and so far I have got it this far -> https://prnt.sc/g2z1co

I have a very specific requirement I am not sure how to achieve.

I want to check tasks in my database (already do), and when I build the calandar, wanna change the CSS of that day and highlight the div or td or ....

Also, I want every single day to be a link (like July 25th on my current code) so I can click and add task etc.

I am not that far but I am not sure how to take it from here.

In the doc they say it's possible to do whatever.

Here is my code:

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

class 
Welcome extends CI_Controller {

 
/**
 * Index Page for this controller.
 *
 * Maps to the following URL
 * http://example.com/index.php/welcome
 * - or -
 * http://example.com/index.php/welcome/index
 * - or -
 * Since this controller is set as the default controller in
 * config/routes.php, it's displayed at http://example.com/
 *
 * So any other public methods not prefixed with an underscore will
 * map to /index.php/welcome/<method_name>
 * @see https://codeigniter.com/user_guide/general/urls.html
 */
 
public function index()
 
         {
 
          $query $this->db->get_where('tasks', array('user_id' => 1));
 
              $this->data['tasks'] = $query->result_array();
 
              $this->calendar();
 
              $this->load->view('welcome_message'$this->data);
 
         }
 
    public function calendar()
 
         {
 
              $prefs['template'] = '
                  {table_open}<table border="0" cellpadding="0" cellspacing="0" class = "text-center table" id = "table-calendar" style = "font-size:13px; border:0; font-family:Arial,sans-serif; width:90%">{/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}"><strong style = "font-size:">{heading}</strong><hr /></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 style = " border:0;">{/week_row_start}
                  {week_day_cell}<td style = "padding:0px; border:none;">{week_day}</td>{/week_day_cell}
                  {week_row_end}</tr>{/week_row_end}

                  {cal_row_start}<tr style = " border:0; ">{/cal_row_start}
                  {cal_cell_start}<td style = "padding:0px; border:none; line-height:2.2">{/cal_cell_start}
                  {cal_cell_start_today}<div class = "div-day"><td style = "background-color:#419dcd">{/cal_cell_start_today}
                  {cal_cell_start_other}<td class="other-month">{/cal_cell_start_other}

                  {cal_cell_content}<a style = "text-decoration:none" href="{content}"><div class = "div-day">{day}</div></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}'
;
 
              $this->load->library('calendar'$prefs);
 
             


View

PHP Code:
<div class="container">
 <
div class="row">
 
  <?php
   $year 
date('Y');
 
  $month=0;
 
     
   
while($month<12)
 
  {
 
  $data=array();
 
         $days=0;
 
         $month++;
 
         while($days<32)
 
  {
 
  $days++;
 
             foreach($tasks as $row => $val)
 
               {
 
                 if(strtotime($year."-".$month."-".$days)==strtotime($val['date_added']))
 
                   {
 
                       $data[$days] = $days;
 
                   }
 
               }
 
           
 
         ?>
   <div class="col-md-2"><?php echo $this->calendar->generate($year,$month$data);?></div>
   <?php
   
if($month==6)
 
  {
 
  ?></div><div class="row"><?php
   
}
 
  ?>

   </div> 
</div> 

Thanks
Reply
#2

(This post was last modified: 08-01-2017, 11:45 AM by Martin7483.)

From the documentation

PHP Code:
$this->load->library('calendar');

$data = array(
 
       3  => 'http://example.com/news/article/2006/06/03/',
 
       7  => 'http://example.com/news/article/2006/06/07/',
 
       13 => 'http://example.com/news/article/2006/06/13/',
 
       26 => 'http://example.com/news/article/2006/06/26/'
);

echo 
$this->calendar->generate(20066$data); 

Quote:Using the above example, day numbers 3, 7, 13, and 26 will become links pointing to the URLs you’ve provided.

You will need to pass a data array that holds an url for each day of a given month


This function will generate an array containing the number of days for each month of a passed in year.
If you don't pass in a year the current year is used
PHP Code:
function days_per_month($year '')
{
 
   $year = empty($year) ? date("Y"time()) : $year;
 
   
    for
($month 1$month <= 12$month++)
 
   {
 
       $days_per_month[$month] = date("t"mktime(000$month1$year) );
 
   }
    return 
$days_per_month;

Reply
#3

(This post was last modified: 08-01-2017, 12:32 PM by behnampmdg3.)

Hey

I dont think you read what I wrote. I need to pass multiple values as data. Please read again to see what I mean.

I want to style it like http://www.bootstrap-year-calendar.com/#...%20example so if there are events in that day the styling changes etc etc.

Thank you
Reply
#4

anyone?
Reply
#5

Is codeigniter forum dead?
Reply
#6

Quote:Is codeigniter forum dead?
Certainly not. Maybe there aren't that many users who have customized the calendering class. I agree that the documentation is not very clear about passing data to the class, if you want something else than just one link per day.

To make a link of every day in the calendar, in order to edit the information, you could use something like this in the template:
PHP Code:
{cal_cell_no_content}<a href="controller/method/{year]/{month}/{day}">{day}</a>{/cal_cell_no_content}
{
cal_cell_no_content_today}<div class="highlight"><a href="controller/method/{year}/{month}/{day}>">{day}</a></div>{/cal_cell_no_content_today

In the above example, replace controller/method with your own controller/method to edit the information for the given day. The year, month and day are passed as parameters in the URL. E.g. calendar/edit/2017/8/3.

The bootstrap calendar shows the appointments when you hover the mouse over the date. You'll need Javascript (or jQuery) to achieve that. In the jQuery function that is triggered when hovering, you can use AJAX to fetch the appointments for that day.
Reply
#7

Hey thanks for writing back. I am trying to understand you but there is somethign missing still Smile  (it's me not you)

I applied your code here -> http://webmoosh.com/ we can track it there.

The part I am not getting is passing the values to each day.

The jquery and css styling part is easy as long as I can dynamically add classes to each day.

First requirement is that every single day is a link. That part is ok for now howver the challenge is dynamcially adding class. Here's what I mean:

See here below is the view. I have list of tasks with date in $task array.

If the date of the task matches the calendar date then I wanna add a class.

PHP Code:
while($month<12)
 
  {
 
  $data=array();
 
         $days=0;
 
         $month++;
 
         while($days<32)
 
  {
 
  $days++;
 
             foreach($tasks as $row => $val)
 
               {
 
                 if(strtotime($year."-".$month."-".$days)==strtotime($val['date_added']))
 
                   {
 
                       //$data[$days] = array('days'=>$days,'class'=>'active');
 
                       $data[$days] = $days;
 
                   }
 
               }
 
           
 
         ?>
   <div class="col-md-2"><?php echo $this->calendar->generate($year,$month$data);?></div>
   <?php
   
if($month==6)
 
  {
 
  ?></div><div class="row"><?php
   
}
 
  ?>

Thanks for helping Smile
Reply
#8

Best way (IMHO) is create a MY_Calendar.php in application/libraries.
PHP Code:
class MY_Calendar extends CI_Calendar {

 
       public function __construct($config = array())
 
       {
 
               parent::__construct($config);
 
       }


Copy the generate() function from the core class (in system/libraries/Calendar.php) and paste it into MY_Calendar.
Now, add an extra parameter to the genarate function, like this:
PHP Code:
public function generate($year ''$month ''$data = array(), $css = array() ) 

To pass a specific css class for each day, use the same approach as for passing links for each day:
PHP Code:
$css = array(
 
 3 => 'cal-blue',
 
 7 => 'cal-red',
 
 13 => 'cal-green',
 
 26 => 'cal-orange'
); 

Inside the new generate() function, write to code for inserting the correct css-class into the output string ($out).
Reply
#9

Ok, so far http://webmoosh.com/

The only thing left is

"Inside the new generate() function, write to code for inserting the correct css-class into the output string ($out). "

Help?
Reply
#10

Can you please help me with this part as well?

"Inside the new generate() function, write to code for inserting the correct css-class into the output string ($out). "

Thanks
Reply




Theme © iAndrew 2016 - Forum software by © MyBB