linked to the default spreadsheet program. If you double-click the file, it will open in your spreadsheet, which will look something like Figure 6-5.
This is an easy way to store large amounts of data because you can use the spreadsheet to manipulate the data. Of course, relational databases (described in Book VI) are even better, but this is a very easy approach for relatively simple data sets. I’ve built many data entry systems by using this general approach.
Figure 6-5: Most spread-sheets can read CSV data directly.
Reading the CSV data in PHP
Of course, you may also want to read in the CSV data yourself. It’s not too difficult to do. The following code for readContactCSV.php:
readContactCSV.phpContacts
print <<< HERE
| First | Last | email | phone |
|---|
HERE;
$data = file(”contacts.csv”);
foreach ($data as $line){
$lineArray = explode(”\t”, $line);
list($fName, $lName, $email, $phone) = $lineArray;
print <<< HERE
| $fName | $lName | $email | $phone |
HERE;
} // end foreach
//print the bottom of the table
print ”
\n”;
?>
Figure 6-6 shows this program in action.
Figure 6-6: This program creates an HTML table from the data in the file.
In this program, I read the contents of a CSV file and display it in an HTML table. It’s not terribly different than reading any other text file, but there are some new twists.
1. Print the table heading.
It’s easiest to manually print out the table heading with the field names, because I know what they’ll be. A simple heredoc will do the job.
print <<< HERE
| First | Last | email | phone |
|---|
HERE;
2. Load the data into an array.
PHP has a marvelous tool called file. This function takes a filename as its only input. It then opens that file and places all the contents in an array, placing each line in its own element of the array. There’s no need to make a file pointer, or to open or close the file. In this example, I load all the contents of contacts.csv into an array called $data.
$data = file(“contacts.csv”);
3. Use a foreach loop to step through the contents.
Now I can walk through the contents of the file with a simple foreach loop. I place the current line in a variable called (wait for it . . .) $line.
foreach ($data as $line){
4. Explode each line into its own array.
You have got to love a function with a violent name, especially when it’s really useful. Use the explode command to separate the line into its component parts. For this example, I break on the tab (\t) character because that’s the delimiter I used when storing the file.
$lineArray = explode(“\t”, $line);
5. Use the list() function to store each element of the array into its own variable.
I could just use the array, but I think it’s easier to pass the data back to the same variable names I used when creating the program. The list() construct does exactly that. Feed it a bunch of variable names and assign an array to it, and now each element of the array will be assigned to the corresponding variable.
list($fName, $lName, $email, $phone) = $lineArray;
6. Print the variables in an HTML table row.
All the variables fit well in an HTML table, so just print out the current row of the table.
print <<< HERE
| $fName | $lName | $email | $phone |
HERE;
7. Clean up your playthings.
There’s a little housekeeping to do. Finish the loop and close up the HTML table. There’s no need to close the file because that was automatically done by the file()
®Online Book Reader