PEAR is archived and read-only

This mirror preserves historical PEAR package releases and metadata so existing references remain available.

Home » Structures » Structures_DataGrid_DataSource_CSV » Bug #8272

Set fields not as array_keys of the first row, but as keys of the longest row

Details

Request #8272Set fields not as array_keys of the first row, but as keys of the longest row
Submitted2006-07-24 10:27 UTC
Fromgramlich at eosc dot de
Assignedwiesemann
StatusClosed
PackageStructures_DataGrid_DataSource_CSV
PHP Version4.3.8
OSLinux
Roadmaps(Not assigned)

Comments

[2006-07-24 10:27 UTC] gramlich at eosc dot de

Description:
------------
This applies to CSV with and without header, if 'fields'
option is not set.

If the first data-line of the csv does not contain all
columns, then only these few columns are used for the
inherited fetch() method.

It would be nice to allow a variable number of columns
from row to row (aligned left), and use all columns that
ever appear, possibly null.
(To get it through to the renderer, you will still have to
set the option 'generate_columns' => true.)

Patch:

--- CSV.phpBAK 2006-07-24 10:20:44.000000000 +0200
+++ CSV.php 2006-07-24 11:36:14.036036368 +0200
@@ -93,11 +93,22 @@
$keys = null;
}

+ // Store every field (column name) that is
actually used.
+ // Otherwise, we get in trouble with fetch() from
+ // Structures_DataGrid_DataSource_Array, where
the fields option
+ // is simply set to array_keys($this->_ar[0]),
but this might have not
+ // all the fields, if the first line has less
fields.
+ $fields = $keys;
+ // This applies to csv without header.
+ $maxkeys = 0;
+
foreach ($rowList as $row) {
$row = rtrim($row); // to remove DOSish \r
if (!empty($row)) {
if (empty($keys)) {
- $this->_ar[] =
explode($this->_options['delimiter'], $row);
+ $rowArray =
explode($this->_options['delimiter'], $row);
+ $this->_ar[] = $rowArray;
+ $maxkeys = max($maxkeys,
count($rowArray));
} else {
$rowAssoc = array();
$rowArray =
explode($this->_options['delimiter'], $row);
@@ -105,6 +116,11 @@
if (!empty($keys[$index])) {
$rowAssoc[$keys[$index]] =
$val;
} else {
+ // There are more fields,
than we have column names from the header
+ // Use the numeric index.
+ if (!in_array($index,
$fields, true)) {
+ $fields[] = $index;
+ }
$rowAssoc[$index] = $val;
}
}
@@ -113,6 +129,15 @@
}
}

+ // Set field names, if they were not set as
option
+ if (!$this->_options['fields']) {
+ if (empty($keys)) {
+ $this->_options['fields'] = range(0,
$maxkeys-1);
+ } else {
+ $this->_options['fields'] = $fields;
+ }
+ }
+
return true;
}
}

[2006-07-24 18:45 UTC] gramlich at eosc dot de

Hi Marc,

sorry. The diff is here:
http://phpfi.com/134992

The new version is here:
http://phpfi.com/134994