[thelist] Form data to office database

Jon Molesa rjmolesa at consoltec.net
Sat Oct 13 20:45:04 CDT 2007

This not elegant code by any means, but it did the trick in a pinch for
me a few months back on a job.  HIH.

$header = '';
$data = '';

while($row = mysql_fetch_array($result, MYSQL_ASSOC))
	$f=1;  // this was needed because for some reason it was skipping the
		   // first row of data

	foreach($row as $key => $val)
		$cnt = count($row);
			if($f==$cnt){  // i had to add this test as well.
				$header .= "\"".$key."\"\n";
				$data .= "\"".$val."\"\n";
				$header .= "\"".$key."\",";
				$data .= "\"".$val."\",";
			if($f==$cnt){  // i never quite figured out why.
				$data .= "\"".$val."\"\n";
				$data .= "\"".$val."\",";

// this would output our csv data.  To do it in a browser, you'll need
// to send the appropriate headers as well.  Then it'll open
// automatically in Excel.
// you may want to change the output method though
echo $header;
echo $data;

I just remembered why that was needed.  I needed a way to not insert the
commas on the last column of data.

Jon Molesa
rjmolesa at consoltec.net

More information about the thelist mailing list