Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Codeigniter calendar not displaying correct data and set limit not working

I am using CodeIgniter Calendar library.

I have two tables that I get calendar data from my database.

  1. events table
  2. extra_events table

The problem I have 2 foreach loops with the second foreach loop $query2->result_array() it does not let me get the first events on each day from database table extra_events I am also trying to be able to set the limit from the extra events table but not working.

From the extra_events table

Example

Test 2 | Example 2
       | Example 3

It should produce

Test 1 | Example 1 <!-- Missing
Test 2 | Example 2
       | Example 3

Question how can I make sure I get all the results correct for $query2->result_array().

Image

enter image description here

Get function

public function get_events($year, $month) {
        $calendar = array();

        $this->db->select('*');
        $this->db->from('events');
        $this->db->where('year', $year);
        $this->db->where('month', $month);
        $query1 = $this->db->get();

        foreach ($query1->result_array() as $event) {

            $this->db->select('*');
            $this->db->from('extra_events');
            $this->db->where('events_id', $event['events_id']);
            $this->db->where('year', $year);
            $this->db->where('month', $month);
            //$this->db->limit(2);
            $query2 = $this->db->get();

            foreach ($query2->result_array() as $result) {

                if (array_key_exists($event['day'], $calendar)) {

                    $calendar[$event['day']] =  $calendar[$event['day']]  . '<br/>' . $result['event'];

                } else {

                    $calendar[$event['day']] = '<br/><b>From Events Table</b> <i class="fa fa-long-arrow-down"></i><br/>' . $event['event'] . '<hr><b>From Extra Events Table</b> <i class="fa fa-long-arrow-down"></i>' ;

                }

            }
        }

        return $calendar;

    }

Database

enter image description here

enter image description here

like image 201
Mr. ED Avatar asked Dec 25 '15 21:12

Mr. ED


2 Answers

Solved Now

I have had to create two tables and then on the model function added this line

$calendar[$event['day']] = '<br/>' . $event['event'];

Below first foreach for the main event and then any other extra events use the second foreach with array key exists.

This now also lets me control how many rows are now displayed in extra events foreach loop.

public function get_calendar_events($year, $month) {
        $calendar = array();

        $events = $this->get_events($year, $month);

        foreach($events as $event) {

            // Added line for main calendar event
            $calendar[$event['day']] = '<br/>' . $event['event'];

            // Passes main event id to extra_events get function
            $extra_events = $this->get_extra_events($event['events_id']);

            // checks if array key exists for extra event in side foreach loop
            foreach($extra_events as $extra_event) {
                if (array_key_exists($extra_event['day'], $calendar)) {
                    $calendar[$extra_event['day']] = $calendar[$extra_event['day']] . '<br/>' .  $extra_event['event'];
                } else {
                    $calendar[$extra_event['day']] =  $extra_event['event'];
                }
            }
        }


        return $calendar;
    }

Calendar Model

<?php

class Model_calendar extends CI_Model {

    public function add_event() {
        $data = array(
            'year' => $this->input->post('year'),
            'month' => ($this->input->post('month') > 10) ? $this->input->post('month') : '0' . $this->input->post('month'),
            'day' => $this->input->post('day'),
            'date' => $this->input->post('year') .'-'. $this->input->post('month') .'-'. $this->input->post('day'),
            'event' => $this->input->post('event'),
            'date_added' => mdate('%Y-%m-%d %H:%i:%s', now())
       );

       $this->db->insert($this->db->dbprefix . 'events', $data);
    }

    public function add_extra_event() {
        $data = array(
            'events_id' => (int)$this->get_event_id(),
            'year' => $this->input->post('year'),
            'month' => ($this->input->post('month') > 10) ? $this->input->post('month') : '0' . $this->input->post('month'),
            'day' => $this->input->post('day'),
            'date' => $this->input->post('year') .'-'. $this->input->post('month') .'-'. $this->input->post('day'),
            'event' => $this->input->post('event'),
            'date_added' => mdate('%Y-%m-%d %H:%i:%s', now())
        );

        $this->db->insert($this->db->dbprefix . 'extra_events', $data);
    }

    public function get_event_id() {
        $this->db->where('year', $this->input->post('year'));
        $this->db->where('month', ($this->input->post('month') > 10) ? $this->input->post('month') : '0' . $this->input->post('month'));
        $this->db->where('day', $this->input->post('day'));

        $query = $this->db->get($this->db->dbprefix . 'events');

        if ($query->num_rows() > 0) {
            $row = $query->row();
            return $row->events_id;
        } else {
            return false;
        }
    }

     public function check_event() {
        $this->db->where('year', $this->input->post('year'));
        $this->db->where('month', ($this->input->post('month') > 10) ? $this->input->post('month') : '0' . $this->input->post('month'));
        $this->db->where('day', $this->input->post('day'));
        $query = $this->db->get($this->db->dbprefix . 'events');

        if ($query->num_rows() > 0) {
            return TRUE;
        } else {
            return FALSE;
        }
    }

    public function delete_events() {
        $this->db->where("date <", date('Y-m-d'));
        $this->db->delete($this->db->dbprefix . 'events');
    }

    public function delete_extra_events() {
        $this->db->where("date <", date('Y-m-d'));
        $this->db->delete($this->db->dbprefix . 'extra_events');
    }

    public function get_calendar_events($year, $month) {
        $calendar = array();

        $events = $this->get_events($year, $month);

        foreach($events as $event) {

            $calendar[$event['day']] = '<br/>' . $event['event'];

            $extra_events = $this->get_extra_events($event['events_id']);

            foreach($extra_events as $extra_event) {
                if (array_key_exists($extra_event['day'], $calendar)) {
                    $calendar[$extra_event['day']] = $calendar[$extra_event['day']] . '<br/>' .  $extra_event['event'];
                } else {
                    $calendar[$extra_event['day']] =  $extra_event['event'];
                }
            }
        }


        return $calendar;
    }

    public function get_events($year, $month) {
        $this->db->where('year', $year);
        $this->db->where('month', $month);
        $events = $this->db->get($this->db->dbprefix . 'events');
        return $events->result_array();
    }

    public function get_extra_events($events_id) {
        $this->db->limit(5);
        $this->db->where('events_id', $events_id);
        $extra_events = $this->db->get($this->db->dbprefix . 'extra_events');
        return $extra_events->result_array();
    }
}

Calendar Controller

<?php

class Calendar extends CI_Controller {

    public function __construct() {
        parent::__construct();
        $this->load->model('dashboard/model_calendar');
        $this->load->library('calendar');
        $this->load->library('form_validation');

    }

    public function index() {    
        if ($this->uri->segment(3) == FALSE) {
            $year = date('Y');
        } else {
            $year = $this->uri->segment(3);
        }

        if ($this->uri->segment(4) == FALSE) {
            $month = date('m');
        } else {
            $month = $this->uri->segment(4);
        }

        $prefs = array(
            'start_day' => 'monday',
            'show_next_prev' => true,
            'month_type'   => 'long',
            'day_type' => 'long',
            'next_prev_url' => base_url('dashboard/calendar')
        );

        $prefs['template'] = '
            {table_open}<div class="table-responsive"><table border="0" cellpadding="0" cellspacing="0" class="table table-hover table-striped table-bordered calendar">{/table_open}

            {heading_row_start}<tr>{/heading_row_start}

            {heading_previous_cell}<th><a href="{previous_url}"><i class="fa fa-chevron-left fa-2x "></i></a></th>{/heading_previous_cell}
            {heading_title_cell}<th class="text-center" colspan="{colspan}">{heading}</th>{/heading_title_cell}
            {heading_next_cell}<th class="text-right "><a href="{next_url}"><i class="fa fa-chevron-right fa-2x"></i></a></th>{/heading_next_cell}

            {heading_row_end}</tr>{/heading_row_end}

            {week_row_start}<tr >{/week_row_start}
            {week_day_cell}<td class="text-center" style="height: 5rem;">{week_day}</td>{/week_day_cell}
            {week_row_end}</tr>{/week_row_end}

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

            {cal_cell_content}
                <div class="day_number">{day}</div>
                <div class="content" style="margin-top: 0;">{content}</div>
            {/cal_cell_content}
            {cal_cell_content_today}
                <div class="day_number highlight">{day}</div>
                <div class="content" style="margin-top: 0;">{content}</div>
            {/cal_cell_content_today}

            {cal_cell_no_content}
            <div class="day_number">{day}</div>
            {/cal_cell_no_content}
            {cal_cell_no_content_today}
            <div class="day_number 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></div>{/table_close}
        ';

        $this->calendar->initialize($prefs);

        $this->model_calendar->delete_events();

        $this->model_calendar->delete_extra_events();

        $events = $this->model_calendar->get_calendar_events($year, $month);

        $data['calendar'] = $this->calendar->generate($year, $month, $events);

        if ($this->uri->segment(3) == TRUE) {
            $data['view_more'] = site_url('report/events/'.  $year .'/'. $month);
        } else {
            $data['view_more'] = site_url('report/events');
        }

        $this->form_validation->set_rules('event', 'Calendar Event', 'trim|required');

        if ($this->form_validation->run() == FALSE) {

            $this->load->view('dashboard/calender_view', $data);

        } else {

            if ($this->model_calendar->check_event() == TRUE) {

                $this->model_calendar->add_extra_event();

            } else {

                $this->model_calendar->add_event();

            }

            $this->session->set_flashdata('added_event', '<i class="fa fa-check-circle"></i> You have added event to your calendar!');

            redirect('common/dashboard');

        }
    }
} 
like image 120
Mr. ED Avatar answered Jan 30 '23 09:01

Mr. ED


You can limit foreach to get just 2 events.

limiting foreach loop in get_events() function.

public function get_events($year, $month) {
        $calendar = array();

        $this->db->where('year', $year);
        $this->db->where('month', $month);
        $query = $this->db->get('events');

        $results = $query->result_array();

        $i = 0;
        foreach ($results as $event) {
            if ($i < 2) {     // getting / fetching / extracting value from foreach for 2 times only
                if (array_key_exists($event['day'], $calendar)) {

                    $calendar[$event['day']] = $calendar[$event['day']] . '
                <hr/>

                <div class="text-center">
                <span class="label label-info">New</span>
                </div>
                <br/>
                <div class="clearfix">
                <div class="pull-left">
                <ul class="list-unstyled text-center">
                <li >'
                            . word_limiter(anchor(base_url('report/events/' . $year . '/' . $month . '/?event_id=' . $event['events_id']), $event['event']), 4) . '
                </li>
                </ul>
                </div>
                <div class="pull-right">
                <form id="form-event" action="' . base_url('dashboard/calendar') . '" class="form-inline" method="post" enctype="multipart/form-data" onsubmit="return confirm(\'Are you sure you want to remove this!\')";>
                <input type="hidden" name="delete_id" value="' . $event['events_id'] . '">
                <a href="' . base_url('report/events/edit' . '/?event_id=' . $event['events_id']) . '" role="button" class="btn btn-primary"><i class="fa fa-pencil"></i></a>
                <button type"submit" class="btn btn-danger"><i class="fa fa-trash"></i></button>
                </form>
                </div>
                </div>';
                } else {

                    $calendar[$event['day']] = '
                <div class="text-center">
                <span class="label label-info">New</span>
                </div>
                <br/>
                <div class="clearfix">
                <div class="pull-left">
                <ul class="list-unstyled text-center">
                <li >'
                            . word_limiter(anchor(base_url('report/events/' . $year . '/' . $month . '/?event_id=' . $event['events_id']), $event['event']), 4) . '
                </li>
                </ul>
                </div>
                <div class="pull-right">
                <form id="form-event" action="' . base_url('dashboard/calendar') . '" class="form-inline" method="post" enctype="multipart/form-data" onsubmit="return confirm(\'Are you sure you want to remove this!\')";>
                <input type="hidden" name="delete_id" value="' . $event['events_id'] . '">
                <a href="' . base_url('report/events/edit' . '/?event_id=' . $event['events_id']) . '" role="button" class="btn btn-primary"><i class="fa fa-pencil"></i></a>
                <button type"submit" class="btn btn-danger"><i class="fa fa-trash"></i></button>
                </form>
                </div>
                </div>
                ';
                }
                $i++;
            }
        }

        return $calendar;
    }
like image 23
Ali Shan Avatar answered Jan 30 '23 09:01

Ali Shan