0

Im using a CSV Import plugin for open cart and by default is completely skips products if they are missing any of the column fields. Is there anyway i can read the file and replace all the empty fields with 'null' or 0 and send it back to the code.

skipping that check below causes an offset in the reading/placing format !

    $fh = fopen($file, 'r');
    if(!$fh) die('File no good!');

    // Get headings
    $headings = fgetcsv($fh, 0, $delim);
    $num_cols = count($headings);
    $num_rows = 0;

    //Read the file as csv
    while (($row = fgetcsv($fh, 0, $delim)) !== FALSE) {

         //missed product if num columns in this row not the same as num headings
     if (count($row) != $num_cols) {
        $this->total_items_missed++;
        continue;
        }   


        for ($i=0; $i<count($headings); $i++) {
            $raw_prod[$headings[$i]] = $row[$i];
        }
2
  • 1
    remove the 4 lines that do the check, depending what you do with the data next that may be all you need. Commented Oct 24, 2011 at 19:42
  • that just causes an offset here then, $raw_prod[$headings[$i]] = $row[$i]; Commented Oct 24, 2011 at 19:58

2 Answers 2

1

Replace these two lines

$this->total_items_missed++;
continue;

with

$row = array_pad($row, $num_cols, 0);

Which will add any missing values with 0's

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

2 Comments

thanks, that worked great ! not sure wether one below would suit better, i assume they provide same result. Never used array_pad bfore myself
You could only use that if you removed the column count check really...so no it wouldn't work as you want. Array pad is one of those functions that you just have to remember. I've barely ever used it myself either
1

try

for($i = 0 ; $i<count($headings) ; $i++){
    if(!empty($row[$i])){
        $raw_prod[$headings[$i]] = $row[$i];
    }else{
        $raw_prod[$headings[$i]] = 0;//or what ever value you want
    }
}

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.