Calendar data retrieval

j9cj9c
edited October 2015 in Bug Reports
Hello! I don't know if anyone has found this. I've tried searching around the site but I did not see any reports so I opted to post about it myself.

If you are using Codeigniter's Calendar class that retrieves data from the database, be wary of the day you are inputting. If you are like me who used this tutorial to get started, you might encounter the same problem I did. Typically in MySQL, the date format it accepts is 'Y-m-d' or '2015-09-25'. In the tutorial, it teaches you to get the substring of the date. So in '2015-09-25', you are to use only the day '25' with $day = substr(<date from database>,8,2) and input it as $data[$day] = <data from database> and pass that to CI's calendar class. It worked fine for me but I noticed the days that are less than 10 and have contents were not showing data like all other days do. I looked into CI's calendar class and found the tiny bug. During this part:
if (isset($data[$day])) { // Cells with content $temp = ($is_current_month == TRUE AND $day == $cur_day) ? $this->temp['cal_cell_content_today'] : $this->temp['cal_cell_content']; $out .= str_replace('{day}', $day, str_replace('{content}', $data[$day], $temp)); } else { // Cells with no content $temp = ($is_current_month == TRUE AND $day == $cur_day) ? $this->temp['cal_cell_no_content_today'] : $this->temp['cal_cell_no_content']; $out .= str_replace('{day}', $day, str_replace('{class}', $class, $temp)); }
That chunk of code is inside a for loop with $day counting up from 1 to 30 or 31 depending on the month. What that code is trying to do is check the array $data at index $day with $day being an int. From 1 to 9, $day is valued 1, 2, 3, and so on. However, when you retrieve data from the database and added that data into an array, since you used SQL's format where there would be a leading zero, you are actually putting it into index 01, 02, 03, and so on. A quick fix for this is an if statement before that chunk of code.
if($day < 10) { $dayString = '0' . strval($day); } else { $dayString = strval($day); }
and replace $day with dayString
if (isset($data[$dayString])) { // Cells with content $temp = ($is_current_month == TRUE AND $day == $cur_day) ? $this->temp['cal_cell_content_today'] : $this->temp['cal_cell_content']; $out .= str_replace('{day}', $day, str_replace('{content}', $data[$dayString], $temp)); } else { // Cells with no content $temp = ($is_current_month == TRUE AND $day == $cur_day) ? $this->temp['cal_cell_no_content_today'] : $this->temp['cal_cell_no_content']; $out .= str_replace('{day}', $day, str_replace('{class}', $class, $temp)); }
I hope this helps someone.
Sign In or Register to comment.