在foreach循环中导入excel并进行过滤

yv5phkfx  于 2021-06-18  发布在  Mysql
关注(0)|答案(0)|浏览(160)

当执行foreach循环,然后使用表中的数据进行过滤时,下面的代码运行得非常慢 cabang 并插入一个名为 part_rak .
通过excel文件中的每一行进行过滤循环,然后基于循环 comma 分离器
滤波回路

if (explode(",", $rowData[0][14]) > 1) {

                foreach (explode(",", $rowData[0][14]) as $key => $value2) {
                    $data       = array(
                                "rak_part_id"   => $last,
                                "rak_cabang_id" => $this->spareparts_m->get_cabang_id_by_code("B"),
                                "rak_code"      => $value2,
                                "rak_created_at"=> date('Y-m-d H:i:s')
                    );
                    $this->db->insert("part_rak",$data);
                }

            }else{
                foreach (explode("|", $cabang) as $key => $value) {
                    $data       = array(
                                "rak_part_id"   => $last,
                                "rak_cabang_id" => $value,
                                "rak_code"      => $expRak[$key],
                                "rak_created_at"=> date('Y-m-d H:i:s')
                    );
                    $this->db->insert("part_rak",$data);

                }
            }

这里是我的模型方法,通过代码获取\u cabang \u id \u

public function get_cabang_id_by_code($cabang_code)
    {
        $q = $this->db->get_where("cabang",array("cabang_code" => $cabang_code));
        return $q->result_array()[0]['cabang_id'];
    }

如果有任何建议能提高我剧本的表现,我将不胜感激。
下面是我完整的导入源代码

public function import($uploaded)
    {
        $inputFileName =  './public/uploads/excel/'.$uploaded['file_name'];
        try {
            $inputFileType = IOFactory::identify($inputFileName);
            $objReader = IOFactory::createReader($inputFileType);
            $objPHPExcel = $objReader->load($inputFileName);
        } catch(Exception $e) {
            die('Error loading file "'.pathinfo($inputFileName,PATHINFO_BASENAME).'": '.$e->getMessage());
        }

        $sheet          = $objPHPExcel->getSheet(0);

        $highestRow     = $sheet->getHighestRow();
        $highestColumn  = $sheet->getHighestDataColumn();
        $last_id="";
        for ($row = 3; $row <= $highestRow; $row++){                  //  Read a row of data into an array                 
            $rowData = $sheet->rangeToArray('A' . $row . ':' . $highestColumn . $row, NULL, TRUE, FALSE);

            $data = array(
                "part_no"           => $rowData[0][0],
                "part_revised_no"   => $rowData[0][1],
                "part_latest_no"    => $rowData[0][2],
                "part_desc"         => $rowData[0][3],
                "part_incoming_date"=> $rowData[0][4],
                "part_fob_japan"    => $rowData[0][5],
                "part_min_order"    => $rowData[0][6],
                "part_master_key1"  => $rowData[0][7],
                "part_total_stock"  => $rowData[0][8],
                "part_stock_j"      => $rowData[0][9],
                "part_stock_b"      => $rowData[0][10],
                "part_stock_s"      => $rowData[0][11],
                "part_stock_m"      => $rowData[0][12],
                // "part_rak"          => $rowData[0][13],
                "part_last_opname"  => $rowData[0][17],
                "part_discontinued" => $this->convDiscontinued($rowData[0][18]),
                "part_mesin_code"   => $rowData[0][19],
                "part_srp"          => $rowData[0][20],
                "part_srp_cod"      => $rowData[0][21],
                "part_supv_price"   => $rowData[0][22],
                "part_mgr_price"    => $rowData[0][23],
                "part_dtr_price"    => $rowData[0][24],
                "part_keterangan"   => $rowData[0][25],
            );
            // $data['part_rak_id']   = implode(",", $arrLast);
            $insert     = $this->db->insert("sparepart", $data);
            $last       = $this->db->insert_id();

            $cabang = $this->spareparts_m->get_cabang_id_by_code("J")."|".
                      $this->spareparts_m->get_cabang_id_by_code("B")."|".
                      $this->spareparts_m->get_cabang_id_by_code("S")."|".
                      $this->spareparts_m->get_cabang_id_by_code("M");

            $rak_code   = $rowData[0][13]."|".$rowData[0][14]."|".$rowData[0][15]."|".$rowData[0][16];
            $expRak     = explode("|", $rak_code);
            $arrLast    = [];

            if (explode(",", $rowData[0][13]) > 1) {

                foreach (explode(",", $rowData[0][13]) as $key => $value2) {
                    $data       = array(
                                "rak_part_id"   => $last,
                                "rak_cabang_id" => $this->spareparts_m->get_cabang_id_by_code("J"),
                                "rak_code"      => $value2,
                                "rak_created_at"=> date('Y-m-d H:i:s')
                    );
                    $this->db->insert("part_rak",$data);
                }

            }else{
                foreach (explode("|", $cabang) as $key => $value) {
                    $data       = array(
                                "rak_part_id"   => $last,
                                "rak_cabang_id" => $value,
                                "rak_code"      => $expRak[$key],
                                "rak_created_at"=> date('Y-m-d H:i:s')
                    );
                    $this->db->insert("part_rak",$data);
                    // $arrLast[]= $this->db->insert_id();
                }
            }

            if (explode(",", $rowData[0][14]) > 1) {

                foreach (explode(",", $rowData[0][14]) as $key => $value2) {
                    $data       = array(
                                "rak_part_id"   => $last,
                                "rak_cabang_id" => $this->spareparts_m->get_cabang_id_by_code("B"),
                                "rak_code"      => $value2,
                                "rak_created_at"=> date('Y-m-d H:i:s')
                    );
                    $this->db->insert("part_rak",$data);
                }

            }else{
                foreach (explode("|", $cabang) as $key => $value) {
                    $data       = array(
                                "rak_part_id"   => $last,
                                "rak_cabang_id" => $value,
                                "rak_code"      => $expRak[$key],
                                "rak_created_at"=> date('Y-m-d H:i:s')
                    );
                    $this->db->insert("part_rak",$data);
                    // $arrLast[]= $this->db->insert_id();
                }
            }

            if (explode(",", $rowData[0][15]) > 1) {

                foreach (explode(",", $rowData[0][15]) as $key => $value2) {
                    $data       = array(
                                "rak_part_id"   => $last,
                                "rak_cabang_id" => $this->spareparts_m->get_cabang_id_by_code("S"),
                                "rak_code"      => $value2,
                                "rak_created_at"=> date('Y-m-d H:i:s')
                    );
                    $this->db->insert("part_rak",$data);
                }

            }else{
                foreach (explode("|", $cabang) as $key => $value) {
                    $data       = array(
                                "rak_part_id"   => $last,
                                "rak_cabang_id" => $value,
                                "rak_code"      => $expRak[$key],
                                "rak_created_at"=> date('Y-m-d H:i:s')
                    );
                    $this->db->insert("part_rak",$data);
                    // $arrLast[]= $this->db->insert_id();
                }
            }

            if (explode(",", $rowData[0][16]) > 1) {

                foreach (explode(",", $rowData[0][16]) as $key => $value2) {
                    $data       = array(
                                "rak_part_id"   => $last,
                                "rak_cabang_id" => $this->spareparts_m->get_cabang_id_by_code("M"),
                                "rak_code"      => $value2,
                                "rak_created_at"=> date('Y-m-d H:i:s')
                    );
                    $this->db->insert("part_rak",$data);
                }

            }else{
                foreach (explode("|", $cabang) as $key => $value) {
                    $data       = array(
                                "rak_part_id"   => $last,
                                "rak_cabang_id" => $value,
                                "rak_code"      => $expRak[$key],
                                "rak_created_at"=> date('Y-m-d H:i:s')
                    );
                    $this->db->insert("part_rak",$data);
                    // $arrLast[]= $this->db->insert_id();
                }
            }

        }
    }

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题