Issue
This is what my csv file looks like:
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
.
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?
Solution
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);
}
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
In your $output
array you'll have data with keys $output["import_site"]
and so on.
Hope this will be helpfull.
Answered By - ineersa
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.