The Best Way of Adding and Editing Daily Comments

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:

  1. 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.

  2. 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?

Strider,

I might be confused on what you are asking, but, I always say simple is always best in programming.
So, if it is an appointment database, you would enter each appointment as a separate table entry and
if a user adds one or deletes one it only has to do with the one table entry and nothing else.

Then, displaying would be handled in the query to pull out the data for the one hour being displayed,
on hour at a time. (Just a query sorted by date, hour and appointments inside that one hour period.)

I feel there is really no reason to overthink this as you would just want to use the table queries instead
of thinking of a way to save all of one group of appointments combined into one record…

Or, perhaps I am not understanding your question…

The problem I’m having is the daily appointments are dynamically created, by that I mean the calendar itself is dynamically created. For example, December 2015 calendar isn’t physically stored, it’s generated every time a user goes to it. That means the individual times won’t be physically stored in the database table until the user(s) click submit for that particular day.

I think I’m getting my head wrapped around it, here’s some pseudo code of what I am trying to do:

Check to see if any rows are return

if rows are return then put the values in the corresponding rows and generate the form

else just create the form and save the data when user click save

{ This is the part I’m a little confused on }
Delete the data for that particular, re-save the data
or
would it better just to update the data for that particular day?

Then repeat redirect the user back to the first step in both cases.

I’m leaning in just updating the data for deleting the data can lead to problems of data being lost and less secure, specially if multiple users are allowed. I’m basically answering my own question…lol ;D

LOL, I answer my questions all the time. Sometimes correctly, too…

Well, anyways, how can you create a calendar for Dec 2015 if you do not save it somewhere?

Also, when you say it is “dynamically” created, I assume you mean locally dynamically created when the
user selects Dec-2015, it shows that calendar. So, the data is pulled from the database for all of the days
in Dec-2015 and displayed. If multiple users load that month’s calendar, it would just pull the same data
and dynamically create the displays. Correct?

So, why would that be less secure? You don’t change anything unless the user adds more info to the
displayed calendar. Not sure what you mean by “Dec 2015 calendar isn’t physically stored”. It must be
if you display it… Guess I am still not clear on your problem…

The calendar month is created each time the calendar is accessed and is dumped on the screen with the corresponding in holidays:

Here’s a small snippet of code what I’m talking about:

[php] private function tabulateForm($date) {

self::$currentDay = date('F j, Y');

self::$displayYear = self::displayYear($date);  // Pull out the year for date string:
$this->prevMonth = self::numericDay($date); // The Numeric Day of the week (Example Wednesday is 3 [Sunday = 0] ):
$this->calcFutureDays = 6 - self::numericDay(self::displayMonth($date) . ' ' . self::daysInMonth($date) . ', ' . self::displayYear($date));

/* Calculate the previous number of days from previous month for filler and       */
/* day's location. Then put previous month's days in the array. If the day        */
/* is Sunday then the previous month's day value would be 0 (No Action Taken).    */
for ($this->y = $this->prevMonth; $this->y > 0; $this->y--) {
  $this->monthyCalendar[] = '<td class="smallText">' . $this->previousMonth($date, $this->y) . '</td>';
} // End of Previous Month Filler Days:

/* Since we know the last location of previous month's day location in the week, */
/* we don't have to figure out the location for the current month, we just have  */
/* to add it on to the existing array.                                           */
for ($this->x = 1; $this->x <= self::daysInMonth($date); $this->x++) {

  /* Put grab the Month, Day and Year in ( Month Day, Year ) format  */
  self::$urldate = self::displayMonth($date) . ' ' . $this->x . ', ' . self::displayYear($date);

  self::$highlightToday = self::compareDates(self::$urldate);
  
  self::$holidayText = self::checkForHoliday(self::$displayYear, self::$urldate);
  
  $this->monthyCalendar[] = '<td><a class="calText' . self::$highlightToday . '" href="bookingCalendar.php?app=' . self::$urldate . '">' . $this->x . '<span class="holidayPosition">' . self::$holidayText . '</span></a></td>';
} // End of Current Calendar Days:

/* If there are any blank days at the end of calendar then put days from next    */
/* month into the existing array. This should complete this array.               */
for ($this->a = 1; $this->a <= $this->calcFutureDays; $this->a++) {
  $this->monthyCalendar[] = '<td class="smallText">' . $this->a . '</td>';
} // End of Futher Month Filler Days:
/* Put Single Dimensional Array ($monthyCaller) into Two Dimensional Array */
$this->displayCalendar = $this->twoDimensional($this->monthyCalendar);

} // End of tabulateForm function:[/php]

Oh, Here’s a very basic beta version of the calendar: http://www.magiccuckoo.com/

Magic Cuckoo… LOL I love internet domain names…

Nice looking and, well, pretty… I like it. I tried to enter a date and no way back to the calendar.

So, did you figure out how you are going to store the info? I still say to enter each item as a separate
DB row. Then, when you draw the one day of the month’s square, load it using a query to pull all the
items in the DB for that one date.

Just curious how you are planning on coding it… Let us know when you do…

Well, I got it to add and update, now I just have add the polishing touches to it. I haven’t yet posted the updates to the website, but I want to have some kind of login system. I hoping to use Facebook and maybe Twitter as a way to login. Though I start with my own login system for now, for I least know how to get that working. I plan on having a sidebar navigation to the site, so people can jump back to the calendar, have where people can scroll to different days just like the monthly calender and have the main calendar show some how when a particular day has an entry made to it. In the near future, I’m going to make it responsive, so the calendar will fit to a mobile phone or tablet. I plan on adding JQuery to the the mix to give some bells and whistles, plus utilizing Ajax so the page doesn’t refresh every time there data added or updated. All done in OOP so that the code will be very portable and the calendar can be used like a small php calendar library. For example here is my home page for the calendar:

[php]$pageTitle = “Graphical Calendar”;

require(‘lib/includes/utilities.inc.php’);
/* This makes sures that the input is a number */
$page = filter_input(INPUT_GET, ‘page’, FILTER_VALIDATE_INT);
if (!$page) {
$page = 0;
}

/* Set the Calendar Controls to the correct page /
$setMonth = new CalendarControls($page);
/
Grab the previous or next month */
$todaysDate = $setMonth->returnDate;

/* This is the actual calendar for you could subsitute /
/
$todaysDate with you own script in figuring out /
/
today’s date or what have you */
$month = new MyCalendar($todaysDate);

/* Grab the corresponding picture number for the calendar month */
$numericMonth = MyCalendar::numericLeadingZeroMonth($todaysDate);
include(‘lib/includes/header.inc.php’);
?>

<?php echo '
' . "\n"; echo '' . "\n"; echo '
' . "\n";

/* OUTPUT CSS STYLED CALENDAR /
echo $month->generateForm;
/
OUTPUT CSS STYLED CALENDAR BUTTONS */
echo $setMonth->returnControls;
?>

©2014 John R. Pepp

[/php]

It still have a few rough edges for the code isn’t optimized, but as you can see it’s pretty compact.

Yes, it looks nice and I am sure someone could use it.

I do not do much site work that needs oop types of code. But, I see where it would be very handy
as an add-in library for someone to add to their site. Perhaps you should put it into a full tutorial
format and post it…

Well, good luck with finishing it up…

Sponsor our Newsletter | Privacy Policy | Terms of Service