【发布时间】:2019-01-01 19:46:39
【问题描述】:
我希望网站管理员选择带有文件输入的 CSV 文件。包含用户的 CSV 文件,然后使用随机生成的密码插入数据库。对于插入数据库的每个用户,用户将收到一封邮件,其中包含他们的登录名(即他们的电子邮件)和密码。
我尝试使用库在数组中获取 CSV 内容,但我没有设法使其工作。
这是控制器中读取 CSV 文件的函数
public function lire_csv()
{
$this->load->library('Csvimport');
$file_data = $this->Csvimport->get_array($_FILES["csv_file"]["liste_etu"]);
foreach($file_data as $row)
{
$data[] = array(
'nom' => $row["Nom"],
'prenom' => $row["Prenom"],
'statut'=> "Etudiant",
'civilite' => $row["Civilite"],
'mail' => $row["Email"],
'adresse' => $row["Adresse"],
'complement' => $row["Complement"],
'code_postal' => $row["Code_postal"],
'ville' => $row["Ville"],
'pays' =>$row["Pays"],
'tel' => $row["Telephone"],
'date_naiss' => $row["Date_de_naissance"],
'num_secu' => $row["Numero_secu"],
'mutuelle' => $row["Mutuelle"],
'objectif_pro' => $row["Objectif_pro"],
'motdepasse' => uniqid()
);
$this->envoyer_mail($data['mail'],$data['motdepasse'],$data['nom'],$data['prenom']);
}
$this->mStages->create_utilisateur($data);
}
这是我使用的库CSV Import
这个函数给我关于时间设置的警告
最后是管理员必须选择 CSV 文件的视图代码部分。有一个奇怪的行为,因为只有应该打开文件资源管理器的按钮周围的矩形有效,但按钮本身什么也不做。
<form action=<?php echo base_url('index.php/main_stage/lire_csv'); ?> method="post">
<center><input type="file" name="fichier" id="csv_file" required accept=".csv"/></center> <p>Fichier .csv uniquement</p>
<input type="submit" value="Envoyer la liste">
当我尝试发送 CSV 时出现以下错误:
A PHP Error was encountered
Severity: Notice
Message: Undefined property: Main_stage::$Csvimport
Filename: controllers/main_stage.php
Line Number: 507
Backtrace:
File: D:\boulot\php\UwAmp\www\Projet_PHP\CodeIgniter-3.1.9\application\controllers\main_stage.php
Line: 507
Function: _error_handler
File: D:\boulot\php\UwAmp\www\Projet_PHP\CodeIgniter-3.1.9\index.php
Line: 315
Function: require_once
( ! ) Fatal error: Call to a member function get_array() on null in D:\boulot\php\UwAmp\www\Projet_PHP\CodeIgniter-3.1.9\application\controllers\main_stage.php on line 507
Call Stack
# Time Memory Function Location
1 0.0149 150416 {main}( ) ...\index.php:0
2 0.0281 197248 require_once( 'D:\boulot\php\UwAmp\www\Projet_PHP\CodeIgniter-3.1.9\system\core\CodeIgniter.php' ) ...\index.php:315
3 0.3565 2069632 call_user_func_array:{D:\boulot\php\UwAmp\www\Projet_PHP\CodeIgniter-3.1.9\system\core\CodeIgniter.php:532} ( ) ...\CodeIgniter.php:532
4 0.3565 2069832 Main_stage->lire_csv( ) ...\CodeIgniter.php:532
A PHP Error was encountered
Severity: Error
Message: Call to a member function get_array() on null
Filename: controllers/main_stage.php
Line Number: 507
Backtrace:
我想看看如何改进我的代码或更简单的方法来做到这一点。
【问题讨论】:
-
文档中有一个非常重要的部分 - 加载后,您可以使用小写版本访问您的类 - 看看这里 - codeigniter.com/user_guide/general/… - 这意味着的
$this->Csvimport使用$this->csvimport
标签: php codeigniter csv