Create a CSV file.

Takes arrays and turns them into CSV files.

package dstruct_presentation
author David
todo Accept recordset objects like DataTable?

 Methods

Class constructor.

__construct(array $dataset) : string

Accepts multi-dimensional array suitable for addRows() and will return a csv output. Quick way of generating a csv, but not suitable if you want to change default field enclosure character, add headers etc.

Parameters

$dataset

array

Returns

stringCSV formatted string.

Add headers to the CSV.

addHeaders($headers) 

Parameters

$headers

array Header titles from left to right

Exceptions

\DStructGeneralException

Add a row to the table.

addRow(array $arraydata) 

The array will be added as a row to the end of the current data. Method does not check that the array has the correct number of items. This is expected behaviour.

Parameters

$arraydata

array

Flat array of data.

Exceptions

\DStructGeneralException

Add multiple rows of data.

addRows(array $data) 

Expects a multi-dimensional array of data to output as a csv.

Parameters

$data

array

Multi-dimensional array of data.

Exceptions

\DStructGeneralException

Clear the object.

clear() 

Removes all data, counters and header info.

Get the number of headers set.

getHeaderCount() : integer

Returns

integer

Set the character to enclose fields with.

setEnclosedBy(string $by) 

Default is nothing - fields will not be enclosed. Common setting would be double quotes. Must be set before any data or headers are added.

Parameters

$by

string

Exceptions

\DStructGeneralException

Set the character to escape fields with.

setEscapeCharacter(string $char) 

Default is \ (backslash). Must be set before any data or headers are added.

Parameters

$char

string

Exceptions

\DStructGeneralException

Set the line ending.

setLineEnding(string $ending) 

Default is windows style \r\n. You must set the string using double quotes! e.g.

$csvout->setLineEnding("\n"); // works!
$csvout->setLineEnding('\n'); // doesn't work!

Parameters

$ending

string

Exceptions

\DStructGeneralException

Set the field separator.

setSeparator(string $sep) 

Default is a comma. Must be set before any headers or data.

Parameters

$sep

string

Exceptions

\DStructGeneralException

Output the CSV string.

write(string $name, string $dest) : string

Can either stream to browser as csv, as a download, output a string for use in the script, or save as a local file. Note that the browser and download options can only be used if their has been no output from the script yet as they send headers.

Parameters

$name

string

If streaming, downloading or saving can set name. Will default to export.csv if not given.

$dest

string

Output destination. See notes and class constants.

Exceptions

\DStructGeneralException

Returns

string

Add an element to the array.

addElement(string $element) 

Parameters

$element

string

End a line of the CSV.

endLine() 

 Properties

 

$buffer : string
 

$enclosedby : string
 

$escape : string
 

$headercount : integer
 

$line : string
 

$separator : string

 Constants

 

Export to browser

EXPORT_BROWSER : string
 

Export to download

EXPORT_DOWNLOAD : string
 

Export to file

EXPORT_FILE : string
 

Export to string

EXPORT_STRING : string