Package home | Report new bug | New search | Development Roadmap Status: Open | Feedback | All | Closed Since Version 0.1.6

Request #8272 Set fields not as array_keys of the first row, but as keys of the longest row
Submitted: 2006-07-24 10:27 UTC
From: gramlich at eosc dot de Assigned: wiesemann
Status: Closed Package: Structures_DataGrid_DataSource_CSV (version 0.1.2)
PHP Version: 4.3.8 OS: Linux
Roadmaps: (Not assigned)    
Subscription  


 [2006-07-24 10:27 UTC] gramlich at eosc dot de (Gregor Gramlich)
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; } }

Comments

 [2006-07-24 17:08 UTC] User who submitted this comment has not confirmed identity
If you submitted this note, check your email.If you do not have a message, click here to re-send
MANUAL CONFIRMATION IS NOT POSSIBLE.  Write a message to pear-dev@lists.php.net
to request the confirmation link.  All bugs/comments/patches associated with this

email address will be deleted within 48 hours if the account request is not confirmed!
 [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
 [2006-07-24 19:58 UTC] User who submitted this comment has not confirmed identity
If you submitted this note, check your email.If you do not have a message, click here to re-send
MANUAL CONFIRMATION IS NOT POSSIBLE.  Write a message to pear-dev@lists.php.net
to request the confirmation link.  All bugs/comments/patches associated with this

email address will be deleted within 48 hours if the account request is not confirmed!