I’m trying wrap my head around in what the best way to edit current day comments that I have for a daily calendar. Let me explain what I’m doing first, on any day of the week a user can click on the day and it will jump to a daily calendar consisting of times 6am to 6pm with space to put a comment in that particular day. Kind of like having an online appointment book.
Here’s a rough demo of what I’m talking about: http://www.magiccuckoo.com/bookingCalendar.php?app=September%205,%202014
I have it were it inserts all the times & comments in a database table, here is the script that does that:
[php]<?php
require(‘lib/includes/utilities.inc.php’);
$name = “John Pepp”;
$insertQuery = array();
$insertData = array();
$n = 0;
$theCalendar = (filter_input(INPUT_POST, ‘dailyCalendar’, FILTER_DEFAULT, FILTER_REQUIRE_ARRAY));
$sql = 'INSERT INTO schedule (name, appDate, comments) VALUES ';
foreach ($theCalendar as $innerArray) {
$insertQuery[] = ‘(:name’ . $n . ‘, :appDate’ . $n . ‘, :comments’ . $n . ‘)’;
$insertData[‘name’ . $n] = $name;
foreach ($innerArray as $key => $value) {
if ($key == ‘appDate’) {
$insertData[‘appDate’ . $n] = $value;
}
if ($key == ‘comments’) {
$insertData[‘comments’ . $n] = $value;
}
}
$n += 1;
}
if (!empty($insertQuery)) {
$sql .= implode(’, ', $insertQuery);
$stmt = $pdo->prepare($sql);
$stmt->execute($insertData);
}[/php]
and here’s the method (Function) that generates the form in order to get the data into the database table:
[php] protected function displayTimes($calendarDay) {
/* Set Current Time if it is the Current Day */
self::$currentTime = ($calendarDay == self::currentDay()) ? self::currentTime() : NULL;
/* Display Alpha Day of the Week */
self::$dayofWeek = MyCalendar::displayDay($calendarDay);
/* Human Form of Date ***August 28, 1964*** for example */
self::$headingDay = MyCalendar::dateHumanForm($calendarDay);
$this->myForm = '<section class="">' . "\n" . '<form class="container timeManagementForm" action="processComments.php" method="post">' . "\n";
$this->myForm .= '<fieldset class="timeFieldset">' . "\n" . '<legend class="timeLegend">' . self::$dayofWeek . ', ' . self::$headingDay . '</legend>';
for (self::$counter = 6; self::$counter < 19; self::$counter++) {
/* Display Time for Daily Calendar */
self::$displayTime = ( self::$counter < 0 ) ? MyCalendar::getTimeFormat('0' . self::$counter . ':00') : MyCalendar::getTimeFormat(self::$counter . ':00');
/* MySQL TIMESTAMP Format 0000-00-00 00:00:00 */
self::$mysqlFormat = MyCalendar::mysqlFormat(self::$headingDay . ' ' . self::$displayTime);
/* If the comment time equals the current day's time then set the hightlight class */
self::$hightlightTime = (self::$displayTime == self::$currentTime) ? ' highlightTime' : NULL;
$this->myForm .= '<input type="hidden" name="dailyCalendar[' . self::$counter . '][appDate]" value="' . self::$mysqlFormat . '">' . "\n";
$this->myForm .= '<label for="time' . self::$counter . '" class="span2 appTimeStyle' . self::$hightlightTime . '">' . self::$displayTime . '</label>' . "\n";
$this->myForm .= '<input class="span10" id="time' . self::$counter . '" type="text" name="dailyCalendar[' . self::$counter . '][comments]" value="">' . "\n";
}
$this->myForm .= '<input class="timeSubmitBtn" type="submit" name="submit" value="Submit">' . "\n" . '</fieldset></form>' . "\n" . '</section>' . "\n";
return $this->myForm;
}[/php]
The following is what I trying to wrap my brain around with:
-
I could check to see if that particular day of the week has been enter, if so pull all the comments for the times that have them into the form then instead of going to a insert method (I’m going to eventually write that into a class) go to an update method. That to me makes it more logical, but at the same time might make the script (code) look awkward and messy.
-
I could simply read the data in, display comments in the appropriate time slots and then when it goes out to be saved simply delete the information first and then just re-insert the data into the database table. The only potential problem that might happen is existing time comments accidentally getting changed?
I’m leaning towards number 2 way of doing it than number 1 way of doing it. I just want other opinions in which way you would go about doing this?