1

I have the following PHP script which reads from two CSV files, At the moment I am putting the data into a table however I need it to output to a CSV file...

<?php

$products_file = fopen('Products.csv', 'r');
$manufacturers_file = fopen('Manufacturers.csv', 'r');

$manufacturers = array();

while (($manufacturers_line = fgetcsv($manufacturers_file)) !== FALSE) {

     $manufacturers[$manufacturers_line[0]] = $manufacturers_line [1];

    }

echo '<table><tr><th>SKU</th><th>Make and Model</th><th>Make and Model</th></tr>';

while (($products_line = fgetcsv($products_file)) !== FALSE ) {

  echo '<tr><td>'.$products_line[3].'</td><td>';

  echo $manufacturers[$products_line[5]];

  echo '</td><td>'.$products_line[4].'</td></tr>';
}

echo '</table>';

fclose($products_file);
fclose($manufacturers_file);

?>

How can I do this using fputcsv ?

1

3 Answers 3

3

Looks like you just need to change where it renders the html table to write it to a csv file like this as from the PHP documentation:-

$fp = fopen('newfile.csv', 'w');

while (($products_line = fgetcsv($products_file)) !== FALSE )
{
  fputcsv($fp, $products_line);
}

fclose($fp);

Hope that helps.

Rick.

Sign up to request clarification or add additional context in comments.

4 Comments

Mind though that this will output different values than what's in your html table.
This is true as it will just output the whole row but that depends what data he wants to print out. It can easily be changed like you did by creating a new array.
Ta very Richard, I did end upp using this in the end... nice one!
Ah your most welcome, shame I didn't get the answer though :)
3

You could also do it this way:

Declare $csv outside your while loop:

$csv = '';

Then in your while loop populate the variable:

$csv .= $products_line[3].','.$manufacturers[$products_line[5]].','.$products_line[4]."\n";

Then once outside your loop you can write $csv to a file:

$myFile = 'testFile.csv';
$fh = fopen($myFile, 'w') or die('cannot open file');
fwrite($fh, $csv);
fclose($fh);

Done!

Note the double quote around the \n. If you use single quotes the \n character will not have the expected result.

1 Comment

+1 for double quote around the \n - I wouldn't have thought of that!
2

I'd suggest this:

$outfile = fopen('output.csv', 'w');

while (($products_line = fgetcsv($products_file)) !== FALSE 
      && fputcsv(
        array($products_line[3], $manufacturers[$products_line[5]], $products_line[4]
      ) !== FALSE) {
  echo '<tr><td>'.$products_line[3].'</td><td>';
  echo $manufacturers[$products_line[5]];
  echo '</td><td>'.$products_line[4].'</td></tr>';
}

First, a new array is being created with
array($products_line[3], $manufacturers[$products_line[5]], $products_line[4].
Then this array is being fed to fputcsv().

The above code outputs both csv and html. Remove the echo if you don't want html output.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.