amitc Posted January 21, 2011 Share Posted January 21, 2011 $result = mysql_query("SHOW COLUMNS FROM ".$table.""); $i = 0; if (mysql_num_rows($result) > 0) { while ($row = mysql_fetch_assoc($result)) { $csv_output .= $row['Field'].","; $i++; } } $csv_output .= "\n"; $values = mysql_query("SELECT * FROM ".$table.""); while ($rowr = mysql_fetch_row($values)) { for ($j=0;$j<$i;$j++) { $csv_output .= $rowr[$j].", "; } $csv_output .= "\n"; } print $csv_output; I'm using the above to get a csv output in my browser, but it appears as a string of text eg a1,a2,a3,a4,b1,b2,b3,b4,... I'd like it to appear as a1,a2,a3,a4 b1,b2,b3,b4 c1,c2,c3,c4 Any ideas how? Link to comment https://forums.phpfreaks.com/topic/225191-csv-line-break-with-php/ Share on other sites More sharing options...
Pikachu2000 Posted January 21, 2011 Share Posted January 21, 2011 It looks like you're ading \n newlines to the output, so you should be able to use echo nl2br($csv_output); to display it correctly. Link to comment https://forums.phpfreaks.com/topic/225191-csv-line-break-with-php/#findComment-1163040 Share on other sites More sharing options...
amitc Posted January 21, 2011 Author Share Posted January 21, 2011 It looks like you're ading \n newlines to the output, so you should be able to use echo nl2br($csv_output); to display it correctly. Thank you... Is it possible to not have a comma on the last entry of each row? Link to comment https://forums.phpfreaks.com/topic/225191-csv-line-break-with-php/#findComment-1163088 Share on other sites More sharing options...
amitc Posted January 22, 2011 Author Share Posted January 22, 2011 BUMP Link to comment https://forums.phpfreaks.com/topic/225191-csv-line-break-with-php/#findComment-1163750 Share on other sites More sharing options...
litebearer Posted January 23, 2011 Share Posted January 23, 2011 Try... $result = mysql_query("SHOW COLUMNS FROM ".$table.""); $i = 0; if (mysql_num_rows($result) > 0) { while ($row = mysql_fetch_assoc($result)) { $csv_output .= $row['Field'].","; $i++; } } $csv_output = substr_replace($csv_output,"",-1); $csv_output .= "\n"; $values = mysql_query("SELECT * FROM ".$table.""); while ($rowr = mysql_fetch_row($values)) { for ($j=0;$j<$i;$j++) { $csv_output .= $rowr[$j].", "; } $csv_output = substr_replace($csv_output,"",-1); $csv_output .= "\n"; } print $csv_output; Link to comment https://forums.phpfreaks.com/topic/225191-csv-line-break-with-php/#findComment-1163787 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.