This project has two components: a text file we use as database and a HTML/PHP page.
For simplicity I separated the files into HTML and PHP.
Text file: data.txt
PHP engine: databaseAction.php
Note:the HTML and PHP file can be combined in one single PHP file that calls itself with the use of the $_SERVER['PHP_SELF'] variable (this is what I did in the live demo).
###How it works
The HTML page calls the PHP script to manipulate the text database. Simple and effective.
The actions we can perform on the database are the following:
Add a new record (sequentially)
Empty the database
Display all the records in the database (all the lines in the text file) with the option to delete a selected line
Be able to backup the database when needed (not present in the live demo)
The record added will be in the form of timestamp,note, for example:
Wednesday Jun 11 2014 20:54:54,first record Thursday Jun 12 2014 22:07:55,second record Thursday Jun 12 2014 21:08:14,third record
The HTML source code:
The PHP file that performs the selected actions to the file is quite simple. I use the Switch/Case control structure to select what operation to perform.
The fopen function allows you to read/write/append to a text file through a file handler variable (in our case the variable $f
I use the ftruncate function to clear the file. This function truncates a file to a given length (in this case 0
Very interesting is the cutline function on line 57. What that
function does is delete the line in the file with a specific number.
I found the function on some PHP help website I shamelessly copied it. It works great (first rule of programming: never reinvent the wheel, use what’s out there).
To display the records I use the PHP function explode to split the record at the comma and show the timestamp and the note separately for an easier read