【问题标题】:While Loop in FPDF only prints 1 resultFPDF 中的循环仅打印 1 个结果
【发布时间】:2017-08-09 15:51:34
【问题描述】:

我在使用 FPDF 时遇到问题,我想创建一个 While 循环,它返回我使用 Phpmyadmin 时我的 SQL 查询执行的每个结果,这里的问题是它只返回一个。如果我使用
$pdf->Cell(190,10,''.$pdf_info2['format'].'',1,1,0); 它确实打印了我想要的结果,但我需要在表格中返回它们,如下所示。 Ps:这是我的第一个问题,所以如果我不是很清楚我的问题,我很抱歉。 提前致谢

$html='<table border="0">
            <tr>
                <td width="150" height="40" bgcolor="#e6e6e6">Tipo</td>
                <td width="150" height="40" bgcolor="#e6e6e6">Formato</td>
                <td width="150" height="40" bgcolor="#e6e6e6">&nbsp;</td>
                <td width="150" height="40" bgcolor="#e6e6e6">Pago</td>
                <td width="150" height="40" bgcolor="#e6e6e6">Editar</td>
            </tr>';
            while($pdf_info2 = $smth->fetch(PDO::FETCH_ASSOC)) {
                $html2 = '<tr>
<td width="150" height="40"   bgcolor="#e6e6e6">'.$pdf_info['format'].'</td>
                <td width="150" height="40" bgcolor="#e6e6e6">.$pdf_info['format'].</td>
                <td width="150" height="40" bgcolor="#e6e6e6">.$pdf_info['format'].</td>
                <td width="150" height="40" bgcolor="#e6e6e6">.$pdf_info['format'].</td>
                <td width="150" height="40" bgcolor="#e6e6e6">.$pdf_info['format'].</td>
            </tr>';
            }

$pdf->WriteHTML($html);
$pdf->WriteHTML($html2);

【问题讨论】:

  • $html2 .= 看到=前面的那个点了吗?
  • $pdf_info['format'] 应该是$pdf_info2['format']

标签: php mysql pdf fpdf


【解决方案1】:

使用此代码:

首先,您必须在循环之前定义:$html2 = ''; 并在 while 循环中与 $html2 .= 连接,请参阅下面的代码以获取更新的代码:

$html2 ='';
$html ='<table border="0">
<tr>
    <td width="150" height="40" bgcolor="#e6e6e6">Tipo</td>
    <td width="150" height="40" bgcolor="#e6e6e6">Formato</td>
    <td width="150" height="40" bgcolor="#e6e6e6">&nbsp;</td>
    <td width="150" height="40" bgcolor="#e6e6e6">Pago</td>
    <td width="150" height="40" bgcolor="#e6e6e6">Editar</td>
</tr>';

while($pdf_info2 = $smth->fetch(PDO::FETCH_ASSOC)) {
    $html2 .='<tr>
        <td width="150" height="40"   bgcolor="#e6e6e6">'.$pdf_info2['format'].'</td>
        <td width="150" height="40" bgcolor="#e6e6e6">'.$pdf_info2['format'].'</td>
        <td width="150" height="40" bgcolor="#e6e6e6">'.$pdf_info2['format'].'</td>
        <td width="150" height="40" bgcolor="#e6e6e6">'.$pdf_info2['format'].'</td>
        <td width="150" height="40" bgcolor="#e6e6e6">'.$pdf_info2['format'].'</td>
    </tr>';
}

$pdf->WriteHTML($html);
$pdf->WriteHTML($html2);

【讨论】:

  • 谢谢,它成功了,我会在 10 分钟内将其标记为正确,再次感谢。
【解决方案2】:

**Option1.**首先您需要将数据与以前的数据合并,所以使用$html .= $html;

选项2更新$pdf_info

$pdf_info2

并且不需要使用2个$pdf-&gt;WriteHTML();

所以总代码将是

$html='<table border="0">
            <tr>
                <td width="150" height="40" bgcolor="#e6e6e6">Tipo</td>
                <td width="150" height="40" bgcolor="#e6e6e6">Formato</td>
                <td width="150" height="40" bgcolor="#e6e6e6">&nbsp;</td>
                <td width="150" height="40" bgcolor="#e6e6e6">Pago</td>
                <td width="150" height="40" bgcolor="#e6e6e6">Editar</td>
            </tr>';
            while($pdf_info2 = $smth->fetch(PDO::FETCH_ASSOC)) {
                $html .= '<tr>
                <td width="150" height="40"   bgcolor="#e6e6e6">'.$pdf_info2['format'].'</td>
                <td width="150" height="40" bgcolor="#e6e6e6">'.$pdf_info2['format'].'</td>
                <td width="150" height="40" bgcolor="#e6e6e6">'.$pdf_info2['format'].'</td>
                <td width="150" height="40" bgcolor="#e6e6e6">'.$pdf_info2['format'].'</td>
                <td width="150" height="40" bgcolor="#e6e6e6">'.$pdf_info2['format'].'</td>
            </tr>';
            }

$pdf->WriteHTML($html);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-05-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-21
    • 2018-07-27
    • 2022-10-06
    • 2020-09-04
    相关资源
    最近更新 更多