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? Quote Link to comment 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. Quote Link to comment 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? Quote Link to comment Share on other sites More sharing options...
amitc Posted January 22, 2011 Author Share Posted January 22, 2011 BUMP Quote Link to comment 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; Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.