Insert id (auto increment) into other table in same moment

Topics: Developer Forum, Project Management Forum, User Forum
Jun 7 at 8:27 AM
I have 3 tables with name tbl_m_name, tbl_m_office and tbl_m_misc.

In tbl_m_name I have an auto increment id column named id_name.

I want id_name value in every tbl_m_office and tbl_m_misc row but I don't know what should I do nor place should I insert the code

tbh the code that I have is mostly modified copy-paste so I don't exactly know what every line means

by that in mind please help me what should I do
<?php
class Tools
{
    
    function importdata($file,$table1,$table2,$table3,$startRow,$checkPrimary=FALSE)
    {
        $CI=& get_instance();
        $CI->load->database();
        $CI->load->library('phpexcel');
        $CI->load->library('PHPExcel/iofactory');
        $objPHPExcel = new PHPExcel();
        try{
            $inputFileType = IOFactory::identify($file);
            $objReader = IOFactory::createReader($inputFileType);
            $objPHPExcel = $objReader->load($file);
        } catch (Exception $ex) {
            die("Tidak dapat mengakses file ".$ex->getMessage());
        }
        
        $sheet1 = $objPHPExcel->getSheet(0);
        $highestRow = $sheet1->getHighestRow();
        $highestColumn = $sheet1->getHighestColumn();
        
        for ($row = $startRow; $row <= $highestRow; $row++){
            $rowData = $sheet1->rangeToArray('A' . $row . ':' . $highestColumn . $row,NULL,TRUE,FALSE);
            $startCol=0;
            if($checkPrimary==TRUE)
            {
                $startCol=0;
            }else{
                $startCol=1;
            }
                    
            $fields = $CI->db->list_fields('tbl_m_name');
            $countfield=count($fields)-1;
            
            $datacol=array();
            $dataxl=array();
            $colXl=-1;
            for($col=$startCol;$col<=$countfield;$col++)
            {
                $colXl+=1;                
                $datacol[]=$fields[$col];
                $dataxl[]=$rowData[0][$colXl];
            }
            $data=array_combine($datacol,$dataxl);
            $CI->db->insert($table1,$data);    
        }

        $sheet2 = $objPHPExcel->getSheet(1);
        $highestRow = $sheet2->getHighestRow();
        $highestColumn = $sheet2->getHighestColumn();
        
        for ($row = $startRow; $row <= $highestRow; $row++){
            $rowData = $sheet2->rangeToArray('A' . $row . ':' . $highestColumn . $row,NULL,TRUE,FALSE);
            $startCol=0;
            if($checkPrimary==TRUE)
            {
                $startCol=0;
            }else{
                $startCol=1;
            }
                    
            $fields = $CI->db->list_fields('tbl_m_office');
            $countfield=count($fields)-1;
            
            $datacol=array();
            $dataxl=array();
            $colXl=-1;
            for($col=$startCol;$col<=$countfield;$col++)
            {
                $colXl+=1;                
                $datacol[]=$fields[$col];
                $dataxl[]=$rowData[0][$colXl];
            }
            $data=array_combine($datacol,$dataxl);
            $CI->db->insert($table2,$data);        
        }

        $sheet3 = $objPHPExcel->getSheet(2);
        $highestRow = $sheet3->getHighestRow();
        $highestColumn = $sheet3->getHighestColumn();
        
        for ($row = $startRow; $row <= $highestRow; $row++){
            $rowData = $sheet3->rangeToArray('A' . $row . ':' . $highestColumn . $row,NULL,TRUE,FALSE);
            $startCol=0;
            if($checkPrimary==TRUE)
            {
                $startCol=0;
            }else{
                $startCol=1;
            }
                    
            $fields = $CI->db->list_fields('tbl_m_misc');
            $countfield=count($fields)-1;
            
            $datacol=array();
            $dataxl=array();
            $colXl=-1;
            for($col=$startCol;$col<=$countfield;$col++)
            {
                $colXl+=1;                
                $datacol[]=$fields[$col];
                $dataxl[]=$rowData[0][$colXl];
            }
            $data=array_combine($datacol,$dataxl);
            $CI->db->insert($table3,$data);        
        }


    }        
}
?>