Snippet Name: Output as Word Doc format
Description: Pull data from a DB and output it in MS Word (.DOC) format.
Comment: (none)
Language: PHP
Highlight Mode: PHP
Last Modified: February 27th, 2009
|
<?PHP
$query = "SELECT * FROM TABLE WHERE data = '$data'";
$result = mysql_query($query);
$count = mysql_num_fields($result);
FOR ($i = 0; $i < $count; $i++){
IF (ISSET($header))
$header .= mysql_field_name($result, $i)."\t";
ELSE
$header = mysql_field_name($result, $i)."\t";
}
WHILE ($row = mysql_fetch_row($result)){
$line = '';
FOREACH ($row AS $value)
{
IF (!ISSET($value) || $value == '')
$value = "\t";
ELSE
{
$value = STR_REPLACE('"', '""', $value);
$value = '"'.$value.'"'."\t";
}
$line .= $value;
}
IF (ISSET($data))
$data .= TRIM($line)."\n";
ELSE
$data = TRIM($line)."\n";
}
$data = STR_REPLACE("\r", "", $data);
IF ($data == '')
$data = "\nno matching records\n";
HEADER("Content-Type: application/vnd.ms-word; name='word'");
HEADER("Content-type: application/octet-stream");
HEADER("Content-Disposition: attachment; filename=filename_here.doc");
HEADER("Cache-Control: must-revalidate, post-check=0, pre-check=0");
HEADER("Pragma: no-cache");
HEADER("Expires: 0");
ECHO $header."\n".$data;
EXIT;
?> |