TCPDF - printing table from mysql

折月煮酒 提交于 2019-11-29 14:46:30

问题


in my php,

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

Prints the whole table always in my pdf. But I want to print <table> and </table> for a single instance and then want to loop printing the rows in between. How can I solve this??


回答1:


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



回答2:


If what you mean is you want one table that includes all the results as rows, only loop the row printing and migrate the opening and closing tags outside the loop.

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



回答3:


I would certainly do something like this:

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

If you can't organize your presentation layer with templates at least make it as separated as you can from the actual logic.

You can read about sprintf here and implode here




回答4:


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


来源:https://stackoverflow.com/questions/5261684/tcpdf-printing-table-from-mysql

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!