【问题标题】:Output Table on While Loop using Dompdf使用 Dompdf 在 While 循环上输出表
【发布时间】:2017-03-16 12:37:57
【问题描述】:

如何在 while 循环中打印表格以使用 Dompdf 从数据库中获取数据?

以下是我已经完成但结果失败的代码。

$html =  '<table>
<tr>
  <td>Date</td><td>Name</td>
</tr>';

// Query from mysql 
if (mysqli_num_rows($result) > 0) {
  while ($row = mysqli_fetch_assoc($result)) {
   $date = $row['date '];
   $name = $row['name'];

   $html . = '<tr>
    <td> ' . $date . ' </td>' . $name . '</td>
   </tr>';
  }
}

$html .= '</table>';

require('../dompdf/autoload.inc.php');
use Dompdf\Dompdf;
$dompdf = new Dompdf();
$dompdf->load_html($html);

$dompdf->render();
$dompdf->stream("Result.pdf",array("Attachment"=>0));
$dompdf->clear();

【问题讨论】:

  • 现在你去编辑了,没有将其标记为编辑,说明你犯了一个错误。
  • 对不起@Fred-ii- 但如何将其标记为编辑?我确实在更新中提到我正在对代码进行更正
  • “堆栈提示”:每当您犯错时,您需要在(已编辑的)问题中指定您将代码更改为正确的语法。这主要是因为,当人们根据您(最初)发布的内容发布答案时,其他人会使用正确的语法查看答案并将其与您现在拥有的内容进行比较,并且可能会告诉自己:“他们有正确的语法,那么为什么要回答?” 他们可能会因此而对答案投反对票。因此,请始终将“新”代码放在原始问题下方,并带有 "Edit: 和快速解释;-)

标签: php html mysql dompdf


【解决方案1】:

1 字符串必须加引号。改变

$html .= </table>

$html .= '</table>';

2 运算符“.=”中不能有空格。改变

$html . = '<tr>

$html .= '<tr>

3最后,“日期”中可能没有空格,所以更改

$date = $row['date '];

$date = $row['date'];

【讨论】:

  • 感谢@Webjuju 的帮助。更正后,我有这个错误Fatal error: Maximum execution time of 30 seconds exceeded
  • 就在您的 require() 调用之前,您能否 die($html); 确保 html 部分不是罪魁祸首。你有多少行?
  • 如果我添加die($html);,效果很好。我有大约 400 行。
  • 好吧,在每一行之后放一个 die(_LINE_) 直到你找到问题。我也可以推荐stackoverflow.com/questions/18050071/…
  • 感谢您的帮助。我发现解决方案是我需要扩展max_execution_time。只是想知道如果我延长时间是否有任何问题。也许喜欢性能或者这不是好的解决方案?
猜你喜欢
  • 2021-11-02
  • 2013-09-28
  • 2012-01-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-10-09
  • 2015-09-04
相关资源
最近更新 更多