dongsiju1941 2017-08-23 11:45
浏览 37

使用Codeigniter从mysql数据库中获取值,从而在CSV文件中下拉列表

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.

enter image description here

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.

enter image description here

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>&nbsp;<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.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥17 pro*C预编译“闪回查询”报错SCN不能识别
    • ¥15 微信会员卡接入微信支付商户号收款
    • ¥15 如何获取烟草零售终端数据
    • ¥15 数学建模招标中位数问题
    • ¥15 phython路径名过长报错 不知道什么问题
    • ¥15 深度学习中模型转换该怎么实现
    • ¥15 HLs设计手写数字识别程序编译通不过
    • ¥15 Stata外部命令安装问题求帮助!
    • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
    • ¥15 TYPCE母转母,插入认方向