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 - PHP Tutorial
From the course: PHP: Exporting Data to Files
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…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.
Contents
-
-
-
-
(Locked)
Outputting the database result to a text file6m 32s
-
(Locked)
Outputting the result as a CSV file6m 53s
-
(Locked)
Introducing the Base class for file downloads4m 37s
-
(Locked)
Using the Text class for greater control over output7m 20s
-
(Locked)
Controlling CSV options with the Csv class6m 49s
-
(Locked)
Saving the data to a local file6m 36s
-
(Locked)
-
-
-
-
-
-