How do I change the CodeIgniter calendar to handle multiple events per day?

I am creating a calendar without a jquery calendar plugin. I could get the data in the database in the calendar. But if there is more than one data per day, only one information comes into the calendar view. I want this to be the case when there is some data.

expected

But I get it when there is some data

current

day 16 has every four data. but he shows only one. If anyone has an idea about this, this will help me.

follow my code

controller

<?php

class My_calendar extends CI_Controller {

    public function index($year = null, $month = null) {
        echo "My calendar project";
        $this->showcal($year, $month);
    }

    public function showcal($year = null, $month = null) {
        $this->load->model('mycal_model');
        $data['calendar'] = $this->mycal_model->generate($year, $month);

        $this->load->view('mycal', $data);
    }

}

View

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <title>Welcome to CodeIgniter</title>

        <style type="text/css">



            #body{
                margin: 0 15px 0 15px;
            }



            #container{
                margin: 10px;
                border: 1px solid #D0D0D0;
                -webkit-box-shadow: 0 0 8px #D0D0D0;
            }


            .calendar{
                /*           background-color: yellow;*/
            }
            table.calendar{
                margin: auto;
                border-collapse: collapse;
            }
            .calendar .days td {
                width:80px;
                height: 90px;
                padding: 4px;
                border:  1px solid #999;
                vertical-align:  top;
                background-color: #DEF;

            }
            .calendar .days td:hover{
                background-color: #fff;
            }
            .calendar .highlight {
                font-weight: bold;
                color: #EF1BAC;
            }
            .calendar .content .gk_am{
                float: left;
                display: inline-block;
                width: 40px;
                background-color: #D0D0D0;
            }
            .calendar .content .gk_pm{
                float: right;
                display: inline-block;
                width: 40px;
                background-color: red;
            }

            .calendar .content .rp_am{
                float: left;
                display: inline-block;
                width: 40px;
                background-color: gray;
            }

            .calendar .content .rp_pm{
                float: right;
                display: inline-block;
                width: 40px;
                background-color: #D893A1;
            }
        </style>
    </head>
    <body>

        <div id="container">

            <div id="body">
                <?PHP echo $calendar; ?>

            </div>


        </div>

    </body>
</html>

Model

<?php

class mycal_model extends CI_Model {

    function __construct() {
        parent::__construct();
        $this->mycal_model();
    }

    function mycal_model() {

        $this->conf = array(
            'show_next_prev' => True,
            'next_prev_url' => base_url() . 'index.php/my_calendar/index/'
        );

        $this->conf['template'] = '
            {table_open}<table cellpadding="1" cellspacing="2" class="calendar">{/table_open}

            {heading_row_start}<tr>{/heading_row_start}

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

            {heading_row_end}</tr>{/heading_row_end}

            //Deciding where to week row start
            {week_row_start}<tr class="week_name" >{/week_row_start}
            //Deciding  week day cell and  week days
            {week_day_cell}<td >{week_day}</td>{/week_day_cell}
            //week row end
            {week_row_end}</tr>{/week_row_end}

            {cal_row_start}<tr class= "days">{/cal_row_start}
            {cal_cell_start}<td>{/cal_cell_start}

            {cal_cell_content}
                <div class="day_num">{day}
                </div>
                <div class= "content">{content}
                </div>
            {/cal_cell_content}

            {cal_cell_content_today}
            <div class="day_num highlight">{day}</div>
             <div class= "content">{content}</div>
            {/cal_cell_content_today}

            {cal_cell_no_content}<div class="day_num">{day}</div>{/cal_cell_no_content}
            {cal_cell_no_content_today}<div class="day_num highlight">{day}</div>{/cal_cell_no_content_today}

            {cal_cell_blank}&nbsp;{/cal_cell_blank}

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

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

    function get_calendar_data($year, $month) {

        $query = $this->db->select('date_cal,title,type')->from('reservations')->like('date', "$year-$month", 'after')->get();
        $cal_data = array();

        foreach ($query->result() as $row) {
            if ($row->title == 'GK' && $row->type == 'AM') {
                $cal_data[substr($row->date_cal, 8, 2)] = '<div class="gk_am">' . $row->title . ' ' . $row->type . '</div>';
            } else if ($row->title == 'GK' && $row->type == 'PM') {
                $cal_data[substr($row->date_cal, 8, 2)] = '<div class="gk_pm">' . $row->title . ' ' . $row->type . '</div>';
            } else if ($row->title == 'RP' && $row->type == 'AM') {
                $cal_data[substr($row->date_cal, 8, 2)] = '<div class="rp_am">' . $row->title . ' ' . $row->type . '</div>';
            } else if ($row->title == 'RP' && $row->type == 'PM') {
                $cal_data[substr($row->date_cal, 8, 2)] = '<div class="rp_pm">' . $row->title . ' ' . $row->type . '</div>';
            }
        }



        return $cal_data;
    }

    function generate($year, $month) {


        $this->load->library('calendar', $this->conf);

        $cal_data = $this->get_calendar_data($year, $month);

        return $this->calendar->generate($year, $month, $cal_data);
    }

}

?>
0
source share
1 answer

If you want to show all four fields for dates containing data, try changing get_calendar_data () foreach, as shown below,

$content = "";
$lastDay = -1;
$index = 0;
foreach ($query->result() as $row) {

        if($lastDay != intval(substr($row->date_cal, 8, 2))){       
            if($index > 0 ){
               if($content != ''){
                   $cal_data[$lastDay] = $content;
                   $content = '';
               }
            }
            $index = 0;               
        }


        if ($row->title == 'GK' && $row->type == 'AM') {
            $content .= '<div class="gk_am">' . $row->title . ' ' . $row->type . '</div>';
        } else if ($row->title == 'GK' && $row->type == 'PM') {
            $content .= '<div class="gk_pm">' . $row->title . ' ' . $row->type . '</div>';
        }else if ($row->title == 'RP' && $row->type == 'AM') {
            $content .= '<div class="rp_am">' . $row->title . ' ' . $row->type . '</div>';                
        } else if ($row->title == 'RP' && $row->type == 'PM') {
            $content .= '<div class="rp_pm">' . $row->title . ' ' . $row->type . '</div>';                
        }

        $lastDay = intval(substr($row->date_cal, 8, 2));
        $index++;     

    }

    if($lastDay != -1 && $content != ''){
        $cal_data[$lastDay] = $content;
    }

, .

+2

All Articles