I want to create an import from excel to database. But in one of the inputs are using <select> which has the value of an id. like this:
<select id="merchant" name="merchant">
<?php
foreach($merchant as $opt_merchant){
?>
<option value="<?= $opt_merchant['merchant_id'] ?>"><?= $opt_merchant['merchant_name'] ?></option>
<?php
}
?>
</select>
How do I get the ID when the user only input the merchant_name in the excel?
my senior told me to get all the merchants first, then when it loops, just search for the object by name, then get the id. But I don’t know how to get each row of merchant_name and then search for their id.
the controller for the import:
public function import_retail_proses(){
$this->load->library(array('excel','session'));
if (isset($_FILES["fileExcel"]["name"])) {
$path = $_FILES["fileExcel"]["tmp_name"];
$object = PHPExcel_IOFactory::load($path);
foreach($object->getWorksheetIterator() as $worksheet)
{
$highestRow = $worksheet->getHighestRow();
$highestColumn = $worksheet->getHighestColumn();
$values = [];
for($row=2; $row<=$highestRow; $row++)
{
$pos_pelanggan_code = $worksheet->getCellByColumnAndRow(0, $row)->getValue();
$pos_merchant = $worksheet->getCellByColumnAndRow(1, $row)->getValue();
$pos_name = $worksheet->getCellByColumnAndRow(2, $row)->getValue();
$pos_sales_name = $worksheet->getCellByColumnAndRow(3, $row)->getValue();
$pos_address = $worksheet->getCellByColumnAndRow(4, $row)->getValue();
$pos_email = $worksheet->getCellByColumnAndRow(5, $row)->getValue();
$pos_phone = $worksheet->getCellByColumnAndRow(6, $row)->getValue();
$pos_website = $worksheet->getCellByColumnAndRow(7, $row)->getValue();
$pos_npwp = $worksheet->getCellByColumnAndRow(8, $row)->getValue();
$pos_post_code = $worksheet->getCellByColumnAndRow(9, $row)->getValue();
$pos_fax = $worksheet->getCellByColumnAndRow(10, $row)->getValue();
$pos_vip = $worksheet->getCellByColumnAndRow(11, $row)->getValue();
$values[] = array(
'kode_pelanggan' => $pos_pelanggan_code,
'nama' => $pos_name,
'vip' => 0,
'tgl_daftar' => date('Y-m-d H:i:s'),
'alamat' => $pos_address,
'email' => $pos_email,
'no_hp' => $pos_phone,
'website' => $pos_website,
'no_npwp' => $pos_npwp,
'kode_pos' => $pos_post_code,
'no_fax' => $pos_fax,
'limit' => 0,
'top' => 0,
'merchant' => $pos_merchant,
'jenis_pelanggan' => 0
);
}
}
$insert = $this->db->insert_batch('pelanggan',$values);
if( $insert ) {
$this->session->set_flashdata('success','Import item');
redirect($_SERVER['HTTP_REFERER']);
}else{
print_r('error ');die;
}
} else {
echo "Tidak ada file yang masuk";die;
}
}