PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, January 26, 2022

[FIXED] Importing and saving data from csv file in yii

 January 26, 2022     csv, php, yii     No comments   

Issue

This is what my csv file looks like: enter image description here

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.

enter image description here

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
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing