title: Import excel into multiple tables - PHP and MYSQL (codeigniter)
body: <p>with the function below, you can import data from an Excel spreadsheet to the database via PHP.</p>
  
  <p>A table <code>client</code> contains the following fields:</p>
  
  <pre>
  `id` INT(11) NOT NULL AUTO_INCREMENT,
  `name` VARCHAR(255) NOT NULL,
  `address` VARCHAR(255) NOT NULL,
  </pre>
  
  <p>But I need the columns: <code>$invoice, $expiry, $value</code> to import into another invoice table, which will contain the following fields:</p>
  
  <pre>
  `client_id` INT(11) NOT NULL AUTO_INCREMENT,
  `invoice` VARCHAR(20) NOT NULL,
  `expiry` VARCHAR(20) NOT NULL,
  `value` VARCHAR(20) NULL DEFAULT NULL,
  </pre>
  
  <p>where client_id will receive the client ID.</p>
  
  <p>I am using the following method to import:</p>
  
  <p><strong>controller</strong></p>
  
  <pre><code>function import()
      {
          if(isset($_FILES["file"]["name"]))
          {
              $path = $_FILES["file"]["tmp_name"];
              $object = PHPExcel_IOFactory::load($path);
  
  
              foreach($object-&gt;getWorksheetIterator() as $worksheet)
              {               
                  $highestRow    = $worksheet-&gt;getHighestRow();
                  $highestColumn = $worksheet-&gt;getHighestColumn();
  
                  for($row = 2; $row getCellByColumnAndRow(0, $row)-&gt;getValue();
                      $address = $worksheet-&gt;getCellByColumnAndRow(1, $row)-&gt;getValue();
                      $invoice = $worksheet-&gt;getCellByColumnAndRow(2, $row)-&gt;getValue();
                      $expiry  = $worksheet-&gt;getCellByColumnAndRow(3, $row)-&gt;getValue();
                      $value   = $worksheet-&gt;getCellByColumnAndRow(4, $row)-&gt;getValue();
  
                      $data[] = [
                          'name'    =&gt;    $name,
                          'address' =&gt;    $address,
                          'invoice' =&gt;    $invoice,
                          'expiry'  =&gt;    $expiry,
                          'value'   =&gt;    $value
                      ];
                  }
              }
              $this-&gt;excel_import_model-&gt;insert($data);
              echo 'Data Imported successfully';
          }
      }
  </code></pre>
  
  <p><strong>Model</strong></p>
  
  <pre><code>function insert($data)
  {
      $this-&gt;db-&gt;insert_batch('client', $data);
      $insert = $this-&gt;db-&gt;insert_id();
  }
  </code></pre>
  
  <p><img src="https://i.stack.imgur.com/PYbjv.png" alt="enter image description here"></p>