TCPDF print results of a MySQL PHP query

0

Hello, I have the following problem, I am trying to select all the records of a table and then pass them to a pdf file, the problem is that it only prints a record in the pdf, when in fact they must be 3 which are the ones that I have in the bd, I leave the code to see me in better detail.

$sql=mysqli_query($conn, "SELECT * FROM USUARIOS");

foreach($sql as $key => $value) {

$html='

<h1 align="center">Usuarios</h1><br>
<table style="font-size:13px; padding:5px 10px; border: 1px solid #666;" align="center">

    <tr>
        <td style="border-right: 1px solid #666;">Nombres</td>
        <td style="border-right: 1px solid #666;">Apellidos</td>
        <td style="border-right: 1px solid #666;">Correo</td>
        <td style="border-right: 1px solid #666;">Usuario</td>
        <td style="border-right: 1px solid #666;">Privilegio</td>

    </tr>




        <tr>
            <td style="border-right: 1px solid #666; border: 1px solid #666;">'.$value["nombres"].'</td>
            <td style="border-right: 1px solid #666; border: 1px solid #666;">'.$value["apellidos"].'</td>
            <td style="border-right: 1px solid #666; border: 1px solid #666;">'.$value["correo"].'</td>
            <td style="border-right: 1px solid #666; border: 1px solid #666;">'.$value["usuario"].'</td>
            <td style="border-right: 1px solid #666; border: 1px solid #666;">'.$value["privilegio"].'</td>

    </tr>
    </table>';
     } //Cierra foreach
     $pdf->writeHTML($html, false, false, false, false, ''); //Salida PDF
     $pdf->Output('reporte.pdf', 'I');

Result on screen:

    
asked by Jonathan 30.04.2018 в 22:42
source

2 answers

0

I think your problem is in what you have inside your loop. You just have to put what you want to be repeated. It would stay like this:

$sql=mysqli_query($conn, "SELECT * FROM USUARIOS");  

$html='

<h1 align="center">Usuarios</h1><br>
<table style="font-size:13px; padding:5px 10px; border: 1px solid #666;" align="center">

    <tr>
        <td style="border-right: 1px solid #666;">Nombres</td>
        <td style="border-right: 1px solid #666;">Apellidos</td>
        <td style="border-right: 1px solid #666;">Correo</td>
        <td style="border-right: 1px solid #666;">Usuario</td>
        <td style="border-right: 1px solid #666;">Privilegio</td>

    </tr>';    

    foreach($sql as $key => $value) {

    $html .= '<tr>
            <td style="border-right: 1px solid #666; border: 1px solid #666;">'.$value["nombres"].'</td>
            <td style="border-right: 1px solid #666; border: 1px solid #666;">'.$value["apellidos"].'</td>
            <td style="border-right: 1px solid #666; border: 1px solid #666;">'.$value["correo"].'</td>
            <td style="border-right: 1px solid #666; border: 1px solid #666;">'.$value["usuario"].'</td>
            <td style="border-right: 1px solid #666; border: 1px solid #666;">'.$value["privilegio"].'</td>

    </tr>';
    } //Cierra foreach
    $html .= '</table>';

     $pdf->writeHTML($html, false, false, false, false, ''); //Salida PDF
     $pdf->Output('reporte.pdf', 'I');
    
answered by 30.04.2018 в 22:46
0

Put just inside the for the tr with the new values because the way you are doing this is overwriting the whole table and at the end you show a single table with a single value would be something like:

html = '
<h1 align="center">Usuarios</h1><br>
<table style="font-size:13px; padding:5px 10px; border: 1px solid #666;" align="center">    
    <tr>
        <td style="border-right: 1px solid #666;">Nombres</td>
        <td style="border-right: 1px solid #666;">Apellidos</td>
        <td style="border-right: 1px solid #666;">Correo</td>
        <td style="border-right: 1px solid #666;">Usuario</td>
        <td style="border-right: 1px solid #666;">Privilegio</td>    
    </tr>';
foreach($sql as $key => $value) {
 html .=' <tr>
            <td style="border-right: 1px solid #666; border: 1px solid #666;">'.$value["nombres"].'</td>
            <td style="border-right: 1px solid #666; border: 1px solid #666;">'.$value["apellidos"].'</td>
            <td style="border-right: 1px solid #666; border: 1px solid #666;">'.$value["correo"].'</td>
            <td style="border-right: 1px solid #666; border: 1px solid #666;">'.$value["usuario"].'</td>
            <td style="border-right: 1px solid #666; border: 1px solid #666;">'.$value["privilegio"].'</td>

    </tr>';
}
$html.='</table>'
$pdf->writeHTML($html, false, false, false, false, ''); //Salida PDF
$pdf->Output('reporte.pdf', 'I');
    
answered by 30.04.2018 в 22:50