Help us understand the problem. What is going on with this article?

cakephp 2.x csv登録機能

More than 1 year has passed since last update.

1.コントローラーに書き込む
ちょっと複雑に書いていますが、取込エラーした場合のエラーリスト生成を含んでいます。
そのままでも基本使えますが、デバックはしてください。
array_spliceはカラムを削除しているのですが、なんで削除しているのか忘れました。
多分いらないはずです。

controller.php
public function csv() {
    error_reporting(0);
    ini_set("max_execution_time",600);

    // CSV取込
    if($_POST['mode'] == "csv"){

    $this->set('csvdata', $_POST['data']);
    $this->set('txtmode', $_POST['txtmode']);
    $result = $_POST['data'];

    // 配列に格納
    $member_list = explode("\r\n", $result); // 改行で配列
    foreach($member_list as $value){
        if($value !== reset($member_list)){// 最初は飛ばす
            if($_POST['txtmode'] == 0){
                $member_lists[] = explode(',',$value); // カンマで区切る
            }else{
                $member_lists[] = explode("\t",$value); // タブで区切る
            }
        }
    }

    //空データ削除
    foreach($member_lists as $key => $value){
        if(!empty($value[0])){
            $lists[] = $value;
        }
    }

    // header情報取得
    $column = $this->MemberColumn->find('all');
    foreach($column as $value){ // フィールド設定
        $column_field[] = $value['MemberColumn']['category_data_name'];
    }
    $column_field_u = $column_field;
    array_splice($column_field_u, 0, 1);

    //カラム付ける
    foreach($mem_lists as $key => $value){
        foreach($column_field as $key2 => $values){
            $mem_list[$key][$values] = $value[$key2];
        }
    }

    //insert & update
    foreach($mem_list as $value){
        $data = array('Members' => $value);
        $check = $this->Members->findByMember_id($data['Members']['member_id']);
        if(empty($check['Members'])){
            try{
                $this->Members->save($data, false, $column_field);
            }catch(Exception $e){
                foreach($member_list as $value){
                    if(preg_match('/'.$data['Members']['member_id'].'/', $value)){
                    $errorlist['data']['insert'][] = $value;
                    }
                }
            }
        }else{
            array_splice($column_field_u, 17, 1);
            array_splice($data['Members'], 18, 1);
            try{
                $this->Members->save($data, false, $column_field_u);
            }catch(Exception $e){
                foreach($member_list as $value){
                    if(preg_match('/'.$data['Members']['member_id'].'/', $value)){
                        $errorlist['data']['updata'][] = $value;
                    }
                }
            }
        }
    }
}// csv if end

//エラーリスト
//ヘッダー格納
$errorlist['header'] = (!empty($errorlist['data']))? $member_list[0]: none;
$this->set('errorlist',$errorlist);

2.続いてctpの書き方
カンマ区切りなのか、タブ区切りか選べるようにしてます。

csv.ctp
<?php if(!empty($errorlist['header']) && $errorlist['header'] != 'none'): ?>
<div class="error">
  <p>エラーリスト</p>
  <p class="attention">データ修正を行い、別途登録してください。</p>

  <?php if(!empty($errorlist['data']['insert'])): ?>
    <p>インサート</p>
    <textarea><?php
      echo $errorlist['header'].'&#13;';
      foreach($errorlist['data']['insert'] as $error){
        echo $error.'&#13;';
      }
    ?></textarea>
  <?php endif; ?>

  <?php if(!empty($errorlist['data']['updata'])): ?>
    <p>アップデート</p>
    <textarea><?php
      echo $errorlist['header'].'&#13;';
      foreach($errorlist['data']['updata'] as $error){
        echo $error.'&#13;';
      }
    ?></textarea>
  <?php endif; ?>

</div>
<?php endif; ?>

<form method="post" name="csv" id="form" class="csv">
  <input type="hidden" name="mode" value="csv">
  <div>
    <h4>区切り指定</h4>
    <label><input type="radio" name="txtmode" value="0" <?php if($txtmode == 0){ echo 'checked'; } ?> required> カンマ区切り</label>
    <label><input type="radio" name="txtmode" value="1" <?php if($txtmode == 1){ echo 'checked'; } ?> required> タブ区切り</label>
  </div>
  <textarea name="data"><?php if(isset($csvdata)){ echo $csvdata; }else{ echo "ログインID\t会員番号\n"; } ?></textarea>
  <input type="submit" value="送信">
</form>
Youz
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした