我试图上传一个csv文件,这是一个微软excel类型的文件,似乎有一个问题,在导入一个csv文件。
控制器(csv\u import.php):
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Csv_import extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->model('csv_import_model');
$this->load->library('csvimport');
}
function index()
{
$this->load->view('csv_import');
$this->load_data();
}
function load_data()
{
$result = $this->csv_import_model->select();
$output = '
<h3 align="center">Imported User Details from CSV File</h3>
<div class="table-responsive">
<table class="table table-bordered table-striped">
<tr>
<th>ID Number</th>
<th>First Name</th>
<th>Last Name</th>
<th>Gender</th>
<th>Course</th>
<th>Year</th>
</tr>
';
$count = 0;
if($result->num_rows() > 0)
{
foreach($result->result() as $row)
{
$count = $count + 1;
$output .= '
<tr>
<td>'.$count.'</td>
<td>'.$row->fname.'</td>
<td>'.$row->lname.'</td>
<td>'.$row->gndr.'</td>
<td>'.$row->crse.'</td>
<td>'.$row->yr.'</td>
</tr>
';
}
}
else
{
$output .= '
<tr>
<td colspan="6" align="center">Data not Available</td>
</tr>
';
}
$output .= '</table></div>';
echo $output;
}
function import()
{
$file_data = $this->csvimport->get_array($_FILES["csv_file"]
["tmp_name"]);
foreach($file_data as $row)
{
$data[] = array(
'id_number' => $row["ID Number"],
'fname' => $row["First Name"],
'lname' => $row["Last Name"],
'gndr' => $row["Gender"],
'crse' => $row["Course"],
'yr' => $row["Year"]
);
}
$this->csv_import_model->insert($data);
}
}
模型(csv\u import\u model.php):
<?php
class Csv_import_model extends CI_Model
{
function select()
{
$this->db->order_by('id_number', 'DESC');
$query = $this->db->get('tbl_user');
return $query;
}
function insert($data)
{
$this->db->insert_batch('tbl_user', $data);
}
}
以及视图(csv\u import.php):
<html>
<head>
<title>CSV Sample</title>
<script type="text/javascript" src="<?php echo base_url();
>bootstrap/js/jquery.js" ></script>
<script type="text/javascript" src="<?php echo base_url();
>bootstrap/dist/js/bootstrap.js" ></script>
<link href="<?php echo base_url()?>bootstrap/dist/css/bootstrap.css"
rel="stylesheet" type="text/css"/>
</head>
<body>
<div class="container box">
<h3 align="center">CSV Sample</h3>
<br />
<form method="post" id="import_csv" enctype="multipart/form-data">
<div class="form-group">
<label>Select CSV File</label>
<input type="file" name="csv_file" id="csv_file" required accept=".csv" />
</div>
<br />
<button type="submit" name="import_csv" class="btn btn-info"
id="import_csv_btn">Import CSV</button>
</form>
<br />
<div id="imported_csv_data"></div>
</div>
</body>
</html>
<script>
$(document).ready(function(){
load_data();
function load_data()
{
$.ajax({
url:"<?php echo base_url(); ?>csv_import/load_data",
method:"POST",
success:function(data)
{
$('#imported_csv_data').html(data);
}
})
}
$('#import_csv').on('submit', function(event){
event.preventDefault();
$.ajax({
url:"<?php echo base_url(); ?>csv_import/import",
method:"POST",
data:new FormData(this),
contentType:false,
cache:false,
processData:false,
beforeSend:function(){
$('#import_csv_btn').html('Importing...');
},
success:function(data)
{
load_data();
$('#import_csv')[0].reset();
$('#import_csv_btn').attr('disabled', false);
$('#import_csv_btn').html('Import Done');
}
})
});
});
</script>
当我单击import按钮时,什么也没有发生,我也在运行代码之前下载了csv库。顺便说一下,我用的是codeigniter
1条答案
按热度按时间s5a0g9ez1#
哈哈,我知道已经一年了,但不知怎么的,我设法回来解决这个问题。我想我自己的问题是,我从另一个源代码复制了这段代码,并保证这段代码100%正常工作,所以为了让这段代码正常工作,我编辑了控制器,而不是:
我将$output替换为echo,然后应该是这样的:
终于成功了。