• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Calendar Class: multiple events on one day

#1
[eluser]ehalber[/eluser]
Hi all,

This question has been addressed before here: http://ellislab.com/forums/viewthread/54422/

But, I can't seem to get things working. My question is: how to I get multiple events on each day?

CONTROLLER
Code:
(other stuff) .......

            $year = $this->uri->segment(3);
            $year = ($year > 0) ? $year : date("Y");

            $month = $this->uri->segment(4);
            $month = ($month > 0) ? $month : date("n");
            
            $events = $this->calendarModel->getEvents($year, $month);
            
            $calendar = array();
            
            foreach ($events as $event)
            {
                $calendar[date('d', strtotime($event->date))] = $event->name;
            }
          
            $data['calendar'] = $calendar;
            
            $this->load->view('admin/calendar', $data);
MODEL:
Code:
public function getEvents($year, $month)
    {
        
        $sql = "SELECT *
                FROM (events)
                INNER JOIN event_dates
                ON events.id = event_dates.event_id
                WHERE YEAR(date) = {$year}
                AND MONTH(date) = {$month}";

        $query = $this->db->query($sql);

        if ( $query->result() > 0 )
        {
            return $query->result();
        }
    }

Thanks!

#2
[eluser]sophistry[/eluser]
the code here:

Code:
$calendar[date('d', strtotime($event->date))] = $event->name;

will only have *one* event per array index because the index is the day number. so, if you have two events on day 1 of the month the foreach will write over the first one with the second.

if you don't want to modify the current code you have (which i'd suggest you do if you want multiple events per day), just use a nested array for the events.

it isn't clear whether you want to use the CI calendar class for this, but it seems like you might be because why else would you be preparing an array with day numbers as keys; that is one of the essential flaws of the current CI calendar class.

#3
[eluser]ehalber[/eluser]
Hi sophistry and thanks for your reply...I ended up solving the problem like this:

Code:
foreach ($events as $event)
            {
                if ( array_key_exists( $event->day, $calendar ) )
                {
                    $calendar[$event->day] = $calendar[$event->day] .'<br />'.
                    anchor(base_url() .'admin/calendar/'. $year .'/'. $month .'/'.
                    $event->id, $event->name);
                }
                else
                {
                    $calendar[$event->day] = anchor(base_url() .'admin/calendar/'.
                    $year .'/'. $month .'/'. $event->id, $event->name)   ;
                }
            }

This enables me to check whether or not an array key exists...if it does great, we'll just append some data to it, otherwise, just create a new entry.

In your post you mentioned nested arrays...

[quote author="sophistry" date="1202867826"]the code here:
if you don't want to modify the current code you have (which i'd suggest you do if you want multiple events per day), just use a nested array for the events.
[/quote]

I'm not sure if that's what I did ( still a very new to the development stuff ), but would you mind elaborating a little. Thank you. Smile

#4
[eluser]sophistry[/eluser]
your solution is fine and works well with the CI calendar class. but, appending strings is not very flexible once you get the data into the view (which is exactly why the CI calendar class is sub par (compared to much of the rest of CI)). it forces you to have one key per day! then if you want to use the template you have to manipulate strings in the view or build HTML in the controller... ugh.

i suggested nested arrays because arrays are nicer to work with:

Code:
$arr1 = array();
$arr2 = array();
$nested_array = array($arr1, $arr2);


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2019 MyBB Group.