A Useful Event Calendar Written In PHP - Adding an event
(Page 5 of 6 )
If a particular day is selected, then we also show a form to add a new event to the current day under the list of events. The form is just basic HTML and looks like this:
When the new event form is submitted, the AddPost function is called with the selected day, month and year, as well as the name and details of the post:
function AddPost($Day, $Month, $Year, $Name, $Desc) AddPost simply opens the events file and output the details of the new post, like this:
// Add this event to the events file
$fp = @fopen($_SERVER["DOCUMENT_ROOT"] . "/" . EVENT_FILE, "a")
or die("<span class='error'>ERROR: Couldn't open events file to write event.</span>");
@fputs($fp, "$Month $Day $Year\r\n$Name\r\n$Desc\r\n")
or die("<span class='error'>ERROR: Couldn't write to events file.</span>");
@fclose($fp); Deleting an event As you may've noticed in the screenshot above, each event is displayed with a "Remove" link beneath it. This link calls cal.php with the details of the post to be deleted.
The DelPost function handles the deletion of an event, but because we're working with a file, we read all events in and then only write-out the ones that aren't marked for deletion by calling the AddPost function. We do this by comparing the selected event with all events in the events file:
for($i = 0; $i < sizeof($arrEvents); $i+=3)
{
if(!($arrEvents[$i] == $EventDate && $arrEvents[$i+1] == $Name && $arrEvents[$i+2] == $Desc))
{
// This entry hasn't been chosen to be deleted.
// We will add it back to the events
if($arrEvents[$i+1] != "" && $arrEvents[$i+2] != "")
{
// Start by getting the date of the post
$thisDate = explode(" ", $arrEvents[$i]);
AddPost($thisDate[1], $thisDate[0], $thisDate[2], $arrEvents[$i+1], $arrEvents[$i+2]);
}
}
}Next: Conclusion >>
More PHP Articles
More By Mitchell Harper
|
| · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | | |
|