Save associative array in mysql with modified parameters

0

My intention is to read a csv file, pass it through a form to tell where each column goes in relation to mysql and save it in mysql with the new design going through all the records.

The fact is that I already have the code to read what would be this:

    $registros = array();
if (($fichero = fopen($RutaDocumentos.'/'.$_POST["Destino"].".csv", "r")) !== FALSE) { //abrimos el csv y lo leemos
// Lee los nombres de los campos
    $nombres_campos = fgetcsv($fichero, 0, $_POST["Separador"], "\"", "\"");
    $num_campos = count($nombres_campos);
// Lee los registros
    while (($datos = fgetcsv($fichero, 0, $_POST["Separador"], "\"", "\"")) !== FALSE) { // Crea un array asociativo con los nombres y valores de los campos
        for ($icampo = 0; $icampo < $num_campos; $icampo++) { $registro[$nombres_campos[$icampo]] = $datos[$icampo]; }
// Añade el registro leido al array de registros
    $registros[] = $registro;
}
fclose($fichero);
} //cerramos la lectura del csv

This generates an associative array with this output:

array(4) { [0]=> array(29) { ["Campo 1"]=> string(21) "Buy Electrical Direct" ["Campo 2"]=> string(26) "http://www.be-direct.co.uk" ["Campo 3"]=> string(19) "01/06/2005 04:46:48" ["Campo 4"]=> string(33) "Stoves 60cm Wide Dual Fuel Cooker" ["Campo 5"]=> string(6) "61DFDO" ["Campo 6"]=> string(33) "Stoves 60cm Wide Dual Fuel Cooker" ["Campo 7"]=> string(5) "16488" ["Campo 8"]=> string(6) "Stoves" ["Campo 9"]=> string(6) "61DFDO" ["Campo 10"]=> string(0) "" ["Campo 11"]=> string(0) "" ["Campo 12"]=> string(3) "GBP" ["Campo 13"]=> string(4) "0.00" ["Campo 14"]=> string(6) "583.99" ["Campo 15"]=> string(4) "0.00" ["Campo 16"]=> string(0) "" ["Campo 17"]=> string(146) "http://www.kqzyfj.com/click-1679007-10385498?url=http%3A%2F%2Fwww.be-direct.co.uk%2FproductDetails.asp%3Freferer%3Dcommissionjunction%26id%3D16488" ["Campo 18"]=> string(46) "http://www.awltovhc.com/image-1679007-10385498" ["Campo 19"]=> string(0) "" ["Campo 20"]=> string(32) "Cooking (cookers and microwaves)" ["Campo 21"]=> string(0) "" ["Campo 22"]=> string(0) "" ["Campo 23"]=> string(0) "" ["Campo 24"]=> string(0) "" ["Campo 25"]=> string(0) "" ["Campo 26"]=> string(0) "" ["Campo 27"]=> string(0) "" ["Campo 28"]=> string(0) "" ["Campo 29"]=> string(0) "" } [1]=> array(29) { ["Campo 1"]=> string(21) "Buy Electrical Direct" ["Campo 2"]=> string(26) "http://www.be-direct.co.uk" ["Campo 3"]=> string(19) "01/06/2005 04:46:48" ["Campo 4"]=> string(53) "Hotpoint Ultima 60cm Gas Cooker with Sandstone Finish" ["Campo 5"]=> string(5) "EG73T" ["Campo 6"]=> string(53) "Hotpoint Ultima 60cm Gas Cooker with Sandstone Finish" ["Campo 7"]=> string(5) "16122" ["Campo 8"]=> string(8) "Hotpoint" ["Campo 9"]=> string(5) "EG73T" ["Campo 10"]=> string(0) "" ["Campo 11"]=> string(0) "" ["Campo 12"]=> string(3) "GBP" ["Campo 13"]=> string(4) "0.00" ["Campo 14"]=> string(6) "473.99" ["Campo 15"]=> string(4) "0.00" ["Campo 16"]=> string(0) "" ["Campo 17"]=> string(147) "http://www.tkqlhce.com/click-1679007-10385498?url=http%3A%2F%2Fwww.be-direct.co.uk%2FproductDetails.asp%3Freferer%3Dcommissionjunction%26id%3D16122" ["Campo 18"]=> string(45) "http://www.lduhtrp.net/image-1679007-10385498" ["Campo 19"]=> string(64) "http://www.be-direct.co.uk/additional/product_images/id16122.jpg" ["Campo 20"]=> string(32) "Cooking (cookers and microwaves)" ["Campo 21"]=> string(0) "" ["Campo 22"]=> string(0) "" ["Campo 23"]=> string(0) "" ["Campo 24"]=> string(0) "" ["Campo 25"]=> string(0) "" ["Campo 26"]=> string(0) "" ["Campo 27"]=> string(0) "" ["Campo 28"]=> string(0) "" ["Campo 29"]=> string(0) "" } [2]=> array(29) { ["Campo 1"]=> string(21) "Buy Electrical Direct" ["Campo 2"]=> string(26) "http://www.be-direct.co.uk" ["Campo 3"]=> string(19) "01/06/2005 04:46:48" ["Campo 4"]=> string(64) "Hotpoint 60cm Electric Ceramic/Halogen Hob with Sandstone Finish" ["Campo 5"]=> string(5) "EW83T" ["Campo 6"]=> string(64) "Hotpoint 60cm Electric Ceramic/Halogen Hob with Sandstone Finish" ["Campo 7"]=> string(5) "16390" ["Campo 8"]=> string(8) "Hotpoint" ["Campo 9"]=> string(5) "EW83T" ["Campo 10"]=> string(0) "" ["Campo 11"]=> string(0) "" ["Campo 12"]=> string(3) "GBP" ["Campo 13"]=> string(4) "0.00" ["Campo 14"]=> string(6) "503.99" ["Campo 15"]=> string(4) "0.00" ["Campo 16"]=> string(0) "" ["Campo 17"]=> string(147) "http://www.tkqlhce.com/click-1679007-10385498?url=http%3A%2F%2Fwww.be-direct.co.uk%2FproductDetails.asp%3Freferer%3Dcommissionjunction%26id%3D16390" ["Campo 18"]=> string(43) "http://www.tqlkg.com/image-1679007-10385498" ["Campo 19"]=> string(64) "http://www.be-direct.co.uk/additional/product_images/id16390.jpg" ["Campo 20"]=> string(32) "Cooking (cookers and microwaves)" ["Campo 21"]=> string(0) "" ["Campo 22"]=> string(0) "" ["Campo 23"]=> string(0) "" ["Campo 24"]=> string(0) "" ["Campo 25"]=> string(0) "" ["Campo 26"]=> string(0) "" ["Campo 27"]=> string(0) "" ["Campo 28"]=> string(0) "" ["Campo 29"]=> string(0) "" } [3]=> array(29) { ["Campo 1"]=> string(21) "Buy Electrical Direct" ["Campo 2"]=> string(26) "http://www.be-direct.co.uk" ["Campo 3"]=> string(19) "01/06/2005 04:46:48" ["Campo 4"]=> string(48) "Hotpoint 60cm Gas Cooker with Polar White Finish" ["Campo 5"]=> string(5) "GW62P" ["Campo 6"]=> string(48) "Hotpoint 60cm Gas Cooker with Polar White Finish" ["Campo 7"]=> string(5) "16402" ["Campo 8"]=> string(8) "Hotpoint" ["Campo 9"]=> string(5) "GW62P" ["Campo 10"]=> string(0) "" ["Campo 11"]=> string(0) "" ["Campo 12"]=> string(3) "GBP" ["Campo 13"]=> string(4) "0.00" ["Campo 14"]=> string(6) "495.99" ["Campo 15"]=> string(4) "0.00" ["Campo 16"]=> string(0) "" ["Campo 17"]=> string(147) "http://www.dpbolvw.net/click-1679007-10385498?url=http%3A%2F%2Fwww.be-direct.co.uk%2FproductDetails.asp%3Freferer%3Dcommissionjunction%26id%3D16402" ["Campo 18"]=> string(43) "http://www.tqlkg.com/image-1679007-10385498" ["Campo 19"]=> string(64) "http://www.be-direct.co.uk/additional/product_images/id16402.jpg" ["Campo 20"]=> string(32) "Cooking (cookers and microwaves)" ["Campo 21"]=> string(0) "" ["Campo 22"]=> string(0) "" ["Campo 23"]=> string(0) "" ["Campo 24"]=> string(0) "" ["Campo 25"]=> string(0) "" ["Campo 26"]=> string(0) "" ["Campo 27"]=> string(0) "" ["Campo 28"]=> string(0) "" ["Campo 29"]=> string(0) "" } }

The first line of that array I put it in a form in a select to define where exactly that field has to go to mysql and I do it like this:

<tr>
            <td class="derecha_listados"><label title="Nombre del artículo">Concepto:</label></td>
            <td class="izquierda_listados"><select style="width: 200px;" class="buskiselect" name="ImportarCsv_Nombre" id="ImportarCsv_Nombre" placeholder="Seleccionar Articulo">
                <?php for ($i = 0; $i < $num_campos; $i++) { echo '<option value="'.$nombres_campos[$i].'">'.$nombres_campos[$i].'</option>'; } ?>
            </select>
            </td>
        </tr>

Repeating the last code as many times as tables have the database they have chosen.

And now comes the crap for me. I already have the array, I already have defined where each field of the csv goes ... How do I cross the data to store them in mysql? I do not know if I explain myself ... for example: I want to define that field 3 of csv is equivalent to article name in mysql and that it crosses all the csv lines counting with field 3 is the name ...

Thank you very much for your help.

    
asked by Killpe 16.02.2018 в 09:49
source

0 answers