0

I'm having problem when trying to import excel with query builder (I need more performance). I want to convert some of the data with it's corresponding converters, but I don't know which one caused Undefined offset: 3...

public function collection(Collection $rows)
{
    foreach ($rows as $row) {
        // convert data
        $idrow[0] = $this->convert2($this->convert($row[0])); // no_pendaftaran
        $idrow[6] = $this->convert3($this->convert($row[6])); // kode_pilihan_1
        $idrow[7] = $this->convert3($this->convert($row[7])); // kode_pilihan_2

        // add new data to database if not already exist
        DB::table('data_prodi')->insertOrIgnore(['kode_prodi' => $idrow[6],'prodi' => $row[6]]);
        DB::table('data_prodi')->insertOrIgnore(['kode_prodi' => $idrow[7],'prodi' => $row[7]]);

        // add data to main table
        DB::table('data_train')->insert([
            'no_pendaftaran' => $idrow[0],
            'kode_pilihan_1' => $idrow[6],
            'kode_pilihan_2' => $idrow[7],
            'daftar_kembali' => $row[10],
        ]);
    }
}

// if null then -
private function convert($data) {
    if ($data === null) {
        return '-';
    } else {
        return $data;
    }
}

// fill no_pendaftaran with random number if -
private function convert2($data) {
    if ($data === '-') {
        $id = mt_rand(1000000000,9999999999);
        $ifExists = DB::table('data_train')->where('no_pendaftaran','=',$id);
        if ($ifExists->count() > 0) {
            return convert2($data);
        }   
        return $id;
    } else {
        return $data;
    }
}

// replace non-numeric data with it's corresponding id in database if found, if not make it's id starting from 1
private function convert3($data) {
    if (preg_match("/[^0-9-]/i", $data)){
        $ifExists = DB::table('data_prodi')->select('kode_prodi')->where('prodi','=',$data)->first();
        if ($ifExists){
            $data = (int)implode((array)$ifExists);
        } else {
            @$id++;
            $data = $id;
        }
        return $data;
    } else {
        return $data;
    }
}

And here is my excel data: Excel Form Data

P.S. I cut out some items to be able to post my question

iamatstackoverflow
  • 402
  • 2
  • 4
  • 13
Aditya Pratama
  • 657
  • 1
  • 8
  • 21
  • Does this answer your question? ["Notice: Undefined variable", "Notice: Undefined index", and "Notice: Undefined offset" using PHP](https://stackoverflow.com/questions/4261133/notice-undefined-variable-notice-undefined-index-and-notice-undefined) – fonini Dec 02 '19 at 11:36
  • the error message and the stacktrace will lead you to the line that is causing the issue – lagbox Dec 02 '19 at 11:47
  • @fonini There is nothing wrong when I check each idrow with isset in the link you suggested. [result txt](http://www.mediafire.com/file/bzimqgrnmuh96tx/isset.txt/file). But I can't seem to know which one should I check? (where the problem variable lies) – Aditya Pratama Dec 02 '19 at 12:17
  • @lagbox I have found out which part caused the error, I will post a new question – Aditya Pratama Dec 03 '19 at 08:28

0 Answers0