11.07.2015 Views

PHP MySQL - Stilson.net

PHP MySQL - Stilson.net

PHP MySQL - Stilson.net

SHOW MORE
SHOW LESS
  • No tags were found...

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

CHAPTER 10 • WORKING WITH THE FILE AND OPERATING SYSTEMwww.it-ebooks.infoConsider a scenario in which weekly newsletter subscriber data is cached to a file for perusal by themarketing staff. The file might look like this:Jason Gilmore,jason@example.com,614-555-1234Bob Newhart,bob@example.com,510-555-9999Carlene Ribhurt,carlene@example.com,216-555-0987Suppose the marketing department would like an easy way to peruse this list over the Web. This taskis easily accomplished with fgetcsv(). The following example parses the file:// Open the subscribers data file$fh = fopen('/home/www/data/subscribers.csv', 'r');// Break each line of the file into three partswhile (list($name, $email, $phone) = fgetcsv($fh, 1024, ',')) {// Output the data in HTML formatprintf("%s (%s) Tel. %s", $name, $email, $phone);}Note that you don’t necessarily have to use fgetcsv() to parse such files; the file() and list()functions accomplish the job quite nicely. We can revise the preceding example to instead use the latterfunctions:

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!