I've very new to PHP and trying to re-create some code I had in another language. The current version of PHP I'm working with is 7.17.
Right now, I'm trying to re-create a simple function of opening a MySQL query as a comma deliminated text file where there is no comma after the last column (just a line break). I'm trying to figure out two things right now to finish this piece of code.
How do I insert the query column headers at the top of the data output (right now only the data is output, not the column headers).
At the line "fwrite($output, $value.',');", I'm trying to figure out how to not add the comma after the last field (how to skip adding the comma after the last column of data).
PHP:
if (isset($_POST['download'])) {
header('Content-Type: text/plain');
header('Content-Disposition: attachment;filename=cars.txt');
header('Cache-Control: no-cache, no-store, must-revalidate');
header('Pragma: no-cache');
header('Expires: 0');
$output = fopen('php://output', 'w');
while ($row = getRow($result)) {
foreach ($row as $key => $value) {
fwrite($output, $value.',');
}
fwrite($output, "\r\n");
}
fclose($output);
exit;
}
Thank You!