PHP code example of satthi / csv-combine-plugin-for-cakephp

1. Go to this page and download the library: Download satthi/csv-combine-plugin-for-cakephp library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

satthi / csv-combine-plugin-for-cakephp example snippets



namespace App\Controller;

use Cake\Core\Configure;

use CsvCombine\Form\CsvImportForm;

class CsvController extends AppController
{
    public function initialize()
    {
        parent::initialize();
        $this->loadComponent('CsvCombine.CsvExport');
    }
    public function export()
    {
        $list = [
            [
                'test1',
                'test2',
                'test3',
            ],
            [
                'test4',
                'test5',
                'test6',
            ],
        ];
        /*
         *@array $list 出力のための配列(二次元配列が基本)
         *@param $file_name 出力ファイル名(デフォルトはexport.csv)
         *@param $delimiter 区切り文字の設定(デフォルトは",")
         *@param $directory 一時保存ディレクトリ(デフォルトはTMP,最終的にファイルを削除をする)
         *@param $export_encoding 出力するファイルのエンコード(デフォルトはSJIS-win
         *@param $array_encoding 入力する配列のエンコード(デフォルトはUTF-8
        */
        return $this->CsvExport->export($list);
    }

    public function import()
    {
        $import = new CsvImportForm();
        $file = TMP . 'test.csv';
        $column = [
            'key1',
            'key2',
            'key3',
        ];
        /*
         *@array file ファイルパス(必須
         *@array $column カラム名を並び順に(必須
         *@param $delimiter 区切り文字を設定 (デフォルトは","で"\t"や"|"などを指定することが可能)
         *@param $array_encoding 出力する配列のエンコード(デフォルトはUTF-8
         *@param $import_encoding 入力するファイルのエンコード(デフォルトはSJIS-win
        */
        $result = $import->loadDataCsv($file,$column);
        debug($result);
        exit;
    }
}



namespace App\Controller;

use Cake\Core\Configure;

use CsvCombine\Form\FixedLengthImportForm;

class CsvController extends AppController
{
    public function initialize()
    {
        parent::initialize();
        $this->loadComponent('CsvCombine.FixedLengthExport');
    }

    public function export()
    {
        $list = [
            [
                'あいう',
                'いいい',
                'uuu',
            ],
            [
                'あいう',
                'いいい',
                'uuu',
            ],
            [
                'あいう',
                'いいい',
                'uuu',
            ],
        ];
        $fixed_options = [
            8,
            10,
            6
        ];
        //makeでファイル作成のみ
        /*
         * export 固定長の出力アクション
         *
         * @array $list 出力のための配列(二次元配列が基本)
         * @array $fixed_options 出力のための固定長の設定(各カラムのバイト数)
         * @param $file_name 出力ファイル名(デフォルトはexport.txt)
         * @param $line_feed_code 改行コード(デフォルトは\r\n)
         * @param $directory 一時保存ディレクトリ(デフォルトはTMP,最終的に削除をする)
         * @param $export_encoding 出力するファイルのエンコード(デフォルトはSJIS-win
         * @param $array_encoding 入力する配列のエンコード(デフォルトはUTF-8
         */

        //$this->FixedLengthExport->make($list,$fixed_options);
        $this->FixedLengthExport->export($list,$fixed_options);

    }

    public function import()
    {
        $filename = TMP . 'test.txt';
        $column_list = [
            ['name' => 'column1', 'length' => 8],
            ['name' => 'column2', 'length' => 10],
            ['name' => 'column3', 'length' => 6],
        ];
        $import = new FixedLengthImportForm();
        /*
         * @text $fileName 固定長テキストファイ
         * @array $column_list 各カラム情報(name:カラム名,length:バイト数 デフォルトは空配列 空時には列の数だけ0から連番を振る)
         * @param $line_feed_code 改行コード(デフォルトは\r\n)
         * @param $array_encoding 出力するする配列のエンコード(デフォルトはUTF-8
         * @param $import_encoding 入力するテキストのエンコード(デフォルトはSJIS-win
         */
        $result = $import->loadData($filename, $column_list);
        debug($result);
        exit;
    }
}