TCPDF - 从 mysql 打印表

发布于 2024-10-21 03:36:29 字数 807 浏览 0 评论 0 原文

在我的 php 中,

while($info3 = mysql_fetch_array($result3)){
$Name = $info3["Name"];
$Address = $info3["Address"];
$Age = $info3["Age"];
// -----------------------------------------------------------------------------
$tbl = '
<table style="width: 638px;" cellspacing="0">
    <tr>
        <td style="border: 1px solid #000000; width: 150px;">'.$Name.'</td>
        <td style="border: 1px solid #000000; width: 378px;">'.$Age.'</td>
        <td style="border: 1px solid #000000; width: 110px; text-align:center">'.$Address.'</td>
    </tr>
</table>
';

$pdf->writeHTML($tbl, true, false, false, false, '');
}

始终在我的 pdf 中打印整个表格。但我想打印单个实例的

,然后想要循环打印之间的行。我该如何解决这个问题?

in my php,

while($info3 = mysql_fetch_array($result3)){
$Name = $info3["Name"];
$Address = $info3["Address"];
$Age = $info3["Age"];
// -----------------------------------------------------------------------------
$tbl = '
<table style="width: 638px;" cellspacing="0">
    <tr>
        <td style="border: 1px solid #000000; width: 150px;">'.$Name.'</td>
        <td style="border: 1px solid #000000; width: 378px;">'.$Age.'</td>
        <td style="border: 1px solid #000000; width: 110px; text-align:center">'.$Address.'</td>
    </tr>
</table>
';

$pdf->writeHTML($tbl, true, false, false, false, '');
}

Prints the whole table always in my pdf. But I want to print <table> and </table> for a single instance and then want to loop printing the rows in between. How can I solve this??

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

墨落成白 2024-10-28 03:36:29
$tbl_header = '<table style="width: 638px;" cellspacing="0">';
$tbl_footer = '</table>';
$tbl = '';

// foreach item in your array...
$tbl .= '
    <tr>
        <td style="border: 1px solid #000000; width: 150px;">'.$Name.'</td>
        <td style="border: 1px solid #000000; width: 378px;">'.$Age.'</td>
        <td style="border: 1px solid #000000; width: 110px; text-align:center">'.$Address.'</td>
    </tr>
';

$pdf->writeHTML($tbl_header . $tbl . $tbl_footer, true, false, false, false, '');
$tbl_header = '<table style="width: 638px;" cellspacing="0">';
$tbl_footer = '</table>';
$tbl = '';

// foreach item in your array...
$tbl .= '
    <tr>
        <td style="border: 1px solid #000000; width: 150px;">'.$Name.'</td>
        <td style="border: 1px solid #000000; width: 378px;">'.$Age.'</td>
        <td style="border: 1px solid #000000; width: 110px; text-align:center">'.$Address.'</td>
    </tr>
';

$pdf->writeHTML($tbl_header . $tbl . $tbl_footer, true, false, false, false, '');
空气里的味道 2024-10-28 03:36:29

如果您的意思是您想要一个包含所有结果作为行的表,则仅循环行打印并将开始和结束标记迁移到循环之外。

$tbl = '<table style="width: 638px;" cellspacing="0">';

    while($info3 = mysql_fetch_array($result3)){
    $Name = $info3["Name"];
    $Address = $info3["Address"];
    $Age = $info3["Age"];

    $tbl .= '<tr>
            <td style="border: 1px solid #000000; width: 150px;">'.$Name.'</td>
            <td style="border: 1px solid #000000; width: 378px;">'.$Age.'</td>
            <td style="border: 1px solid #000000; width: 110px; text-align:center">'.$Address.'</td>
            </tr>'
     }
$tbl .= '</table>';


$pdf->writeHTML($tbl, true, false, false, false, '');

If what you mean is you want one table that includes all the results as rows, only loop the row printing and migrate the opening and closing tags outside the loop.

$tbl = '<table style="width: 638px;" cellspacing="0">';

    while($info3 = mysql_fetch_array($result3)){
    $Name = $info3["Name"];
    $Address = $info3["Address"];
    $Age = $info3["Age"];

    $tbl .= '<tr>
            <td style="border: 1px solid #000000; width: 150px;">'.$Name.'</td>
            <td style="border: 1px solid #000000; width: 378px;">'.$Age.'</td>
            <td style="border: 1px solid #000000; width: 110px; text-align:center">'.$Address.'</td>
            </tr>'
     }
$tbl .= '</table>';


$pdf->writeHTML($tbl, true, false, false, false, '');
↘人皮目录ツ 2024-10-28 03:36:29

我当然会这样做:

$table = '<table style="width: 638px;" cellspacing="0">%s</table>'
$tr    = '
    <tr>
        <td style="border: 1px solid #000000; width: 150px;"> %s</td>
        <td style="border: 1px solid #000000; width: 378px;"> %s</td>
        <td style="border: 1px solid #000000; width: 110px; text-align:center">%s</td>
    </tr>
';

while($info3 =  = mysql_fetch_array($result3)){
    $trs[] = sprintf($tr, $info3["Name"], $info3["Age"], $info3["Address"]);
}

$tbl = sprintf($table, implode( $trs ));
$pdf->writeHTML($tbl, true, false, false, false, '');

如果您无法使用模板组织表示层,至少使其与实际逻辑尽可能分离。

您可以在此处阅读有关 sprintf在这里崩溃

I would certainly do something like this:

$table = '<table style="width: 638px;" cellspacing="0">%s</table>'
$tr    = '
    <tr>
        <td style="border: 1px solid #000000; width: 150px;"> %s</td>
        <td style="border: 1px solid #000000; width: 378px;"> %s</td>
        <td style="border: 1px solid #000000; width: 110px; text-align:center">%s</td>
    </tr>
';

while($info3 =  = mysql_fetch_array($result3)){
    $trs[] = sprintf($tr, $info3["Name"], $info3["Age"], $info3["Address"]);
}

$tbl = sprintf($table, implode( $trs ));
$pdf->writeHTML($tbl, true, false, false, false, '');

If you can't organize your presentation layer with templates at least make it as separated as you can from the actual logic.

You can read about sprintf here and implode here

巷雨优美回忆 2024-10-28 03:36:29
$tbl = '<table style="width: 638px;" cellspacing="0">';
while($info3 = mysql_fetch_array($result3)){
  $Name = $info3["Name"];
  $Address = $info3["Address"];
  $Age = $info3["Age"];
  // -----------------------------------------------------------------------------
  $tbl = $tbl . '<tr>
          <td style="border: 1px solid #000000; width: 150px;">'.$Name.'</td>
          <td style="border: 1px solid #000000; width: 378px;">'.$Age.'</td>
          <td style="border: 1px solid #000000; width: 110px; text-align:center">'.$Address.'</td>
      </tr>';
  }  
  $tbl = $tbl . '</table>';
  $pdf->writeHTML($tbl, true, false, false, false, '');
$tbl = '<table style="width: 638px;" cellspacing="0">';
while($info3 = mysql_fetch_array($result3)){
  $Name = $info3["Name"];
  $Address = $info3["Address"];
  $Age = $info3["Age"];
  // -----------------------------------------------------------------------------
  $tbl = $tbl . '<tr>
          <td style="border: 1px solid #000000; width: 150px;">'.$Name.'</td>
          <td style="border: 1px solid #000000; width: 378px;">'.$Age.'</td>
          <td style="border: 1px solid #000000; width: 110px; text-align:center">'.$Address.'</td>
      </tr>';
  }  
  $tbl = $tbl . '</table>';
  $pdf->writeHTML($tbl, true, false, false, false, '');
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文