1

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?

4
  • once you extract the data, use an event handler to save information in parent and child tables? - have to implemented anything on this yet, show us some code Commented Jan 16, 2014 at 7:15
  • Here is the link : pastebin.com/jxAPVF03 Please let me know what am i doing wrong.(it is only a portion of the code) Commented Jan 16, 2014 at 7:27
  • on if (!$modelSiteManagement->save()) {, extract the id of the saved model from the variable and save the other record in the child table if I am assuming what you are trying to do here right. Commented Jan 16, 2014 at 7:46
  • @themosquitokiller how do i read the next line in the csv file, i only get the first row of the file. Commented Jan 16, 2014 at 9:05

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

Comments

0

In case problems with csv generated on mac remember do this:

ini_set("auto_detect_line_endings", true);

can save a lots of time ;)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.