<?php
ini_set('memory_limit','64M');
ini_set('error_reporting', E_ALL);

function csvstring_to_array($string, $separatorChar = ',', $enclosureChar = '"', $newlineChar = "\n") {
    // @author: Klemen Nagode
    $array = array();
    $size = strlen($string);
    $columnIndex = 0;
    $rowIndex = 0;
    $fieldValue="";
    $isEnclosured = false;
    for($i=0; $i<$size;$i++) {

        $char = $string{$i};
        $addChar = "";

        if($isEnclosured) {
            if($char==$enclosureChar) {

                if($i+1<$size && $string{$i+1}==$enclosureChar){
                    // escaped char
                    $addChar=$char;
                    $i++; // dont check next char
                }else{
                    $isEnclosured = false;
                }
            }else {
                $addChar=$char;
            }
        }else {
            if($char==$enclosureChar) {
                $isEnclosured = true;
            }else {

                if($char==$separatorChar) {

                    $array[$rowIndex][$columnIndex] = $fieldValue;
                    $fieldValue="";

                    $columnIndex++;
                }elseif($char==$newlineChar) {
                    echo $char;
                    $array[$rowIndex][$columnIndex] = $fieldValue;
                    $fieldValue="";
                    $columnIndex=0;
                    $rowIndex++;
                }else {
                    $addChar=$char;
                }
            }
        }
        if($addChar!=""){
            $fieldValue.=$addChar;

        }
    }

    if($fieldValue) { // save last field
        $array[$rowIndex][$columnIndex] = $fieldValue;
    }
    return $array;
}

$csvArr = csvstring_to_array(file_get_contents('PriceFile_IM_Ver1.csv'));
    include("db.php");
    $counter = 0;
    set_time_limit(0);
    foreach($csvArr as $registro){
            $registro1 = implode(",",$registro);
            $sku                       = str_replace('"', '',trim(stripslashes($registro[0])));
            $manufacturers_part_nbr    = str_replace('"', '',trim(stripslashes($registro[1])));
            $upc_nbr                   = str_replace('"', '',trim(stripslashes($registro[2])));
            $vendor_name               = str_replace('"', '',trim(stripslashes($registro[3])));
            $vendor_nbr                = intval($registro[4]);
            $product_description_1     = str_replace('"', '',trim(stripslashes($registro[5])));
            $product_description_2     = str_replace('"', '',trim(stripslashes($registro[6])));
            $new_category              = str_replace('"', '',trim(stripslashes($registro[7])));
            $new_sub_category          = str_replace('"', '',trim(stripslashes($registro[8])));
            $new_cpu_code              = str_replace('"', '',trim(stripslashes($registro[9])));
            $new_media_code            = str_replace('"', '',trim(stripslashes($registro[10])));

            $weight                    = str_replace('"', '',trim(stripslashes($registro[14])));
            $height                    = str_replace('"', '',trim(stripslashes($registro[15])));
            $width                     = str_replace('"', '',trim(stripslashes($registro[16])));
            $lenght                     = str_replace('"', '',trim(stripslashes($registro[17])));
            
            
            $rs_if_exist = mysql_query("SELECT * FROM price_file_copy WHERE sku = '".stripslashes(trim($sku))."'");       
                if($rs_if_exist && mysql_num_rows($rs_if_exist)>0){
                    $row = mysql_fetch_array($rs_if_exist);
                        $rs_a = mysql_query("UPDATE price_file_copy SET length = '".$lenght."' WHERE id = ".$row["id"]);                        
                        $counter++;
                    }   
            
    }

    echo "<p>Actualizaciones:".$counter."</p>";

    
    
?>