简体   繁体   中英

How to structure database and populate Calendar in CodeIgniter 2 project?

I've searched SO and Google and can't quite find what I need.

I'm building a simple Event Calendar with CodeIgniter 2. As per the CodeIgniter documentation, this is the basic code structure and it's working...

// $data[2] contains the data for day #2, etc.

$data[2] = '<a href="#">event title 1</a>';
$data[8] = '<a href="#">event title 2</a><br/><a href="#">event title 3</a>';
$data[13] = '<a href="#">event title</a>';
$data[24] = '<a href="#">event title</a>';

// {content} in template is where $data is inserted for each day

$prefs = array (
    // my calendar options
    'template'  => '
        {cal_cell_content}{day}<br/>{content}{/cal_cell_content}
        {cal_cell_content_today}<div class="highlight">{day}<br/>{content}</div>{/cal_cell_content_today}'
);

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

$year   = ($year === FALSE ? date('Y') : $year);
$month  = ($month === FALSE ? date('m') : $month);

echo $this->calendar->generate($year, $month, $data);

Now comes how I've set up my database table...

  • Each Event has a title field and that creates the $slug . ( ~/events/view/title-slug )

  • Each Event has a date field and the data format is mm/dd/yyyy

Now, I'm thinking about how I'd query the database for a particular month/year and extract the data to insert into each $data[] variable.

It seems like I'll need to do the following:

  • create new columns in my database table for month , day , and year .

  • take my date input data, after it's validated, and save it into date column.

  • split apart my date input data, and save each piece into month , day , and year columns.

Then I would simply query my database for year & month , then loop through these results to construct my $data[] array for each day .

Is this the correct way I should be approaching this problem? It seems very redundant to have a date column as well as month , day , and year columns. Can it be done with only the date ( mm/dd/yyyy ) column? Too simple? Too complex? I'd like to avoid giving the user more than one field for entering a date, and ultimately I'll have a jQuery date-picker to help ensure the proper data format.

I know this may seem like a simple problem, but I've failed to locate simple code examples online. Most of the ones I've found are out of date (CI instead of CI2), too complex for what I'm doing, or use daily content items which have URI segments that already contain the date ( ~/events/view/yyyy/mm/dd ).


EDIT :

This is how my Model is presently setup:

return $this->db->get_where('events', array('yyyy' => $year, 'mm' => $month))->result_array();

You can leave everything as-is and just structure your query to return the month and year as separate columns:

in SQL:

SELECT id, 
       title, 
       description, 
       MONTH(date_field) as event_month, 
       YEAR(date_field) as event_year 
FROM my_table
... etc ...

and in Active Record ( taken from here ):

$this->db->select('id');
$this->db->select('title');
$this->db->select('description');
$this->db->select("MONTH(date_field) AS event_month");
$this->db->select("YEAR(date_field) AS event_year");
$query = $this->db->get('my_table');
$results = $query->result();

Firstly, I changed my date column into the MySQL "date" format, yyyy-mm-dd , in order to take advantage of the built-in MySQL date functions .

My original $query is as follows, where the 'yyyy' and 'mm' are my redundant "year" and "month" columns:

$this->db->get_where('events', array('yyyy' => $year, 'mm' => $month))->result_array();

I simply changed it into the following using the built-in MySQL date functions:

$this->db->get_where('events', array('YEAR(date)' => $year, 'MONTH(date)' => $month))->result_array();

This solved the question. Now I no longer need to maintain the extra columns for "year" and "month".


Additionally, I can assign the "day" portion of the date column to a virtual column called dd by using the MySQL "alias" function:

$this->db->select('DAY(date) AS dd');

However, this would fail since it over-rides CI's default select('*') . So you'll have to select() all the relevant columns first or it will give an error:

$this->db->select('*');
$this->db->select('DAY(date) AS dd');

And putting it all together in the Model:

// select all relevant columns
$this->db->select('*');

/* use MySQL date functions to creates virtual column called 'dd' containing
"day" portion of the real 'date' column. */
$this->db->select('DAY(date) AS dd'); 

/* use MySQL date functions to match $year and $month to "year" and "month"
portions of real 'date' column. */  
return $this->db->get_where('events', array('YEAR(date)' => $year, 'MONTH(date)' => $month))->result_array();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM