TCPDF 如何创建包含循环内容的表格?

TCPDF How can I create tables with looped content?

我想为我的 TCPDF 创建一个 table,其中的内容是从 mySQL 数据库插入的:

$html = '<table nobr="true">';
$pdf->writeHTML($html, true, false, true, false, '');

    $pdo = $db->prepare("SELECT * FROM data WHERE id=?");  
    $pdo->execute(array($id)); 
    while ($row = $pdo->fetch(PDO::FETCH_ASSOC)) {  
    $html = '<tr><td>'.$row['name'].'</td></tr>';
    $pdf->writeHTML($html, true, false, true, false, '');
} 

$html = '</table>';
$pdf->writeHTML($html, true, false, true, false, '');

但是我收到很多错误信息:

Notice: Undefined index: rows in ...
Warning: array_push() expects parameter 1 to be array, null given in...
Notice: Undefined variable: cellspacingx in...
Notice: Undefined variable: cellspacing in... 
Notice: Undefined index: rows in...

一定和table有关。但是我不知何故需要创建一个 table,它不会被新页面破坏。


更新:

我现在测试了另一个解决方案:

$a = '<table nobr="true">';

    $pdo = $db->prepare("SELECT * FROM data WHERE id=?");  
    $pdo->execute(array($id)); 
    while ($row = $pdo->fetch(PDO::FETCH_ASSOC)) {  
    $b .= '<tr><td>'.$row['name'].'</td></tr>';

} 

$c = '</table>';
$pdf->writeHTML($a.$b.$c, true, false, true, false, '');

但还是报错:

Notice: Undefined variable: b

操作写道:

I finally have a working solution:

$a = '<table nobr="true">';

$pdo = $db->prepare("SELECT * FROM data WHERE id=?");  
$pdo->execute(array($id)); 
$b = '';
while ($row = $pdo->fetch(PDO::FETCH_ASSOC)) {  
$b .= '<tr><td>'.$row['name'].'</td></tr>';

} 

$c = '</table>';
$pdf->writeHTML($a.$b.$c, true, false, true, false, '');