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 05:27 UTC Modified: 2006-07-24 14:58 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 05: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 12:08 UTC] wiesemann (Mark Wiesemann)
Hi Gregor, as long as request #1649 (attaching files to bug reports) is not fulfilled, please use something like http://phpfi.com/ for your patches. Short patches are okay, but longer ones are not readable and require manual work. Just paste your code there and post the link to your code here. Thanks, Mark
 [2006-07-24 13: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 14:58 UTC] wiesemann (Mark Wiesemann)
This bug has been fixed in CVS. If this was a documentation problem, the fix will appear on pear.php.net by the end of next Sunday (CET). If this was a problem with the pear.php.net website, the change should be live shortly. Otherwise, the fix will appear in the package's next release. Thank you for the report and for helping us make PEAR better. --- Thanks, Gregor. Before making a new release of the CSV DataSource, I'd like to get Olivier's opinion on this first.