Search code examples
phpmysqlpdffpdf

While Loop in FPDF only prints 1 result


i'm having a problem with FPDF, i want to create a While Loop that returns every result that my SQL query does when i use Phpmyadmin, the problem here is that it only returns one. If i use
$pdf->Cell(190,10,''.$pdf_info2['format'].'',1,1,0); it does print the result i want, but i need to return them in the table as i show below. Ps: This is my firts question so i appolagise if i wasn't that clear o my problem. Thanks in advance

$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);

Solution

  • Use this code:

    First you must need to define before loop: $html2 = ''; and concatenate with $html2 .= in while loop see below code for updated code:

    $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);