Jump to content

CSV Line Break with PHP


amitc

Recommended Posts

$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

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;

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.