[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.

$i=0;
$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($i==0){
			if($f==$cnt){  // i had to add this test as well.
				$header .= "\"".$key."\"\n";
				$data .= "\"".$val."\"\n";
			}else{
				$header .= "\"".$key."\",";
				$data .= "\"".$val."\",";
			}
		}else{
			if($f==$cnt){  // i never quite figured out why.
				$data .= "\"".$val."\"\n";
			}else{
				$data .= "\"".$val."\",";
			}
		}
		$f++;
	}
	$i++;
}

// 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
http://rjmolesa.com



More information about the thelist mailing list