public function student()
{
if($this->input->post('save'))
{
$client_id[0]['client_id'] = $this->session->userdata('client_id');
$radio = $this->input->post('class');
$client = $client_id[0]['client_id'];
$filename = $_FILES['students_list']['name'];
$path = base_url()."resources/imported_file/".$filename;
$path = FCPATH."resources/imported_file/";
$move=move_uploaded_file($_FILES['students_list']['tmp_name'],$path.$_FILES['students_list']['name']);
if($_FILES["students_list"]["size"] > 0)
{
$file = fopen($path, "r");
while (($importdata = fgetcsv($file, 10000, ",")) !== FALSE)
{
$data = array(
'name' => $importdata[0],
'email' =>$importdata[1],
'phone' =>$importdata[2],
'uploaded_date' => date('d-m-y'),
'session' => date('Y'),
'client_id' => $client[0]['client_id'],
'class' => $radio
);
$this->partner_model->insertCSV($data);
echo $this->db->last_query();
}
fclose($file);
$this->session->set_flashdata('err_csv', '<p style="color: #87b87f;font-weight: bold;text-align:center;">Data are imported successfully..</p>');
}
else
{
$this->session->set_flashdata('err_csv', '<p style="color: red;font-weight: bold;text-align:center;">Something went wrong..</p>');
}
}
}
視圖:
<?php echo $this->session->flashdata('err_csv')?>
<form class="form-horizontal" method="post" role="form" enctype="multipart/form-data">
<input type="radio" name="class" value="11th"/><b>XI</b>
<input type="radio" name="class" value="12th Appearing"/><b>XII Appearing</b>
<input type="radio" name="class" value="12th Passed"/><b>XII Passed</b>
<input type="file" id="students_list" name="students_list" accept=".csv" class="required">
<input type="submit" name="save" id="save" value="save" class="btn btn-info" />
</form>
模型:
<?php
class Partner_model extends CI_Model
{
function __construct()
{
parent::__construct();
}
public function insertCSV($data)
{
$this->db->insert('students', $data);
return TRUE;
}
}
在此代碼我有一個具有輸入名稱student_list我在哪裏上傳僅csv文件。現在,我想從csv文件導入數據到數據庫,但現在它無法工作。那麼,我該怎麼做?請幫幫我。
謝謝
它顯示無法加載所請求的類:CSVReader – omkara
您需要加載庫文件夾中的csvReader庫 – Madhur
此示例可幫助您逐行獲取csv文件中的數據,並在涉及列標題的第1行之後嘗試獲取其他行然後將它們插入數據庫。 – Madhur