Importing and saving data from csv file in yii(在 yii 中从 csv 文件导入和保存数据)
问题描述
这是我的 csv 文件的样子:
This is what my csv file looks like:
我有三个字段(import site
、import goods
、import status
)来导入各自的数据.因此,如果用户点击import site
,则只会保存站点名称,其余的不会保存,对于import goods
和import status
也是如此.
I have three fields (import site
, import goods
, import status
) to import respective data. 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
.
site's
数据和 status'
数据保存在一个表中,而 goods
数据保存在其他表中.地点.如何将它们保存到多个表中?
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. How do i save them into multiple tables?
推荐答案
我将分享一个简单的片段来解析 csv 文件,也许这可以帮助
I will share simple snippet to parse csv files, maybe this can help
$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);
}
这将完成这项工作,我总是在使用 csv 时使用它.为了使这个工作第一行必须包含键,例如:
This will do the job, i'm using it always when come to csv. To make this work 1st line must contain keys, for example:
import_site, import_goods, import_status
在您的 $output
数组中,您将拥有带有键 $output["import_site"]
等的数据.
In your $output
array you'll have data with keys $output["import_site"]
and so on.
希望这会有所帮助.
这篇关于在 yii 中从 csv 文件导入和保存数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在 yii 中从 csv 文件导入和保存数据
基础教程推荐
- Doctrine 2 - 在多对多关系中记录更改 2022-01-01
- PHP 守护进程/worker 环境 2022-01-01
- 如何在 Symfony 和 Doctrine 中实现多对多和一对多? 2022-01-01
- 如何在 XAMPP 上启用 mysqli? 2021-01-01
- 找不到类“AppHttpControllersDB",我也无法使用新模型 2022-01-01
- 使用 PDO 转义列名 2021-01-01
- 在 CakePHP 2.0 中使用 Html Helper 时未定义的变量 2021-01-01
- phpmyadmin 错误“#1062 - 密钥 1 的重复条目‘1’" 2022-01-01
- 在 yii2 中迁移时出现异常“找不到驱动程序" 2022-01-01
- HTTP 与 FTP 上传 2021-01-01