Hi all
I am looking at the fputcsv function for something that I need. In summary,
this is what I want to do:
1. Go through a table
2. Get all the columns and put them into an array
3. write the array to a CSV file
4. Go to the next row
5. Append the table row into the CSV file
6. Repeat for each row in the table
The php site shows this:
<?php
$list = array (
'aaa,bbb,ccc,dddd',
'123,456,789',
'"aaa","bbb"'
);
$fp = fopen('file.csv', 'w');
foreach ($list as $line) {
fputcsv($fp, split(',', $line));
}
fclose($fp);
?>
I'm assuming that I need the 'a+' mode for opening the file and not 'w'?
I'm ashamed to say that I have no idea how to loop through the table and put
the columns into the array.
What I'm coming up with at the moment is this:
<?php do {
$list = array (
'
$row_Recordset1['firstname'],
$row_Recordset1['lastname'],
$row_Recordset1['email'],
$row_Recordset1['telephone']'
'
);
$fp = fopen('/path/to/the/file.csv', 'a+');
foreach ($list as $line) {
fputcsv($fp, split(',', $line));
}
fclose($fp);
} while ($row_Recordset1 = mysql_fetch_assoc($Recordset1)); ?>
Probably very inelegant!
Does it look right though?
Andy