Jump to content

Codeigniter Calendar - Populating


Danny620

Recommended Posts

Hi I am wanting to use codeigniter to build a calendar however to populate the calendar you need to provide the day numbers how can i do this with a database design like so

 

id - int (10)

check_in - date

check_out - date

 

p.s it would need to fill in the dates to between

 

sample data

 

1,      2014-02-12      , 2014-02-15

2,      2014-02-15      , 2014-02-22

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

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

echo $this->calendar->generate(2014, 6, $data);

http://ellislab.com/codeigniter%20/user-guide/libraries/calendar.html

Link to comment
https://forums.phpfreaks.com/topic/285613-codeigniter-calendar-populating/
Share on other sites

It sounds like you are trying to populate multi-day events into your calendar. CodeIgniter provides no way to span multiple days with a single event, so you would need to determine all of the days that include the start date, end date, and all days in between. Once you know those days, you can add them to the array that sets links for the days.

 

So, lets say you have an event from the DB:

$events = array(
  array(
    'id' => 234235235,
    'start' => '2014-01-02 12:00:00',
    'end' => '2014-01-04 12:00:00'
  )
);

The event here spans three days, the 2nd, 3rd, and 4th. How can you get these dates with PHP? Maybe something like this:

$m = 6;
$y = 2014;

$month_start = mktime( 0,0,0,$m,1,$y);
$num_days = date('t',$month_start);

foreach( $events as $event )
{
    $event_start = strtotime($event['start']);
    $event_end = strtotime($event['end']);

    for( $x=1; $x<=$num_days; $x++ )
    {
        $day_start = mktime( 0,0,0,$m,$x,$y);
        $day_end = mktime( 0,0,0,$m,$x,$y);

        // Does event start on this day?
        if( $event_start >= $day_start && $event_start <= $day_end )
        {
            $data[$x] = 'http://example.com/news/article/' . $y . '/' . $x . '/' . $m;
        }
        
        // Is day in the middle of start and end of event?
        else if( $day_start > $event_start && $day_end < $event_end )
        {
            $data[$x] = 'http://example.com/news/article/' . $y . '/' . $x . '/' . $m;
        }

        // Does the event end on this day?
        else if( $event_end >= $day_start && $event_end <= $day_end )
        {
            $data[$x] = 'http://example.com/news/article/' . $y . '/' . $x . '/' . $m;
        }
    }
}

This is untested code, but shows how to know what days the events are on. The CI calendar class does have an extreme limitation in that it assumes that you may only have one event per day. You'll have a lot of work to do, and you'll spend quite a bit of time extending the calendar class if you want to have multiple events per day. I've done this, so I know how painful it is.

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.