HTML, XHTML and CSS All-In-One for Dummies - Andy Harris [231]
The basic plan is to format the data in a way that it can be read back into variables. Generally, you store all of the data for one form on a single line, and you separate the values on that line with a delimiter, which is simply some character intended to separate data points. Spreadsheets have used this format for many years as a basic way to transport data. In the spreadsheet world, this type of file is called a CSV (for comma-separated values) file. However, the delimiter doesn’t need to be a comma. It can be nearly any character. I typically use a tab character or the pipe (|) symbol because they are unlikely to appear in the data I’m trying to save and load.
Storing data in a CSV file
Here’s how you store data in a CSV file:
1. You can use the same HTML form.
The data is gathered in the same way regardless of the storage mechanism. I did make a new page called addContactCSV.html, but the only difference between this file and the addContact.html page is the action property. I have the two pages send the data to different PHP programs, but everything else is the same.
2. Read the data as normal.
In your PHP program, you begin by pulling data from the previous form.
//read data from form
$lName = $_REQUEST[“lName”];
$fName = $_REQUEST[“fName”];
$email = $_REQUEST[“email”];
$phone = $_REQUEST[“phone”];
3. Store all the data in a single tab-separated line.
Concatenate a large string containing all the data from the form. Place a delimiter (I used the tab symbol \t) between variables, and a newline (\n) at the end.
//generate output for text file
$output = $fName . “\t”;
$output .= $lName . “\t”;
$output .= $email . “\t”;
$output .= $phone . “\n”;
4. Open a file in append mode.
This time, I name the file contacts.csv to help myself remember that the contact form is now stored in a CSV format.
5. Write the data to the file.
The fwrite() function does this job with ease.
6. Close the file.
This part (like most of the program) is identical to the earlier version of the code.
Here’s the code for addContactCSV.php in its entirety:
type = “text/css”
href = “contact.css” />
//read data from form
$lName = $_REQUEST[“lName”];
$fName = $_REQUEST[“fName”];
$email = $_REQUEST[“email”];
$phone = $_REQUEST[“phone”];
//print form results to user
print <<< HERE
Thanks!
Your spam will be arriving shortly.
first name: $fName
last name: $lName
email: $email
phone: $phone
HERE;
//generate output for text file
$output = $fName . “\t”;
$output .= $lName . “\t”;
$output .= $email . “\t”;
$output .= $phone . “\n”;
//open file for output
$fp = fopen(“contacts.csv”, “a”);
//write to the file
fwrite($fp, $output);
fclose($fp);
?>
As you can see, this is not a terribly difficult way to store data.
Viewing CSV data directly
If you look at the resulting file in a plain text editor, it looks like Figure 6-4.
Figure 6-4: The data is separated by tab charac-ters and each entry is on its own line.
Of course, CSV data isn’t meant to be read as plain text. On most operating systems, the .csv file extension is automatically