2

I am trying to display CSV file content in CGridView, I want to display CSV file header as CGridView "Column" and its content as DataProvider. Please provide any idea to achieve this?

Petra Barus
  • 3,815
  • 8
  • 48
  • 87
US-1234
  • 1,519
  • 4
  • 24
  • 61

1 Answers1

3

You can use CArrayDataProvider. http://www.yiiframework.com/doc/api/1.1/CArrayDataProvider

$dataProvider = new CArrayDataProvider(str_getcsv(file_get_contents('file.csv')));

You can do something like this.

$file = fopen('test.csv', 'r');
$data = array();
while (($line = fgetcsv($file)) !== FALSE) {
    //$line is an array of the csv elements
    $data[] = $line;
}
fclose($file);
$columns = array();
foreach ($data[0] as $key => $value) {
    $columns[] = array(
        'name' => $key,
        'header' => $value,
    );
}
$data = array_slice($data, 1);
$dataProvider = new CArrayDataProvider($data, array(
    'keyField' => 0,
));
$this->widget('zii.widgets.grid.CGridView', array(
    'dataProvider' => $dataProvider,
    'columns' => $columns
));

If you want to create a generic data provider for CSV, you can create a new class.

class CsvDataProvider extends CArrayDataProvider {

    private $_columns = array();

    public function __construct($file, $config = array()) {
        $handler = fopen($file, 'r');
        $data = array();
        while (($line = fgetcsv($handler)) !== FALSE) {
            $data[] = $line;
        }
        fclose($handler);
        $this->_columns = array();
        foreach ($data[0] as $key => $value) {
            $this->_columns[] = array(
                'name' => $key,
                'header' => $value,
            );
        }
        $data = array_slice($data, 1);
        parent::__construct($data, array_merge($config, array(
            'keyField' => 0,
        )));
    }

    public function getColumns() {
        return $this->_columns;
    }
}

Then you can do something like this.

$dataProvider = new CsvDataProvider('file.csv');
$this->widget('zii.widgets.grid.CGridView', array(
    'dataProvider' => $dataProvider,
    'columns' => $dataProvider->getColumns(),
));
Petra Barus
  • 3,815
  • 8
  • 48
  • 87
  • I have used the above code, now am getting this error "Invalid argument supplied for foreach()" – US-1234 Jan 06 '14 at 04:09
  • I haven't really tried the code above. I forgot that `fgetcsv()` will return row by row. You can use `str_getcsv()` function if you are using PHP 5.3. Or this one http://stackoverflow.com/questions/1269562/how-to-create-an-array-from-a-csv-file-using-php-and-the-fgetcsv-function – Petra Barus Jan 06 '14 at 04:17
  • I still haven't tried it using gridview sorting, though. Let me know if there is some issues when you use the sorting in the gridview. And I think there will be some performance issue if you try to show a very very large CSV file. This is because even though there is pagination, you still have to load whole CSV file to memory. – Petra Barus Jan 06 '14 at 04:55
  • There is a discussion about this in Yii forum. http://www.yiiframework.com/forum/index.php/topic/14020-cgridview-with-carraydataprovider-sorting/ – Petra Barus Jan 06 '14 at 05:47
  • that's a whole different question. you might need to create a new stackoverflow question. thanks. – Petra Barus Jan 06 '14 at 06:49
  • Posted new question http://stackoverflow.com/questions/20944676/adding-cbuttoncolumn-to-csv-header-column-array-used-in-cgridview-yii-framew – US-1234 Jan 06 '14 at 07:38