How to write the data into a csv file?
<p>Eg:the <code>csv</code> have two columns. The structure is : it only have the header.</p>

<pre><code>sku name
</code></pre>

<p>Now, there is a foreach loop:</p>

<pre><code>foreach ($collection as $product_all) {


$sku = $product_all['sku'];

$product_id = Mage::getModel('catalog/product')-&gt;getIdBySku($sku);

$product    = Mage::getModel('catalog/product');

$product -&gt;load($product_id);


$name = $product['name'];



}
</code></pre>

<p>I want to put the <code>$sku</code> and the <code>$name</code> into the csv file, if there is 8000 rows lines. How do I do? I using the following code. But it doesn't work. How to correct it? Thank you.</p>

<pre><code>  foreach ($collection as $product_all) {


    $sku = $product_all['sku'];

    $product_id = Mage::getModel('catalog/product')-&gt;getIdBySku($sku);

    $product    = Mage::getModel('catalog/product');

    $product -&gt;load($product_id);   

    $name = $product['name'];

    $newCsvData = array();
if (($handle = fopen("test.csv", "r")) !== FALSE) {
    while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
        $data[0] = $sku;
         $data[2] = $name;
        $newCsvData[] = $data;
    }
    fclose($handle);
}

$handle = fopen('test.csv', 'w');

foreach ($newCsvData as $line) {
   fputcsv($handle, $line);
}

fclose($handle);


    }
</code></pre>
