Jump to content

Recommended Posts

I'm trying to export information into a csv file yet each row seems to keep writing over itself until the final product is the last line.  If I use append (a) instead of write (w) for fopen I can get all my rows to export okay except that I don't want to have to use that.  I have tried escapes \r\n, \n etc. and they dont move to the next row but rather work in the individual cell.

 

Thanks for any help,

Alex

 

$list = array("$1,$2,$3,$4,$5");
$fp = fopen('output.csv', 'w');
foreach ($list as $line) {
    fputcsv($fp, split(',', $line));
}
fclose($fp);

Link to comment
https://forums.phpfreaks.com/topic/86409-export-to-csv/
Share on other sites

if i use append i will have to clear my data each time beforehand.  im new to php and assumed write was the correct use for replacing a file as im not really appending anything.  if thats the only way to do it i can live with it but would rather learn the right way.

Link to comment
https://forums.phpfreaks.com/topic/86409-export-to-csv/#findComment-441554
Share on other sites

also forgot to put this

 

you dont need fputcsv

 

a normal fwrite will do the same job for ya

 

Below is something i used in a past project. Hope it helps

 

$_file = '../CSV/Admin_List.csv';

$_fp = @fopen( $_file, 'w' );

 

$SQL2 = "SELECT * FROM adminUsers";

$query2 = mysql_query($SQL2);

while ($res2 = mysql_fetch_assoc($query2))

{

$_csv_data= $res2['title'].','.$res2['name'].','.$res2['surname'].','.$res2['email'].','.$res2['postcode'].','.$comments.','.$res2['lastLogin'] .','.$stat. "\n";

@fwrite( $_fp, $_csv_data );

}

@fclose( $_fp );

Link to comment
https://forums.phpfreaks.com/topic/86409-export-to-csv/#findComment-441555
Share on other sites

also forgot to put this

 

you dont need fputcsv

 

a normal fwrite will do the same job for ya

 

The reason for using fputcsv is because it escapes the fields...if, in your example, one of the fields, $res2['title'] perhaps, contained a comma, it would break your csv.  Using fputcsv, it puts quotes around the field so that the comma contained in the field is ignored by most viewers (e.g. Excel)

Link to comment
https://forums.phpfreaks.com/topic/86409-export-to-csv/#findComment-441575
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.