Welcome Guest, Not a member yet? Register   Sign In
Filling the Calendar class's events via db
#1

[eluser]Leon Stafford[/eluser]
Hi,

I'm hoping to use the calendar class to display the current and next month's event days on a site's front page. Admin will add events to the db via CMS, when calendar is generated, it will check event db and fill array with dates containing events and corresponding links to that day's event page.

Some questions:

How do I dynamically generate the array (syntax wise)?

If I want to cache the top page and don't have access to CRON, what is the best practice way to balance between caching efficiency and ensuring the page is refreshed if the date or other db content has changed? (Can I just call it conditionally via the controller depending on wether the database has changed since last cache?)

Here is my calendar output code so far:

Code:
//get this month and next month's details. ie 17th February, 2009
$days_in_month = date('t'); //ie 28
$todays_date = date('d'); //17
$this_month = date('n'); //2
$next_month = date('n', strtotime('next month')); //3
$this_months_year = date('Y'); //2009
$next_months_year = date('Y', strtotime('next month')); //2009 (if this month is Dec, this will be 2010)

$this_month_events = array(
               3  => 'view/event/'.$this_months_year.'/'.$this_month.'/3',
               7  => 'view/event/'.$this_months_year.'/'.$this_month.'/7',
               13 => 'view/event/'.$this_months_year.'/'.$this_month.'/13',
               26 => 'view/event/'.$this_months_year.'/'.$this_month.'/26'
             );
$next_month_events = array(
               1  => 'view/event/'.$this_months_year.'/'.$this_month.'/1',
               25 => 'view/event/'.$this_months_year.'/'.$this_month.'/25',
               13 => 'view/event/'.$this_months_year.'/'.$this_month.'/13',
               27 => 'view/event/'.$this_months_year.'/'.$this_month.'/27'
             );            

echo $this->calendar->generate($this_months_year,$this_month,$this_month_events);
if ($days_in_month/2 < $todays_date){
    //show next month's date if past the first half of this month
    echo $this->calendar->generate($next_months_year,$next_month,$next_month_events);
    //if adding a next month button, conditionally add it here
}
#2

[eluser]Leon Stafford[/eluser]
For my needs, this is currently working:

Code:
&lt;?php
//get this month and next month's details. ie 17th February, 2009
$days_in_month = date('t'); //ie 28
$todays_date = date('d'); //17
$this_month = date('n'); //2
$next_month = date('n', strtotime('next month')); //3
$this_months_year = date('Y'); //2009
$next_months_year = date('Y', strtotime('next month')); //2009 (if this month is Dec, this will be 2010)

$events_in_database = array( //example of data as stored in events db
            
             0 => array(
                'id' => '0',
                'year' => '2009',
                'month' => '2',
                'day' => '26',
                'event_name' => 'Leons Birthday',
                ),
            1 => array(
                'id' => '1',
                'year' => '2009',
                'month' => '3',
                'day' => '1',
                'event_name' => 'Someone elses Birthday',
                ),
            2 => array(
                'id' => '2',
                'year' => '2009',
                'month' => '2',
                'day' => '27',
                'event_name' => 'Grand Opening',
                ),    
            3 => array( // creating another array with same date is OK for this project
                'id' => '3',
                'year' => '2009',
                'month' => '2',
                'day' => '27',
                'event_name' => 'Different Event',
                ),        
            
            );

$this_month_events = array();
$next_month_events = array();

foreach ($events_in_database as $value) { //create this month's calendar data from db

    if ($value['year'] == $this_months_year && $value['month'] == $this_month)
    {
        $this_month_events[$value['day']] = 'view/event/'.$value['year'].'/'.$value['month'].'/'.$value['day'];
    } else if (    $value['year'] == $next_months_year && $value['month'] == $next_month)
    {
        $next_month_events[$value['day']] = 'view/event/'.$value['year'].'/'.$value['month'].'/'.$value['day'];
    }
}
$bimonthly_events = array(0=>$this_month_events,1=>$next_month_events);
echo '<pre>';
echo print_r($bimonthly_events);    
echo '</pre>';    


            

echo $this->calendar->generate($this_months_year,$this_month,$this_month_events);
if ($days_in_month/2 < $todays_date){
    //show next month's date if past the first half of this month
    echo $this->calendar->generate($next_months_year,$next_month,$next_month_events);
    //if adding a next month button, conditionally add it here
}
?&gt;
#3

[eluser]Leon Stafford[/eluser]
Here is the same thing being pulled from the database in object format:

This is quite easy to do, thanks to CodeIgniter. :cheese:

Code:
&lt;?php

//get this month and next month's details. ie 17th February, 2009
$days_in_month = date('t'); //ie 28
$todays_date = date('d'); //17
$this_month = date('n'); //2
$next_month = date('n', strtotime('next month')); //3
$this_months_year = date('Y'); //2009
$next_months_year = date('Y', strtotime('next month')); //2009 (if this month is Dec, this will be 2010)


$this_month_events = array();
$next_month_events = array();

$this->load->database();
$query = $this->db->query('SELECT * FROM events');

foreach ($query->result() as $row)
{
    if ($row->year == $this_months_year && $row->month == $this_month)
    {
        $this_month_events[$row->day] = 'view/event/'.$row->year.'/'.$row->month.'/'.$row->day;
    } else if (    $row->year == $next_months_year && $row->month == $next_month)
    {
        $next_month_events[$row->day] = 'view/event/'.$row->year.'/'.$row->month.'/'.$row->day;
    }
}
        

echo $this->calendar->generate($this_months_year,$this_month,$this_month_events);
if ($days_in_month/2 < $todays_date){
    //show next month's date if past the first half of this month
    echo $this->calendar->generate($next_months_year,$next_month,$next_month_events);
    //if adding a next month button, conditionally add it here
}
?&gt;
#4

[eluser]Asinox[/eluser]
Im testing ur code, but the calendar don't run to the next month Sad when i try to click to see this.

Thanks
#5

[eluser]Leon Stafford[/eluser]
[quote author="Asinox" date="1236974850"]Im testing ur code, but the calendar don't run to the next month Sad when i try to click to see this.

Thanks[/quote]

Hi,

I don't think I added links to the next month in this code, but I have it set to show this month and next month if your computer's date says it's past the halfway point of this month. You can change your clock to test it.

I have since implemented this into the front page of a website and am using JavaScript to move forwards through time. It will go backwards, but not past the current month. Was having troubles when the previous month was December. It will go forwards, incrementing the months and years fine for as long as you want, but going backwards I still can't get working and don't really need it for the site I'm working on so left it for now.
#6

[eluser]Asinox[/eluser]
Leon ur code is fine Smile is working nice, just i ll try to modify and show next a previous is the user want, but is very fine u code Smile




Theme © iAndrew 2016 - Forum software by © MyBB