1

我正在通过 PHP 将内容从 MySQL 数据库导出到 Word 模板 (RTF)。

有一个部分,“附录 B”,它应该显示为记录找到的所有首字母缩略词。本节最少出现 90 条记录(即,这些是每条记录的标准首字母缩略词 (tblAcronyms));但是最大记录未知,因为用户可以添加到此列表(tblAppendixB)。

Word(即RTF)文档应该显示找到的所有记录,但是,它只显示第一条记录。

这是我迄今为止所拥有的:

<?php
....

#Retrieve Appendix B records
$qry_get_AppB = "SELECT vAcronym, vAcronymDesc FROM tblAcronyms
                   UNION SELECT vAcronym, vAcronymDesc FROM tblAppendixB
                     WHERE fkID = ". $i_fk_id . " ORDER BY vAcronym ASC";
$qry_appb_result = mysql_query($qry_get_AppB);
$qryAppBno_rows = mysql_num_rows($qry_appb_result);    

//Generate the headers to help a browser choose the correct location
header('Content-Type: application/msword');
header('Content-Disposition: inline; filename="'.$vProgramName.'_rec.rtf");

//Open the template file
$tfilename = 'Appb_Template.rtf';
$fp = fopen($tfilename, 'r');

//Read the template into a variable
$toutput = fread($fp, filesize($tfilename));

fclose($fp);

//Replace the place holders in the template with data
if($qryAppBno_rows > 0)
{
  while($rowAppB = mysql_fetch_array($qry_appb_result)) 
  {
     $vAppB = $rowAppB['vAcronym'] . '-' . $rowAppB['vAcronymDesc'] . "\r\n";
     $toutput = str_replace('<<vAppB>>', $vAppB, $toutput);
  }
}

//Send the generated document to the browser
echo $toutput;

?>

我已经搜索了这个论坛和其他人,但还没有找到解决方案。

非常感谢任何帮助。

4

2 回答 2

0

好的,我不太确定您的模板是什么样的,但我想,您只有 1 个名为<<vAppB>>. 在您的第一次迭代中(如果有任何可用数据),您将那个占位符替换为您的第一个数据条目。在这一事实中,仅显示了一个占位符。

可能会将您的代码重写为与此类似的内容

... //do your stuff
$newLine = "\r\n";
$appendix = "";
while($rowAppB = mysql_fetch_array($qry_appb_result)) {
    $appendix .= $rowAppB['vAcronym'] . '-' . $rowAppB['vAcronymDesc'] . $newLine;
}
$toutput = str_replace('<<vAppB>>', $appendix, $toutput);
...//do some other stuff

只是简单的原型设计,因此您可能需要做一些额外的工作。

诀窍是collect你得到的所有条目,而不是用你的占位符替换它:)

于 2012-04-09T17:56:53.343 回答
0

我解决了问题;我必须使用 "\par" 而不是 "\r\n" 让 MS Word (RTF) 将其识别为段落标记。这是现在可以使用的修改后的代码:

<?php
....

$t_newline = "\par";

#Retrieve Appendix B records
$qry_get_AppB = "SELECT vAcronym, vAcronymDesc FROM tblAcronyms
               UNION SELECT vAcronym, vAcronymDesc FROM tblAppendixB
                 WHERE fkID = ". $i_fk_id . " ORDER BY vAcronym ASC";
$qry_appb_result = mysql_query($qry_get_AppB);
$qryAppBno_rows = mysql_num_rows($qry_appb_result);    

//Generate the headers to help a browser choose the correct location
header('Content-Type: application/msword');
header('Content-Disposition: inline; filename="'.$vProgramName.'_rec.rtf");

//Open the template file
$tfilename = 'Appb_Template.rtf';
$fp = fopen($tfilename, 'r');

//Read the template into a variable
$toutput = fread($fp, filesize($tfilename));

fclose($fp);

//Replace the place holders in the template with data
if($qryAppBno_rows > 0)
{
   while($rowAppB = mysql_fetch_array($qry_appb_result)) 
   {
       $vAppendixB[] = $rowAppB['vAcronym'] . '-' . $rowAppB['vAcronymDesc'] . $t_newline;
       $vAppB = implode(" ", $vAppendixB);
   }
}

$toutput = str_replace('<<vAppB>>', $vAppB, $toutput);

//Send the generated document to the browser
echo $toutput;

?>

希望这会对其他人有所帮助。:-)

于 2012-04-17T11:56:32.467 回答