CodeIgniter Forums

Full Version: Adding multiple variables to calendar days
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
Start with adding a placeholder for the css class in the template you want to use:
Code:
'cal_cell_start'    => '<td {css_class}>'

In the generate function (in MY_Calendar.php), look up this line:
PHP Code:
$out .= ($is_current_month === TRUE && $day == $cur_day) ? $this->replacements['cal_cell_start_today'] : $this->replacements['cal_cell_start']; 

Replace it with this:
PHP Code:
$csstmp = isset($css[$day]) ? 'class="' $css[$day] . '"' NULL;
$cstoday str_replace('{css_class}'$csstmp,$this->replacements['cal_cell_start_today']);
$cs str_replace('{css_class}',$csstmp,$this->replacements['cal_cell_start']);
$out .= ($is_current_month === TRUE && $day == $cur_day) ? $cstoday $cs

The result is that the placeholder {css_class} is replaced with the given class for that day, or with NULL if no class was given for that day.
Ok here is my code so far

MY_calendar (modified as you mentioned):
PHP Code:
<?php defined('BASEPATH') OR exit('No direct script access allowed');

class 
MY_Calendar extends CI_Calendar {

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

 /**
 * Generate the calendar
 *
 * @param int the year
 * @param int the month
 * @param array the data to be shown in the calendar cells
 * @return string
 */
 
public function generate($year ''$month ''$data = array(), $css = array() ) 
 {
 
$local_time time();

 
// Set and validate the supplied month/year
 
if (empty($year))
 {
 
$year date('Y'$local_time);
 }
 elseif (
strlen($year) === 1)
 {
 
$year '200'.$year;
 }
 elseif (
strlen($year) === 2)
 {
 
$year '20'.$year;
 }

 if (empty(
$month))
 {
 
$month date('m'$local_time);
 }
 elseif (
strlen($month) === 1)
 {
 
$month '0'.$month;
 }

 
$adjusted_date $this->adjust_date($month$year);

 
$month $adjusted_date['month'];
 
$year $adjusted_date['year'];

 
// Determine the total days in the month
 
$total_days $this->get_total_days($month$year);

 
// Set the starting day of the week
 
$start_days = array('sunday' => 0'monday' => 1'tuesday' => 2'wednesday' => 3'thursday' => 4'friday' => 5'saturday' => 6);
 
$start_day = isset($start_days[$this->start_day]) ? $start_days[$this->start_day] : 0;

 
// Set the starting day number
 
$local_date mktime(1200$month1$year);
 
$date getdate($local_date);
 
$day  $start_day $date['wday'];

 while (
$day 1)
 {
 
$day -= 7;
 }

 
// Set the current month/year/day
 // We use this to determine the "today" date
 
$cur_year date('Y'$local_time);
 
$cur_month date('m'$local_time);
 
$cur_day date('j'$local_time);

 
$is_current_month = ($cur_year == $year && $cur_month == $month);

 
// Generate the template data array
 
$this->parse_template();

 
// Begin building the calendar output
 
$out $this->replacements['table_open']."\n\n".$this->replacements['heading_row_start']."\n";

 
// "previous" month link
 
if ($this->show_next_prev === TRUE)
 {
 
// Add a trailing slash to the URL if needed
 
$this->next_prev_url preg_replace('/(.+?)\/*$/''\\1/'$this->next_prev_url);

 
$adjusted_date $this->adjust_date($month 1$year);
 
$out .= str_replace('{previous_url}'$this->next_prev_url.$adjusted_date['year'].'/'.$adjusted_date['month'], $this->replacements['heading_previous_cell'])."\n";
 }

 
// Heading containing the month/year
 
$colspan = ($this->show_next_prev === TRUE) ? 7;

 
$this->replacements['heading_title_cell'] = str_replace('{colspan}'$colspan,
 
str_replace('{heading}'$this->get_month_name($month).'&nbsp;'.$year$this->replacements['heading_title_cell']));

 
$out .= $this->replacements['heading_title_cell']."\n";

 
// "next" month link
 
if ($this->show_next_prev === TRUE)
 {
 
$adjusted_date $this->adjust_date($month 1$year);
 
$out .= str_replace('{next_url}'$this->next_prev_url.$adjusted_date['year'].'/'.$adjusted_date['month'], $this->replacements['heading_next_cell']);
 }

 
$out .= "\n".$this->replacements['heading_row_end']."\n\n"
 
// Write the cells containing the days of the week
 
.$this->replacements['week_row_start']."\n";

 
$day_names $this->get_day_names();

 for (
$i 0$i 7$i ++)
 {
 
$out .= str_replace('{week_day}'$day_names[($start_day $i) %7], $this->replacements['week_day_cell']);
 }

 
$out .= "\n".$this->replacements['week_row_end']."\n";

 
// Build the main body of the calendar
 
while ($day <= $total_days)
 {
 
$out .= "\n".$this->replacements['cal_row_start']."\n";

 for (
$i 0$i 7$i++)
 {
 if (
$day && $day <= $total_days)
 {
 
$csstmp = isset($css[$day]) ? 'class="' $css[$day] . '"' NULL;
$cstoday str_replace('{css_class}'$csstmp,$this->replacements['cal_cell_start_today']);
$cs str_replace('{css_class}',$csstmp,$this->replacements['cal_cell_start']);
$out .= ($is_current_month === TRUE && $day == $cur_day) ? $cstoday $cs

 if (isset(
$data[$day]))
 {
 
// Cells with content
 
$temp = ($is_current_month === TRUE && $day == $cur_day) ?
 
$this->replacements['cal_cell_content_today'] : $this->replacements['cal_cell_content'];
 
$out .= str_replace(array('{content}''{day}'), array($data[$day], $day), $temp);
 }
 else
 {
 
// Cells with no content
 
$temp = ($is_current_month === TRUE && $day == $cur_day) ?
 
$this->replacements['cal_cell_no_content_today'] : $this->replacements['cal_cell_no_content'];
 
$out .= str_replace('{day}'$day$temp);
 }

 
$out .= ($is_current_month === TRUE && $day == $cur_day) ? $this->replacements['cal_cell_end_today'] : $this->replacements['cal_cell_end'];
 }
 elseif (
$this->show_other_days === TRUE)
 {
 
$out .= $this->replacements['cal_cell_start_other'];

 if (
$day <= 0)
 {
 
// Day of previous month
 
$prev_month $this->adjust_date($month 1$year);
 
$prev_month_days $this->get_total_days($prev_month['month'], $prev_month['year']);
 
$out .= str_replace('{day}'$prev_month_days $day$this->replacements['cal_cell_other']);
 }
 else
 {
 
// Day of next month
 
$out .= str_replace('{day}'$day $total_days$this->replacements['cal_cell_other']);
 }

 
$out .= $this->replacements['cal_cell_end_other'];
 }
 else
 {
 
// Blank cells
 
$out .= $this->replacements['cal_cell_start'].$this->replacements['cal_cell_blank'].$this->replacements['cal_cell_end'];
 }
 
$day++;
 }

 
$out .= "\n".$this->replacements['cal_row_end']."\n";
 }

 return 
$out .= "\n".$this->replacements['table_close'];
 }

 
// -------------------------------------------------------------------- 
}
?>




Controller:
PHP Code:
public function calendar_builder()
 
         {
 
              $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 {css_class} 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}<div class = "div-day"><td class="other-month">{/cal_cell_start_other}

                  {cal_cell_no_content}<div class = "div-day"><a href="controller/method/{year}/{month}/{day}">{day}</a></div>{/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}

                  {cal_cell_no_content}<div class = "div-day">{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();
 
         $css=array();
 
         $days=0;
 
         $month++;
 
         $css = array(
 
             3 => 'cal-blue',
 
             7 => 'cal-red',
 
             13 => 'cal-green',
 
             26 => 'cal-orange'
 
           );
 
         
          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$css);?></div>
   <?php
   
if($month==6)
 
  {
 
  ?></div><div class="row"><?php
   
}
 
  ?>

   </div> 
</div>
 
<hr /> 

 
Just not sure where this goes

PHP Code:
'cal_cell_start'    => '<td {css_class}>' 


Thanks Smile
(08-05-2017, 03:32 AM)behnampmdg3 Wrote: [ -> ]Just not sure where this goes

PHP Code:
'cal_cell_start'    => '<td {css_class}>' 

Thanks Smile

In the $prefs['template'] part. The example shows how to assign a value if you pass the template as an array. You are passing it as a string.
You've done it the right way:
Code:
{cal_cell_start}<td {css_class} style = "padding:0px; border:none; line-height:2.2">{/cal_cell_start}

I just wonder why you have a class name AND a style at the same time. I think it's better to have the style attributes as part of the css class (in your css file). Remember that you can combine multiple classes, separated by spaces:
Code:
.cal_cell { padding: 0; border: none; line-height: 2.2em; }
.cal_red { background-color: red; }
.cal_blue { background-color: blue; }

PHP Code:
$css = array(
 
 3 => 'cal_cell cal_red',
 
 7 => 'cal_cell cal_blue'
); 
Hey Wouter60;

It's strange. I have set up everything the way you mentioned. It "should" work.

When I prinr_r($css) in MY_calendar I get this https://ibb.co/iaFzpv

Looks like classes gets passed to the view however, it doesnt go inside td. Check this https://ibb.co/dzLtGa

So what I did, I attached the applicaiton directory to this post here so you can see where I am messing this up.

Thanks bud
Quote:So what I did, I attached the applicaiton directory to this post here so you can see where I am messing this up.

Where????
Hey sorry;

Here it is Smile

The method in controller is called "calendar_builder".
I'll look into it later this week. One problem I noticed right away is that you pass one array of css-styles to the view, but your view is generating the calendar for 12 months. If the css array contains 5 => 'cal-blue', every 5th day of each month is styled that way.
Hi;

Yes the thing is it's not adding "cal-blue" anywhere.

I am 99% sure the issue is in the main controller.

Thanks
There are two ways to solve this.

1. Put your css styles in a 2-dimensional array (in the controller):
PHP Code:
$css = array(
 
 1 => array(
 
   3 => 'cal-blue',
 
   7 => 'cal-red'
 
  ),
 
 5 => array(
 
   4 => 'cal-red',
 
   23 => 'cal-green'
 
  )
); 
In this exemple, you create styles for a few days in January and May.
Pass this array to the view.

You also need to change the MY_Calender class.
Insert this at the beginning of the generate() function:
PHP Code:
$m $month //because you change $month to a 2-character string; later on you need $m as a digit 

Then, at the position where you inserted new code, change the line that starts with $csstmp:
PHP Code:
$csstmp = isset($css[$m][$day]) ? 'class="' $css[$m][$day] . '"' NULL

2. Generate all months from the controller (in a loop).
The generate() function returns a string.
Put all 12 strings in an array and pass it to your view.
PHP Code:
for ($month 1$month <= 12$month++) {
 
  //collect $data from other function in controller or model
 
  //collect $css from other function in controller or model
 
  $cal_months[] = $this->calendar->generate($year$month$data$css);
}
$this->data['cal_months'] = $cal_months;
$this->load->view('welcome_page',$this->data); 
This way, you can leave the MY_Calendar class as it is now. But you will have to rewrite the view.
With this approach, you move more of your application's logic to the controller, which is better. Keep your views as simple as possible.

In your current view, the $css array is initialized several times (line 10 and line 31). This means that the $css array that you passed from the controller, gets overwritten. Comment these 2 lines with // and see what that does.

With this, you must be able to work it out by yourself. I'm reluctant to write the complete application for you. You learn CI best by overcoming your own mistakes. Good luck!
Pages: 1 2