rhelo Posted May 19, 2019 Share Posted May 19, 2019 Hello, I found this great free php calandar script here "" by Xu and Alessandro, and I'm trying to modify the code to mark several dates(birthdays YYYY-mm-dd) from a date column in a sql database. I've played around with the _showDay() method but I can't seem to get it working.. I've also tried to create a _showBirthday() method to modify the css as well but with no luck. What I'm trying to do is loop through the database to mark the respective dates on the calendar, and have a href to display a new page with the person's name when I click the specific date. Can anyone help with this. Thanks in advance! Quote Link to comment Share on other sites More sharing options...
ginerjm Posted May 19, 2019 Share Posted May 19, 2019 help with what? Quote Link to comment Share on other sites More sharing options...
rhelo Posted May 19, 2019 Author Share Posted May 19, 2019 Hi ginerjm… thanks for your response. I’m trying to use a loop to mark the first 5 days using $k). I modified the public function show() like this: (I added " for ($k = 1; $k <= 5; $k++) { $content .= $this->_showBirthdays($i * 7 + $j, $k); } ") and also created the _showBirthdays() like this: (" private function _showBirthdays($cellNumber, $k) { if ($this->currentDay == 0) { $firstDayOfTheWeek = date(‘N’, strtotime($this->currentYear . ‘-’ . $this->currentMonth . ‘-01’)); if (intval($cellNumber) == intval($firstDayOfTheWeek)) { $this->currentDay = 1; } } if (($this->currentDay != 0) && ($this->currentDay <= $this->daysInMonth)) { $this->currentDate = date(‘Y-m-d’, strtotime($this->currentYear . ‘-’ . $this->currentMonth . ‘-’ . ($this->currentDay))); $cellContent = $this->currentDay; $this->currentDay++; } else { $this->currentDate = null; $cellContent = null; } $today_day = $k;//date(“d”); $today_mon = date(“m”); $today_yea = date(“Y”); $class_day = ($cellContent == $today_day && $this->currentMonth == $today_mon && $this->currentYear == $today_yea ? “calendar_today” : “calendar_days”); return ‘ ’ . $cellContent . ‘ ’ . “\r\n”; }") but the new loop I created seems to do everything 5 times (especially print the entire calendar 5x) My Code: public function show() { $year = null; $month = null; if (null == $year && isset($_GET[‘year’])) { $year = htmlentities($_GET[‘year’], ENT_QUOTES); } elseif (null == $year) { $year = date(“Y”, time()); } if ((!is_numeric($year)) || ($year == “”)) { $year = date(“Y”, time()); } if (null == $month && isset($_GET[‘month’])) { $month = htmlentities($_GET[‘month’], ENT_QUOTES); } elseif (null == $month) { $month = date(“m”, time()); } if ((!is_numeric($month)) || ($month == “”)) { $month = date(“m”, time()); } $this->currentYear = $year; $this->currentMonth = $month; $this->daysInMonth = $this->_daysInMonth($month, $year); $content = ‘ ’ . “\r\n” . ‘ ’ . “\r\n” . $this->_createNavi() . “\r\n” . ‘ ’ . “\r\n” . ‘ ’ . “\r\n” . ‘ ’ . “\r\n” . $this->_createLabels() . ‘ ’ . “\r\n”; $content .= ‘ ’ . “\r\n”; $content .= ‘ ’ . “\r\n”; $weeksInMonth = $this->_weeksInMonth($month, $year); // Create weeks in a month for ($i = 0; $i < $weeksInMonth; $i++) { // Create days in a week for ($j = 1; $j <= 7; $j++) { //$content .= $this->_showDay($i * 7 + $j); for ($k = 1; $k <= 5; $k++) { $content .= $this->_showBirthdays($i * 7 + $j, $k); } } } $content .= '</div>' . "\r\n"; $content .= '<div class="calendar_clear"></div>' . "\r\n"; $content .= '</div>' . "\r\n"; $content .= '</div>' . "\r\n"; return $content; } Quote Link to comment Share on other sites More sharing options...
mac_gyver Posted May 19, 2019 Share Posted May 19, 2019 you would initially create an array of the dates (Y-m-d date format) you want to highlight and store this array in a class property. at the point in the code where it is producing the css class selector and the cell content, you would use in_array() to find if $this->currentDate is one of the dates in the array. note: the author of that script should have produced a $today value in the Y-m-d format and just directly compared it with $this->currentDate in the existing css class selector code. 1 Quote Link to comment Share on other sites More sharing options...
rhelo Posted May 19, 2019 Author Share Posted May 19, 2019 If I'm able to sucessfully mark the first 5days, then I can mark any number of dates from a database. Thanks again! Quote Link to comment Share on other sites More sharing options...
rhelo Posted May 19, 2019 Author Share Posted May 19, 2019 Thanks mac_gyver, that makes a lot of sense. Im trying to do that now Quote Link to comment Share on other sites More sharing options...
ginerjm Posted May 19, 2019 Share Posted May 19, 2019 While I am not able to contribute to your problem solution (as Macgiver has done!) I do want to help you understand code. This: $year = null; $month = null; if (null == $year && isset($_GET[‘year’])) { $year = htmlentities($_GET[‘year’], ENT_QUOTES); } elseif (null == $year) { $year = date(“Y”, time()); } code is rather redundant. Why would one write an IF statement to check for a null value in the $year variable when one has JUST SET IT to null? And again, why test for a value of null in the else clause when it is already known to be null if you didn't just re-set it to a value from the $_GET array? Quote Link to comment Share on other sites More sharing options...
rhelo Posted May 19, 2019 Author Share Posted May 19, 2019 @mac_gyver Thanks again, im getting closer.. (but before I create the dates[] like you suggested), I want to just create a loop to mark the 1st 7days for instance Here's what I did (don't laugh at my output) $mydays = null; for ($k = 1; $k <= 7; $k++) { if ($cellContent == $k && $this->currentMonth == $today_mon && $this->currentYear == $today_yea) { $class_day = "calendar_today"; } else { $class_day = "calendar_days"; } $mydays .= '<div class="' . $class_day . '">' . $cellContent . '</div>';// . "\r\n"; } return $mydays . "\r\n"; Quote Link to comment Share on other sites More sharing options...
mac_gyver Posted May 19, 2019 Share Posted May 19, 2019 the code in the _showDay() method is for producing a single cell. you would not add any loop to it. see the following logic for the end of the _showDay() method - // hard way of testing if the day being displayed is today $today_day = date("d"); $today_mon = date("m"); $today_yea = date("Y"); $class_day = ($cellContent == $today_day && $this->currentMonth == $today_mon && $this->currentYear == $today_yea ? "calendar_today" : "calendar_days"); // easy way of testing if the day being displayed is today $today = date('Y-m-d'); $class_day = $this->currentDate == $today ? "calendar_today" : "calendar_days"; // the above logic sets the $class_day to highlight 'today' and $cellContent if the day number should be displayed (currentDate is between the start and end of the month) // to add highlighting or different cellContent do that here if(in_array($this->currentDate, $this->highlight_dates)) { $class_day = "calendar_today"; // uses the existing 'today' css. change as needed $cellContent = ""; // produce the desired content (link) as needed } return '<div class="' . $class_day . '">' . $cellContent . '</div>' . "\r\n"; you would define a class property highlight_dates or a name of your choice as an empty array. if the array is left empty, the code works as is. you would then just assign an array of dates to the hightlight_dates property to cause the additional logic to do its thing. while you can make this array of values just the day number, here are some reasons to use a full date - you may have an array of holidays or company days off for the entire year or these dates may be event dates. you can just put these values into the array and the code will work. so, for your example of birthdays, you would just need to use the current year in the birth date values to come up with the full date values. 1 Quote Link to comment Share on other sites More sharing options...
rhelo Posted May 19, 2019 Author Share Posted May 19, 2019 WOW!! I just got it working like a charm.. Thank you @mac_gyver -for taking your time to provide such a detailed explanation. You've really assisted me (and future others) a whole lot! Your post was 100% helpful, that's exactly what I needed to do. Now populating the highlight_dates[] from the database is a piece of ?. ??? Quote Link to comment Share on other sites More sharing options...
rhelo Posted May 19, 2019 Author Share Posted May 19, 2019 Nuice! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.