假设这是我的 csv 文件
fileempId,lastName,firstName,middleName,street1,street2,city,state,zip,gender,birthDate,ssn,empStatus,joinDate,workStation,location,custom1,workState,salary,payFrequency,FITWStatus,FITWExemptions,DD1Routing,DD1Account,DD1Amount,DD1AmountCode,DD1Checking,DD2Routing,DD2Account,DD2Amount,DD2AmountCode,DD2Checking
1,Dela Cruz,Juano,Santos,,,,,,1,,,Part Time Internship,, asd Division, Makati,one, asd,150,Bi Weekly,Not Applicable,100,,,,,,1234,9876,100,SAVINGS,BLANK
3,Palogan,Ralph,,,,,,,1,11-Mar-11,,Full Time Contract,2-Mar-11, sdf Department, pasay,, ,,,Not Applicable,,,,,,,,,,, 5,San,Goku,,,,hidden leaf,,,1,11-Mar-11,,,,,,,,,,Not Applicable,0,,,,,,,,,,
这是我的表格
<label>Choose File:</label><font color="#FF0000">*</font>
<input type="file" name="file" id="file" />
<input type="button" id="importButton" value="Import" name="importButton" />
如何读取csv中的数据并将其存储到mysql数据库(codeigniter)?有关如何执行此操作的任何示例代码,。
请在下面查看我的代码有什么问题,..,
我的视图页面包含 jquery 和表单,,。
<td><label>Choose File:</label><font color="#FF0000">*</font></td>
<td><input type="file" name="file" id="file" /></td>
<td><label>Import Type </label><font color="#FF0000">*</font></td>
<td><select name="importname" id="importname" style="width:130px;">
<option value="" selected>--Select--</option>
<?php
foreach($fields as $data){
print '<option value="'.$data->import_id.'">'.$data->name.'</option>';
}
?>
</select></td>
<script type="text/javascript">
$(function() {
$("#importData").click(function(e) {
var file = $('#file').val();
var type = $('#importname').val();
$.post("<?php print base_url().'index.php/MyController/readExcel'?>",{file: file, type: type},
function(data)
{
if(data!='success')
{
error_message(data);
}
else{
alert("Upload Succcessfull!");
}
});
});
});
</script>
我的控制器
function readExcel(){
$file=$this->input->post('file');
$type=$this->input->post('type');
$this->load->library('csvreader');
$result = $this->csvreader->parse_file($file);
$data['csvData'] = $result;
$this->load->view('MyViews/showImportFile', $data);
}
我的图书馆
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
class csvreader {
var $fields; /** columns names retrieved after parsing */
var $separator = ';'; /** separator used to explode each line */
var $enclosure = '"'; /** enclosure used to decorate each field */
var $max_row_size = 4096; /** maximum row size to be used for decoding */
function parse_file($p_Filepath) {
$file = fopen($p_Filepath, 'r');
$this->fields = fgetcsv($file, $this->max_row_size, $this->separator, $this->enclosure);
$keys_values = explode(',',$this->fields[0]);
$content = array();
$keys = $this->escape_string($keys_values);
$i = 1;
while( ($row = fgetcsv($file, $this->max_row_size, $this->separator, $this->enclosure)) != false ) {
if( $row != null ) { // skip empty lines
$values = explode(',',$row[0]);
if(count($keys) == count($values)){
$arr = array();
$new_values = array();
$new_values = $this->escape_string($values);
for($j=0;$j<count($keys);$j++){
if($keys[$j] != ""){
$arr[$keys[$j]] = $new_values[$j];
}
}
$content[$i]= $arr;
$i++;
}
}
}
fclose($file);
return $content;
}
function escape_string($data){
$result = array();
foreach($data as $row){
$result[] = str_replace('"', '',$row);
}
return $result;
}
}
我收到一个错误
遇到 PHP 错误
严重性:警告
消息:fopen(export 1.csv):无法打开流:没有这样的文件或目录
文件名:库/csvreader.php
有人可以帮我吗!!!