2011-03-10 24 views
6

en mi php,TCPDF - mesa de impresión de MySQL

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, ''); 
} 

Imprime toda la mesa siempre en mi pdf. Pero quiero imprimir <table> y </table> para una sola instancia y luego quiero imprimir en bucle las filas intermedias. ¿¿Como puedo resolver esto??

Respuesta

10
$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, ''); 
+0

de Deacon. Muchas gracias .. ':)' –

2

Si lo que quiere decir es que quiere una tabla que incluya todos los resultados como filas, solo repita la fila de impresión y migre las etiquetas de apertura y cierre fuera del ciclo.

$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, ''); 
+0

no funciona .. Esta muestra sólo una única fila .. y estoy recibiendo resultado sólo si '$ pdf-> writeHTML ($ TBL, verdadero, falso, falso, falso, ' '); 'está dentro del bucle –

+0

@blasteralfred - ¿Puedes editar en tu declaración de SQL para que podamos estar seguros de que está obteniendo varias filas? – DeaconDesperado

+0

incluso sin sql, en un simple vistazo, se sobresalta utilizando datos ficticios ... Encontré la respuesta de andre útil ... Pero aún estoy buscando una sugerencia como la del –

0
$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, ''); 
1

sin duda lo hacer algo como esto:

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

Si no puede organizar su capa de presentación con las plantillas al menos que sea lo más separado posible de la lógica real.

Puede read about sprintf here y implode here