[英]Importing and saving data from csv file in yii
This is what my csv file looks like: 这是我的csv文件的样子:
I have three fields ( import site
, import goods
, import status
) to import respective data. 我有三个字段( import site
, import goods
, import status
)来导入相应的数据。 So if a user clicks on import site
only sites name will get saved and the rest won't, similarly for import goods
and import status
. 因此,如果用户单击import site
只会保存站点名称,其余名称将不会保存, import goods
和import status
。
The site's
data and status's
data are saved in a single table but the goods
data is being saved in other table with respect to it's site. site's
数据和status's
数据保存在单个表中,但goods
数据将保存在与其站点相关的其他表中。 How do i save them into multiple tables? 如何将它们保存到多个表中?
I will share simple snippet to parse csv files, maybe this can help 我将分享简单的代码片段来解析csv文件,也许这可以帮助
$i=0; $keys=array();$output=array();
$handle=fopen($filename, "r");
if ($handle){
while(($line = fgetcsv($handle)) !== false) {
$i++;
if ($i==1) {
$keys=$line;
}
elseif ($i>1){
$attr=array();
foreach($line as $k=>$v){
$attr[$keys[$k]]=$v;
}
$output[]=$attr;
}
}
fclose($handle);
}
This will do the job, i'm using it always when come to csv. 这将完成这项工作,我总是在使用它时来到csv。 To make this work 1st line must contain keys, for example: 要使这项工作第1行必须包含键,例如:
import_site, import_goods, import_status
In your $output
array you'll have data with keys $output["import_site"]
and so on. 在你的$output
数组中,你将拥有带有$output["import_site"]
键的数据,依此类推。
Hope this will be helpfull. 希望这会有所帮助。
In case problems with csv generated on mac remember do this: 如果在mac上生成的csv问题,请记住这样做:
ini_set("auto_detect_line_endings", true);
can save a lots of time ;) 可以节省很多时间;)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.