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

Edited by Danny620
Link to comment
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.

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.