将表格中的数据从特定来源字段发送到另一个表格

时间:2018-06-25 02:58:49

标签: php ajax codeigniter

请帮助我解决这个问题。 我有一个表名'aptimportdata'和'aptimportdataaddress'。

下面的代码是用于将Microsoft excel数据导入网站的功能。 我想将数据从表aptimportdata发送到aptimportdataaddress,但我不知道如何做,因为在excel文件中,一个人有2个家庭住址列,一个的住所名称为homeaddress1到4,而另一个是altaddress1到4,“ altaddress”是为住家地址大于1的人创建的。

现在,当我从网站导入数据时,数据将发送到aptimportdata表,并且当我从网站上传文件时没有问题,但是当我将“ altaddress”数据目标表更改为aptimportdataaddress表,该网站只是给我无休止的加载,而没有成功报告。

我已经在aptimportdataaddress表中为“ altaddress”创建了列,但是我找不到解决方案,如何使“ altaddress”数据进入aptimportaddress表,而它只是我要放入的“ altaddress”列数据aptimportdataaddress表。

如果您需要表格的屏幕截图,我将其发送给您。 这是我的代码:

= 5.3.3  * @版权所有2014 Sereware  * @开发人员Yudha Tama Aditiyara  * @应用程序控制器/公寓  * / 类导入扩展了Controller {     受保护的$ _models = array('apartement / import / m_import');

public function dataexcel(){
    $config['ajax_upload_dataexcel'] = site_url('apartement/import/ajax_upload_dataexcel');
    $config['ajax_process_dataexcel'] = site_url('apartement/import/ajax_process_dataexcel/'.$this->d_page->page('noid'));
    $this->load->view('apartement/import/dataexcel',$config);
}

public function ajax_upload_dataexcel(){
    $id = str_replace('.','',microtime(true));
    $upload = new Sereware\ExcelUpload(array('upload_dir' => SYSPATH.'files/'),false);
    $upload->hasNormalFilename = true;
    $this->d_page->obstart();
    $upload->post();
    ob_flush();
}

protected $apttower = array(
    'h' => 1,'s'=>2,'y'=>3,'p'=>4
);

#{{{
protected function get_dataexcel(){
    if (!isset($_REQUEST['file']) || !trim($_REQUEST['file']))return;
    $file = $_REQUEST['file'];
    $file = SYSPATH."files/" . $file . (pathinfo($file,PATHINFO_EXTENSION) !== 'xls' ? '.xls' : '');
    if (!file_exists($file)) return;
    $excel = new Sereware\Excel($file);
    $data = $excel->getCells(0);
    return $data;
}

protected function matcher_excelfield($dataexcelfields, $appimportdetails){
    $fieldnames = array();
    foreach($dataexcelfields as $indexField => $dataexcelfield) {
        $dataexcelfield = strtolower(preg_replace('#[\n\r]#','',trim($dataexcelfield)));
        if (isset($appimportdetails[$dataexcelfield])) {
            $destfield = preg_replace('#[\n\r]#','',$appimportdetails[$dataexcelfield]->destfield);
            $desttypefield = (int)$appimportdetails[$dataexcelfield]->desttypefield;
            $isnormalized = $appimportdetails[$dataexcelfield]->isnormalized;
            $defaultvalue = $appimportdetails[$dataexcelfield]->defaultvalue;

            $appimportdetail = $appimportdetails[$dataexcelfield];
            $appimportdetail_desttables = array_filter(explode(';',$appimportdetail->desttable));

            foreach($appimportdetail_desttables as $tablename) {
                $fieldnames[$tablename][$indexField] = array(
                    'destfield' => $destfield,
                    'desttypefield' => $desttypefield,
                    'isnormalized' => $isnormalized,
                    'defaultvalue' => $defaultvalue
                );
            }

        }
    }
    return $fieldnames;
}





protected function normalize_dataexcel($destfield,$value){
    $value = trim($value);
    switch($destfield) {
        case 25:
            $value = str_replace('%','',$value);
            break;
        case 11:
            if ($value){
                $value = strtotime(str_replace('/', '-',(string)$value));
                $value = date('Y-m-d',$value);
            } else {
                $value = null;
            }
            break;
        case 34:
            if (!$value) {
                $value = -1;
            } else {
                $value = strtolower($value) === 'ok' ? 1 : 0;
            }
            break;
    }
    return $value;
}

public function ajax_process_dataexcel($idpage){
    $appimport = $this->m_import->get_appimport();
    $appimportdetails = $this->m_import->get_appimportdetails($appimport->idconnection,$appimport->noid);

    $aptimportdatas = $this->m_import->get_aptimportdatas($appimport->idconnection);
    $newid_aptimportdata = $this->m_import->get_maxid_aptimportdata();


    $aptsyarats = $this->m_import->data_aptsyarats();
    $aptdatabap = $this->m_import->data_aptbap();

    $dataexcel = $this->get_dataexcel();
    $dataexcelfields = $this->matcher_excelfield(array_shift($dataexcel),$appimportdetails);

    $insert_data = array();
    $update_data = array();
    $insert_data_aptunitroom = array();
    $insert_data_aptimportdataaddress = array();
    $insert_data_aptunitroomsyarat = array();
    $insert_data_aptdatabap = array();

    $datamcarduser = $this->d_page->d_login->get_datamcarduser();

    // var_dump(json_encode($dataexcelfields));
    // exit();
    foreach($dataexcel as $data) {
        foreach($dataexcelfields as $tablename => $fieldnames) {
            ##-1
            ##-build data per-row
            $ndata = array();
            $ready = true;
            $ndata['idcreate'] = $datamcarduser->noid;
            $ndata['idupdate'] = $datamcarduser->noid;
            $ndata['docreate'] = date('Y-m-d H:i:s');
            $ndata['lastupdate'] = date('Y-m-d H:i:s');
            foreach($fieldnames as $indexFieldExcel => $datafields) {
                $fieldname = $datafields['destfield'];
                $isnormalized = intval($datafields['isnormalized']);
                $defaultvalue = $datafields['defaultvalue'];
                $desttypefield = $datafields['desttypefield'];
                if (!$isnormalized) {
                    $defaultvalue = $data[$indexFieldExcel];
                }
                $ndata[$fieldname] = $this->normalize_dataexcel($desttypefield,$defaultvalue);
            }
            ##-2


            if (isset($ndata['lotno']) && isset($aptimportdatas[$ndata['lotno']])) {
                $metadata = $aptimportdatas[$ndata['lotno']];
                if ($tablename === 'aptimportdata') {
                    $update_data[$tablename][$metadata['metadata']->noid] = $ndata;
                    // if (!empty($ndata['ppjbdate'])) {

                    // }
                    $update_data['aptunitroom'][$metadata['metadata']->noid] = array(
                        'statuslegal' => empty($ndata['ppjbdate']) ? 0 : 1,
                        'statusproject' => $ndata['isreadytoho']
                    );

                    $mimportlog_r = array_merge(array("noid" => $metadata['metadata']->noid), $ndata);
                    $insert_data['mimportlog'][] = $mimportlog_r;
                    continue;
                }
                $matchers = $metadata['matchers'];
                $extrafields = $metadata['extrafields'];
                if (isset($matchers[$tablename])) {
                    foreach($matchers[$tablename] as $_fieldname => $value) {
                        if ($value === (int)$ndata[$_fieldname]) {
                            $ready = false;
                            break;
                        }
                    }
                }

                ##-
                if ($ready) {
                    if (isset($extrafields[$tablename])) {
                        foreach($extrafields[$tablename] as $__fieldname => $_value) {
                            $ndata[$__fieldname] = $_value; 
                        }
                    }
                }
            }

            ##-3
            if ($ready) {
                $lotno = false;
                if ($tablename === 'aptimportdata') {
                    $lotno = $ndata['lotno'];
                    $ndata['noid'] = $newid_aptimportdata;
                    $insert_data_aptunitroom[] = array(
                        'noid' => $newid_aptimportdata,
                        'statusproject' => $ndata['isreadytoho'],
                        'statuslegal' => empty($ndata['ppjbdate']) ? -1 : 1
                    );
                    $insert_data_aptimportdataaddress[] = array(
                        'noid' => $newid_aptimportdata
                    );
                    foreach($aptsyarats as $key => &$aptsyaratdata) {
                        $aptsyaratdata = (array)$aptsyaratdata;
                        $aptsyaratdata['idaptunitroom'] = $newid_aptimportdata;
                        $insert_data_aptunitroomsyarat[] = $aptsyaratdata;
                    }
                    foreach($aptdatabap as $key => $data){
                        $data = (array)$data;
                        $data['idaptunitroom'] = $newid_aptimportdata;
                        $insert_data_aptdatabap[] = $data;
                    }
                    $newid_aptimportdata++;
                }
                if ($lotno !== false) {
                    $insert_data[$tablename][$lotno] = $ndata;
                } else {
                    $insert_data[$tablename][] = $ndata;
                }
            }
        }
    }
    $real_insert_data = array();
    if (isset($insert_data['aptimportdata'])) {
        $_aptimportdata = $insert_data['aptimportdata'];
        $real_insert_data['aptimportdata'] = array_values($_aptimportdata);
        $real_insert_data['mimportlog'] = array_values($_aptimportdata);

        foreach($insert_data as $tablename => $datas) {
            if ($tablename !== 'aptimportdata' && $tablename !== 'aptunitroom') {
                $real_insert_data[$tablename] = array();
                foreach($datas as $key => $data) {
                    if ((!isset($data['idaptunitroom']) || intval($data['idaptunitroom']) === 0) || $_aptimportdata[$data['lotno']]) {
                        $data['idaptunitroom'] = $_aptimportdata[$data['lotno']]['noid'];
                    }
                    $real_insert_data[$tablename][$key] = $data;
                }
            }
        }
    } else {
        $real_insert_data = $insert_data;
    }

    if (!empty($insert_data_aptunitroom)) {
        $real_insert_data['aptunitroom'] = $insert_data_aptunitroom;
        $real_insert_data['aptimportdataaddress'] = $insert_data_aptimportdataaddress;
    }
    if (!empty($insert_data_aptunitroomsyarat)) {
        $real_insert_data['aptunitroomsyarat'] = $insert_data_aptunitroomsyarat;
        $real_insert_data['aptunitroomdatabap'] = $insert_data_aptdatabap;
    }

    $gagal = array();
    $dbase = new Sereware\Query($appimport->idconnection);


    // var_dump(json_encode($real_insert_data));
    // exit();

    if (!empty($real_insert_data)) {
        foreach($real_insert_data as $tablename => $data) {
            try {
                $db = $dbase->table($tablename);
                $db->insert($data);
            }catch(Exception $e){
                $gagal['insert'][$tablename] = $e->getMessage();
            }
        }
    }

    if (!empty($update_data)) {
        foreach($update_data as $tablename => $datas) {
            foreach($datas as $noid => $data) {
                try {
                    $db = $dbase->table($tablename);
                    $db->where('noid','=',$noid);
                    $db->update($data);
                }catch(Exception $e){
                    $gagal['update'][$tablename] = $e->getMessage();
                }
            }
        }
    }

    ob_start();
    echo !count($gagal);
    ob_flush();

}
#}}}

}

我仍在使用本地主机,并崇高文本。

“ altaddress”和“ homeaddress”不在代码内,因为它是表中的一列。您只能在代码上找到一个表名。

我的问题是如何使aptimportdata表中的“ altaddress”数据进入aptimportdataaddress表,我不希望altaddress进入aptimportdata表,我只希望数据进入aptimportdataaddress。

0 个答案:

没有答案