mirror of
https://github.com/parsecsv/parsecsv-for-php.git
synced 2026-02-19 00:36:38 +00:00
Compare commits
6 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
fc22bee7ca | ||
|
|
9b64cb07c4 | ||
|
|
2bfa3de220 | ||
|
|
c522cd87a7 | ||
|
|
0395362d66 | ||
|
|
20a6a9d1b1 |
@@ -1,3 +1,54 @@
|
||||
parseCSV 0.4.3 beta
|
||||
-----------------------------------
|
||||
Date: 1-July-2008
|
||||
|
||||
- Issue #4. Added an option for setting sorting
|
||||
type behavior when sorting data.
|
||||
Simply set $csv->sort_type to "regular", "numeric",
|
||||
or "string".
|
||||
|
||||
- Issue #6. Raw loaded file data is now cleared from
|
||||
file_data property when it has been successfully
|
||||
parsed to keep parseCSV's memory footprint to a
|
||||
minimum. Specifically handy when using mulitple
|
||||
instances of parseCSV to process large files.
|
||||
|
||||
-----------------------------------
|
||||
|
||||
|
||||
parseCSV 0.4.2 beta
|
||||
-----------------------------------
|
||||
Date: 31-May-2008
|
||||
|
||||
- IMPORTANT! If you're using the output(),
|
||||
method please note that the first parameter
|
||||
has been completely removed as it was
|
||||
technically just useless. Instead, the second
|
||||
parameter (filename) doubles as its replacement.
|
||||
Simply put, if filename is not set or null, the
|
||||
output() method will not output a downloadable
|
||||
file. Please update your existing code
|
||||
when using 0.4.2 and later :)
|
||||
|
||||
- Small fix to the headers sent by the output()
|
||||
method.
|
||||
|
||||
- Added a download example using the output()
|
||||
method to the examples folder.
|
||||
|
||||
-----------------------------------
|
||||
|
||||
|
||||
parseCSV 0.4.1 beta
|
||||
-----------------------------------
|
||||
Date: 29-May-2008
|
||||
|
||||
- Fixed a small bug in how the output() method
|
||||
handles input data.
|
||||
|
||||
-----------------------------------
|
||||
|
||||
|
||||
parseCSV 0.4 beta
|
||||
-----------------------------------
|
||||
Date: 11-Apr-2008
|
||||
|
||||
34
examples/download.php
Normal file
34
examples/download.php
Normal file
@@ -0,0 +1,34 @@
|
||||
<?php
|
||||
|
||||
|
||||
# include parseCSV class.
|
||||
require_once('../parsecsv.lib.php');
|
||||
|
||||
|
||||
# create new parseCSV object.
|
||||
$csv = new parseCSV();
|
||||
|
||||
|
||||
# Parse '_books.csv' using automatic delimiter detection...
|
||||
$csv->auto('_books.csv');
|
||||
|
||||
# ...or if you know the delimiter, set the delimiter character
|
||||
# if its not the default comma...
|
||||
// $csv->delimiter = "\t"; # tab delimited
|
||||
|
||||
# ...and then use the parse() function.
|
||||
// $csv->parse('_books.csv');
|
||||
|
||||
# now we have data in $csv->data, at which point we can modify
|
||||
# it to our hearts content, like removing the last item...
|
||||
array_pop($csv->data);
|
||||
|
||||
# then we output the file to the browser as a downloadable file...
|
||||
$csv->output('books.csv');
|
||||
# ...when the first parameter is given and is not null, the
|
||||
# output method will itself send the correct headers and the
|
||||
# data to download the output as a CSV file. if it's not set
|
||||
# or is set to null, output will only return the generated CSV
|
||||
# output data, and will not output to the browser itself.
|
||||
|
||||
?>
|
||||
@@ -4,7 +4,7 @@ class parseCSV {
|
||||
|
||||
/*
|
||||
|
||||
Class: parseCSV v0.4 beta
|
||||
Class: parseCSV v0.4.3 beta
|
||||
http://code.google.com/p/parsecsv-for-php/
|
||||
|
||||
|
||||
@@ -94,6 +94,12 @@ class parseCSV {
|
||||
var $sort_by = null;
|
||||
var $sort_reverse = false;
|
||||
|
||||
# sort behavior passed to ksort/krsort functions
|
||||
# regular = SORT_REGULAR
|
||||
# numeric = SORT_NUMERIC
|
||||
# string = SORT_STRING
|
||||
var $sort_type = null;
|
||||
|
||||
# delimiter (comma) and enclosure (double quote)
|
||||
var $delimiter = ',';
|
||||
var $enclosure = '"';
|
||||
@@ -129,6 +135,8 @@ class parseCSV {
|
||||
var $output_delimiter = ',';
|
||||
var $output_filename = 'data.csv';
|
||||
|
||||
# keep raw file data in memory after successful parsing (useful for debugging)
|
||||
var $keep_file_data = false;
|
||||
|
||||
/**
|
||||
* Internal variables
|
||||
@@ -216,20 +224,19 @@ class parseCSV {
|
||||
|
||||
/**
|
||||
* Generate CSV based string for output
|
||||
* @param output if true, prints headers and strings to browser
|
||||
* @param filename filename sent to browser in headers if output is true
|
||||
* @param filename if specified, headers and data will be output directly to browser as a downloable file
|
||||
* @param data 2D array with data
|
||||
* @param fields field names
|
||||
* @param delimiter delimiter used to separate data
|
||||
* @return CSV data using delimiter of choice, or default
|
||||
*/
|
||||
function output ($output = true, $filename = null, $data = array(), $fields = array(), $delimiter = null) {
|
||||
function output ($filename = null, $data = array(), $fields = array(), $delimiter = null) {
|
||||
if ( empty($filename) ) $filename = $this->output_filename;
|
||||
if ( $delimiter === null ) $delimiter = $this->output_delimiter;
|
||||
$data = $this->unparse($data, $fields, null, null, $delimiter);
|
||||
if ( $output ) {
|
||||
if ( $filename !== null ) {
|
||||
header('Content-type: application/csv');
|
||||
header('Content-Disposition: inline; filename="'.$filename.'"');
|
||||
header('Content-Disposition: attachment; filename="'.$filename.'"');
|
||||
echo $data;
|
||||
}
|
||||
return $data;
|
||||
@@ -474,11 +481,20 @@ class parseCSV {
|
||||
}
|
||||
$this->titles = $head;
|
||||
if ( !empty($this->sort_by) ) {
|
||||
( $this->sort_reverse ) ? krsort($rows) : ksort($rows) ;
|
||||
$sort_type = SORT_REGULAR;
|
||||
if ( $this->sort_type == 'numeric' ) {
|
||||
$sort_type = SORT_NUMERIC;
|
||||
} elseif ( $this->sort_type == 'string' ) {
|
||||
$sort_type = SORT_STRING;
|
||||
}
|
||||
( $this->sort_reverse ) ? krsort($rows, $sort_type) : ksort($rows, $sort_type) ;
|
||||
if ( $this->offset !== null || $this->limit !== null ) {
|
||||
$rows = array_slice($rows, ($this->offset === null ? 0 : $this->offset) , $this->limit, true);
|
||||
}
|
||||
}
|
||||
if ( !$this->keep_file_data ) {
|
||||
$this->file_data = null;
|
||||
}
|
||||
return $rows;
|
||||
}
|
||||
|
||||
@@ -501,7 +517,7 @@ class parseCSV {
|
||||
$entry = array();
|
||||
|
||||
// create heading
|
||||
if ( $this->heading && !$append ) {
|
||||
if ( $this->heading && !$append && !empty($fields) ) {
|
||||
foreach( $fields as $key => $value ) {
|
||||
$entry[] = $this->_enclose_value($value);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user