Sample data that is already in the database table:
INSERT INTO `csvtbl` (`ID`, `SKU`, `Product_Name`, `Model`, `Make`, `Year_From`, `Year_To`) VALUES (1, 'C2AZ-3B584-AR', 'Power Steering Pump Seal Kit (Eaton Pump)', 'Galaxie', 'Ford', '1960', '1965'), (2, 'C2AZ-3B584-AR', 'Power Steering Pump Seal Kit (Eaton Pump)', 'Thunderbird ', 'Fordtrest', '1960', '1965');
I have the code below and insertable years with a comma (,) divided in the table:
INSERT INTO `diff_yearstbl` (`ID`, `SKU`, `Product_Name`, `Model`, `Make`, `Year`) VALUES (1, 'C2AZ-3B584-AR', 'Power Steering Pump Seal Kit (Eaton Pump)', 'Galaxie', 'Ford', '1960,1961,1962,1963,1964,1965'), (2, 'C2AZ-3B584-AR', 'Power Steering Pump Seal Kit (Eaton Pump)', 'Thunderbird ', 'Fordtrest', '1960,1961,1962,1963,1964,1965');
Data years work well, but I want to insert the Make and Model data as the same as Years, but this time just insert the data with a comma (,), divided into one line, as shown below, using the SKU field. Therefore, you need to combine the two above records into one line, as shown below.
INSERT INTO `diff_yearstbl` (`ID`, `SKU`, `Product_Name`, `Model`, `Make`, `Year`) VALUES (1, 'C2AZ-3B584-AR', 'Power Steering Pump Seal Kit (Eaton Pump)', 'Galaxie, Thunderbird', 'Ford, Fordtrest' '1960,1961,1962,1963,1964,1965');
Below is the code I made:
$query = "select Year_TO - Year_From as diff_years, ID, SKU,Product_Name,Model,Make,Year_From,Year_To from csvtbl"; $result = mysql_query($query, $connect ); //$result = $db->query($select_test)->fetchAll(); if (count($result) > 0) { while($QryRow = mysql_fetch_assoc($result)) { $diff_years = $QryRow['diff_years']; $Year_From = $QryRow['Year_From']; $SKU = $QryRow['SKU']; $Product_Name = $QryRow['Product_Name']; $Model = $QryRow['Model']; $Make = $QryRow['Make']; $years= array(); for ($x = $QryRow['Year_From']; $x <= $QryRow['Year_To']; $x++) { $years[] = $x; } $query_insert = "INSERT INTO diff_yearstbl(SKU,Product_Name,Model,Make,Year) VALUES('".$SKU."','".$Product_Name."','".$Model."','".$Make."','".implode('|',$years)."')"; $s_insert = mysql_query($query_insert, $connect ); } } else { echo "<p>Nothing matched your query.</p>"; } ?>
Please help about the same.