DotDragnet
February 04, 2012, 10:50:16 PM *
Welcome, Guest. Please login or register.

Login with username, password and session length
News: follow us on twitter @dotdragnet
 
   Home   Help Search Login Register  
Pages: [1]   Go Down
  Print  
Author Topic: Split .csv output to multiple files  (Read 511 times)
Whatever
Hero Member
*****
Posts: 714



View Profile WWW Awards
« on: July 08, 2010, 11:39:53 AM »

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.

Code:
<?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($handle1000","))
{
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($handle1000","))
{
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($price2'.''');

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";
Logged

Office Stationery : Paper : Inks & Toners
10% off first 3 orders for registered users. Use the code DDN10
JasonD
Global Moderator
Hero Member
*****
Posts: 529



View Profile Awards
« Reply #1 on: July 08, 2010, 12:32:03 PM »

Untested, but should work.

Change
Quote
$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";
to

Code:
$header = "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";

$output = $header;
$part = 1;

After
Quote
$i=0;
// Is there data?
while($data = fgetcsv($handle, 1000, ","))
{
   if($i!=0) {
add
Code:
        if (($i % 5000) == 0) {
            file_put_contents('./output' . $part . '.csv', $output);
            $output = $header;
            ++$part;
        }

Change
Quote
file_put_contents('./output.csv', $output);
to
Code:
file_put_contents('./output' . $part . '.csv', $output);

Although it sounds like you have some horribly inefficient code if 20,000 lines is considered large.
Logged
Whatever
Hero Member
*****
Posts: 714



View Profile WWW Awards
« Reply #2 on: July 08, 2010, 01:48:41 PM »

Cheers jason.

It's just the way Magento imports. It's not great with larger amounts of products and the import process is horrible. We're looking at some point to see if we can bypass it and directly import to the db instead, not sure if it's possible though yet.
Logged

Office Stationery : Paper : Inks & Toners
10% off first 3 orders for registered users. Use the code DDN10
Pages: [1]   Go Up
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF | SMF © 2006-2008, Simple Machines Valid XHTML 1.0! Valid CSS!