One of the scripts i've posted before reads 2 .csv files and merges the parts we require. That all works great but Magento which we use for our ecommerce isn't great at handling large files when importing so I have thought about splitting the import over say 4 .csv files. Is there any way that I could make the following script output to a new file with identical headers after say 5000 records? So, as there are just under 20k products, I would end up with output1.csv, output2.csv, output3.csv, output4.csv each with 5000 records apart from the last one which would have the remainder.
<?php
/* Get categories */
$proxy = new SoapClient('http://www.myofficestationery.com/api/soap/?wsdl=1');
$sessionId = $proxy->login('import', 'import');
$allCategories = $proxy->call($sessionId, 'category.tree'); // Get all categories.
$conn = mysql_connect('localhost', '*********_*********', '*********') or die(mysql_error());
mysql_select_db('*********_*********');
mysql_query('truncate table import_data', $conn);
$handle = fopen('spicerf2.csv', "r");
$i=0;
while($data = fgetcsv($handle, 1000, ","))
{
if($i!=0) { Allow first row to be processed as no header row in file
$sql = sprintf('insert into import_data set matrix_band_pricing_flag = "%s", product_code = "%s", alt_prod_code = "%s", retail = "%s", trade = "%s", cat_spec = "%s", non_ret = "%s"',
$data[31],
$data[0],
$data[6],
$data[9],
$data[10],
$data[34],
$data[40]
);
echo $sql."\n";
mysql_query($sql, $conn) or die($sql);
}
++$i;
}
fclose($handle);
// Load file
$handle = fopen("ProductData.csv", "r");
$output = "thumbnail,small_image,image,store,websites,attribute_set,type,sku,category_ids,has_options,status,visibility,weight,name,options_container,url_key,url_path,gift_message_available,price,rrp,description,short_description,tax_class_id\n";
$i=0;
// Is there data?
while($data = fgetcsv($handle, 1000, ","))
{
if($i!=0) {
$category_id = NULL;
$boss_code = $data[26];
$code_row = mysql_fetch_assoc(mysql_query('select * from boss_codes where 5digits = '.$boss_code, $conn));
$category_1 = str_replace(" ", " ", $code_row['ISPC1DESC']);
$category_2 = str_replace(" ", " ", $code_row['ISPC2DESC']);
$category_3 = str_replace(" ", " ", $code_row['ISPC3DESC']);
foreach($allCategories['children'][0]['children'] as $key => $value) {
if($value['name'] == $category_1) {
$cat_1 = $value['category_id'];
foreach($value['children'] as $key1 => $value1) {
if($value1['name'] == $category_2) {
$cat_2 = $value1['category_id'];
foreach($value1['children'] as $key2 => $value2) {
if($value2['name'] == $category_3) {
$cat_3=$value2['category_id'];
$category_id = $cat_1.','.$cat_2.','.$cat_3;
}
}
}
}
}
}
$qry = mysql_query('select * from import_data where product_code = "'.$data[0].'"', $conn) or die('select * from import_data where product_code = "'.$data[0].'"');
$row = mysql_fetch_assoc($qry);
$trade = $row['trade']/100;
$rrp = $row['retail']/100; // sale price
$price = $row['trade']/100;
$vat_code = $data[22];
if($vat_code != 1) {
$tax = 'None';
} else {
$tax = 'Taxable Goods';
}
$matrix_band_pricing_flag = $row['matrix_band_pricing_flag'];
switch($matrix_band_pricing_flag) {
case 'A' : $percent = 0.70;
break;
case 'B' : $percent = 0.70;
break;
case 'C' : $percent = 0.75;
break;
case 'D' : $percent = 0.85;
break;
case 'E' : $percent = 0.88;
break;
case 'F' : $percent = 0.60;
break;
case 'G' : $percent = 0.75;
break;
case 'H' : $percent = 0.75;
break;
case 'N' : $percent = 0.70;
break;
}
$price = $price / $percent;
$price = number_format($price, 2, '.', '');
echo $data[0].' - '.$boss_code.' - '.$category_id.' - '.$price. ' - '.$row['trade'].' - '.$matrix_band_pricing_flag;
echo "\n";
if($matrix_band_pricing_flag != 'N') {
$output .= '/'.$data[31].',/'.$data[31].',/'.$data[31].',admin,base,Default,simple,"'.$data[0].'","'.$category_id.'",0,Enabled,"Catalog, Search",0,"'.$data[1].' - ['.$data[0].']",Block after Info Column,"'.$data[1].'","'.$data[1].'",,'.$price.','.$rrp.',"'.str_replace("\n", '',str_replace('\\', '', str_replace('"', '', str_replace('\r\n', '<br>', nl2br($data[29]))))).'","'.$data[1].'","'.$tax.'"';
$output .= "\n";
}
unset($rrp);
unset($price);
unset($row);
unset($qry);
unset($category_id);
unset($key);
unset($key1);
unset($key2);
unset($value);
unset($value1);
unset($value2);
}
++$i;
}
file_put_contents('./output.csv', $output);
mysql_close($conn);
echo "Done\n";