【问题标题】:TCPDF How can I create tables with looped content?TCPDF 如何创建包含循环内容的表格?
【发布时间】:2017-07-20 19:03:49
【问题描述】:

我想为我的 TCPDF 创建一个表,其中的内容是从 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...

它一定和桌子有关。但我不知何故需要创建一个不会被新页面破坏的表。


更新:

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

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

但还是有错误:

注意:未定义的变量:b

【问题讨论】:

    标签: php mysql foreach html-table tcpdf


    【解决方案1】:

    操作员写道:

    我终于有了一个可行的解决方案:

    $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, '');
    
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-29
    • 2016-01-11
    • 2020-05-15
    • 1970-01-01
    • 1970-01-01
    • 2016-10-05
    相关资源
    最近更新 更多