From the course: PHP: Exporting Data to Files

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Controlling CSV options with the Csv class

Controlling CSV options with the Csv class - PHP Tutorial

From the course: PHP: Exporting Data to Files

Start my 1-month free trial

Controlling CSV options with the Csv class

This is cars_csv.php from the beginning of the chapter. The procedural code on lines 4 to 18 exports the database result to a csv file, but it's rather inflexible. Let's replace it with the csv class which is very similar to the text class. To see how to use it, let's take a look at the class definition. So we need to go to the src folder > foundationphp > Exporter. And it's Csv.php. And if we scroll down in the class definition, we can find the constructor begins on line 64. Like the text class, the constructor accepts three arguments. A database result, the name of the download file and an array of options. The second and third arguments have default values. So the database result is the only required argument. If you don't set a file name, it defaults to download.csv. There are five options all of which have default values so you need to set them only if you want to change them. Local lets you specify that a local file should be created. Local is set to false so the default is to…

Contents