I have a .csv file that I am reformatting, below is the code that reads the file and rearranges the columns. The problem is $line[6] and $line[7] have extra information that I don't need, all I need is the filename, which is for instance Thumb_80073.jpg should just be 80073.jpg. I don't know why the manufacturer does the file this way, but they do and it is annoying. The code is used to reformat the file to a format that I can use to import into my shopping cart.
$in = fopen("datafeed.csv","r");
$out = fopen("converted.csv" , "w");
fgets($in);
fgets($in);
$header_row = array('Name', 'Model', 'Product Price', 'Quantity', 'Categories', 'Description' , 'Product Image', 'Large Image', ' Weight', 'Vendors Price', 'Vendors Product Id', 'Wholesale', 'Special Products Price', 'Product Attributes', 'In Stock');
fputcsv($out, $header_row, ',');
while(($line = fgetcsv($in, 0, ',')) !== false)
{
if($line[4] > 0)
{
$stock = 1;
}
else
{
$stock = 0;
}
if($line[3] == 0 )
{
$output = str_replace('0', " " ,$line[3]);
$formatted = array($line[8], $line[1] ,ceil(($line[2]/0.4)) , $line[4] , str_replace(';', ';;', $line[6]) , $line[8] , $line[9] , $line[10] , $line[5] , $line[2] , $line[1] , ceil(($line[2]/0.7)) ,$output , $line[7], $stock);
fputcsv($out, $formatted, ','); // change '|' to ',' if you want comma delimiter
}
if($line[3] != 0 and $line[4] > 0 )
{
$formatted = array($line[8], $line[1] ,ceil(($line[2]/0.4)) , $line[4] , str_replace(';', ';;', $line[6]) , $line[8] , $line[9] , $line[10] , $line[5] , $line[2], $line[1] , ceil(($line[2]/0.7)), $line[3] , $line[7], $stock);
fputcsv($out, $formatted, ','); // change '|' to ',' if you want comma delimiter
}
}