0

Tengo creado mi código para para poder utilizar la librería TCPDF. Hasta ahí todo bien. Mi problema es cuando intento crear mi tabla.

Los datos me llegan desde la base de datos pero me muestra siempre el mismo dato.

Esta es la estructura de la tabla:

$html = <<<EOF


    <table border="0" cellpadding="2" text-align="center" cellspacing="1" style="width: 100%; margin-top: 1px;">
        <tr>
            <td style="width: 50%; height: 150px;  padding: 5px;">
                <table border="0" cellpadding="5" cellspacing="1">
                    <tr>
                        <td style="width: 80%;">$pintarDatos
                        </td>
                    </tr>
                </table>
            </td>
            <td style="width: 50%; height: 150px;">
                <table border="0" cellpadding="5" cellspacing="1">
                    <tr>
                        <td style="width: 80%;">$pintarDatos
                        </td>
                    </tr>
                </table>
            </td>
        </tr>
        <tr>
            <td style="width: 50%; height: 150px;  padding: 5px;">
                <table border="0" cellpadding="5" cellspacing="1">
                    <tr>
                        <td style="width: 80%;">$pintarDatos
                        </td>
                    </tr>
                </table>
            </td>
            <td style="width: 50%; height: 150px;">
                <table border="0" cellpadding="5" cellspacing="1">
                    <tr>
                        <td style="width: 80%;">$pintarDatos
                        </td>
                    </tr>
                </table>
            </td>
        </tr>
        <tr>
            <td style="width: 50%; height: 150px;  padding: 5px;">
                <table border="0" cellpadding="5" cellspacing="1">
                    <tr>
                        <td style="width: 80%;">$pintarDatos
                        </td>
                    </tr>
                </table>
            </td>
            <td style="width: 50%; height: 150px;">
                <table border="0" cellpadding="5" cellspacing="1">
                    <tr>
                        <td style="width: 80%;">$pintarDatos
                        </td>
                    </tr>
                </table>
            </td>
        </tr>
        <tr>
            <td style="width: 50%; height: 150px;  padding: 5px;">
                <table border="0" cellpadding="5" cellspacing="1">
                    <tr>
                        <td style="width: 80%;">$pintarDatos
                        </td>
                    </tr>
                </table>
            </td>
            <td style="width: 50%; height: 150px;">
                <table border="0" cellpadding="5" cellspacing="1">
                    <tr>
                        <td style="width: 80%;">$pintarDatos
                        </td>
                    </tr>
                </table>
            </td>
        </tr>
        <tr>
            <td style="width: 50%; height: 150px;  padding: 5px;">
                <table border="0" cellpadding="5" cellspacing="1">
                    <tr>
                        <td style="width: 80%;">$pintarDatos
                        </td>
                    </tr>
                </table>
            </td>
            <td style="width: 50%; height: 150px;">
                <table border="0" cellpadding="5" cellspacing="1">
                    <tr>
                        <td style="width: 80%;">$pintarDatos
                        </td>
                    </tr>
                </table>
            </td>
        </tr>
        <tr>
            <td style="width: 50%; height: 150px;  padding: 5px;">
                <table border="0" cellpadding="5" cellspacing="1">
                    <tr>
                        <td style="width: 80%;">$pintarDatos
                        </td>
                    </tr>
                </table>
            </td>
            <td style="width: 50%; height: 150px;">
                <table border="0" cellpadding="5" cellspacing="1">
                    <tr>
                        <td style="width: 80%;">$pintarDatos
                        </td>
                    </tr>
                </table>
            </td>
        </tr>
    </table>


EOF;

Y este es el bucle para añadir los datos a la variable $pintarDatos.

while ($datos = $consulta->fetch_array()){
  $pintarDatos = <<<EOF
                
            <p>Study Plan: $datos['estudio']<br>
            Trial Number: $datos['ensayo']<br>
            Crop: $datos['cultivo']<br>
            Sampling Timing: $datos['muestreo']<br>
            Test System: $datos['matrix']<br>
            Variant: <b>$datos['plot']</b><br>
            Specimen Code: <b>$datos['muestra']</b></p>
                            
        EOF;
};

Solo me muestra el ultimo dato de la consulta.

Otra cosa que me pasa es que como veis, en total sería 12 datos. En la consulta me llegan 4 que son los que quiero pero cuando me muestra los datos, me muestra los 12 y no 4 que son los que me devuelve la consulta.

En resumen, me muestra 12 datos en vez de 4 que son los de la consulta y los 12 datos iguales

Gracias de antemano por la ayuda.

2 respuestas 2

0

debieras hacer algo como esto :

echo "<table>";
echo "<tr>";
echo "<td>Study Plan:</td>";
echo "<td>Trial Number:</td>";
echo "<td>Crop:</td>";
.. etc ..
echo "</tr>";

while ($datos = $consulta->fetch_array()){
  echo "<tr>";
      echo "<td>".$datos['estudio']."</td>";
      echo "<td>".$datos['ensayo']."</td>";
      echo "<td>".$datos['cultivo']."</td>";

      ... etc ...

  echo "</tr>";                 
};
echo "</table>";
3
  • El problema es que la librería TCPDF no me deja ejecutar los echo. Por eso tengo que meter los datos en una variable
    – Maginon
    el 19 mar. 2021 a las 12:43
  • es obligacion que uses tcpdf, porque si no es asi, usa fpdf y con el codigo anterior lo lograras sin problemas fpdf.org el 19 mar. 2021 a las 12:48
  • Sí, por obligación tengo que usar la librería TCPDF
    – Maginon
    el 19 mar. 2021 a las 12:51
0

Estimado, has intentado crear la estructura HTML y luego pasarsela a TCPDF. Te dejo el siguiente ejemplo.

$data = "";

$data .= "<table>";
$data .= "<tr>";
$data .= "<td>Study Plan:</td>";
$data .= "<td>Trial Number:</td>";
$data .= "<td>Crop:</td>";
// bla bla bla
$data .= "</tr>";

while ($datos = $consulta->fetch_array()){
    $data .= "<tr>";
    $data .= "<td>".$datos['estudio']."</td>";
    $data .= "<td>".$datos['ensayo']."</td>";
    $data .= "<td>".$datos['cultivo']."</td>";

      // bla bla bla

    $data .= "</tr>";                 
};
$data .= "</table>";

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge you have read our privacy policy.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.