I am trying to import a csv file to my MySql database. In my page I will provide a sample csv file to download for users and in that csv file, users need to enter corresponding value and they will upload it. The values are getting inserted into the table. But, if I try to insert csv file into a table where we are having foreign keys then, I want the corresponding list in the csv for users to select its corresponding name from master table. Below is my database to which I need to import csv.
As you can see, parent_id is the id of subcategory table. I want the subcategory table's titles in dropdown list inside csv.
The expected output is like the given demo image below.
I am doing this in Codeigniter.
Controller Code
foreach ($csv_array as $row) {
$insert_data1 = array(
'parent_id'=>$row['Subcategory'],
'name' => $row['Name'],
'phone'=>$row['Phone'],
'mobile'=>$row['Mobile'],
'work_hr_from'=>$row['Work_hr_from'],
'work_hr_to'=>$row['Work_hr_to'],
'work_days'=>$row['Work_days'],
'address'=>$row['Address'],
'city_id'=>$row['City'],
'email'=>$row['Email'],
'website'=>$row['Website'],
'gmap_lat'=>$row['Gmap_latitude'],
'gmap_lng'=>$row['Gmap_longitude'],
'fax'=>$row['Fax'],
'start_year'=>$row['Start_year'],
);
$table ='item';
$st_id = $this->excel_data_insert_model->add_excel_details($insert_data1);
}
View Code
Code for downloading sample csv
<a href="#" data-toggle="popover" title="Excel Format : <a href='<?php echo base_url(); ?>uploads/excel/formatcsv.csv'><i class='fa fa-download'></i></a>" data-content="" data-html="true">Download Format</a>
Form action to controller
<form action="<?php echo base_url();?>ExcelUpload/ExcelDataAdd" method="post" enctype="multipart/form-data">
<div class="box-body">
<div class="col-md-6">
<div class="form-group">
<label for="exampleInputFile">File input</label> <label style="color: red">*</label>
<input type="file" name="excel" id="excel" required="required">
</div>
</div>
</div>
<div class="box-footer">
<button type="submit" class="btn btn-primary">Submit</button>
</div>
</form>
Model Code
public function add_excel_details($data_user){
$this->db->insert('item', $data_user);
}
Thanks in advance.