0

我有一个 php 文件(add_member.php)。我正在通过执行 SQL 查询来创建动态表。该文件的代码如下:

$sql = SELECT * FROM member_details WHERE lname='ABC';

$n = new data();

$res = $n -> querySend($sql);

?>

<table border="1" cellpadding="3" cellspacing="1">
   <tr>
      <td align="center"></td>
      <td align="center">First Name</td>
      <td align="center">Last Name</td>
      <td align="center">Caste</td>
      <td align="center">Residence address</td>
      <td align="center">Education</td>
   </tr>
   <?php

       $i=1;

       while($row = mysql_fetch_array($res))
       { 
           $member_no = $row['member_no'];

           $total_member = "SELECT COUNT(*) AS total_member FROM family_member_details WHERE member_no =" .$member_no;

           $total_res = $n -> querySend($total_member);

           $row_total = mysql_fetch_array($total_res);        

          ?>

           <tr>
               <td align="center" rowspan="<?php echo $row_total['total_member']+1;?>"><?php echo $i;?></td>
               <td align="center"><?php echo $row['fname'];?></td>
               <td align="center"><?php echo $row['lname'];?></td>
               <td align="center"><?php echo $row['caste'];?></td>
               <td align="center"><?php echo $row['residence_addr'];?></td>
               <td align="center"><?php echo $row['education'];?></td>
           </tr>
           <?php 
                $family_sql = "SELECT * from family_member_details WHERE member_no = $member_no";
                $family_res = $n -> querySend($family_sql);

                while($row1 = mysql_fetch_array($family_res))
                {
                   ?>
                    <tr>
                        <td align="center"><?php echo $row1['name']?></td>
                        <td align="center"><?php echo $row1['name']?></td>
                        <td align="center"><?php echo $row1['name']?></td>
                        <td align="center"><?php echo $row1['name']?></td>
                        <td align="center"><?php echo $row1['name']?></td>
                    </tr>   
         <?php  }
                $i++; 
          } ?>
    </table>

现在单击按钮后,我想在 PDF 文件中创建相同的表格。为此,我决定使用 TCPDF 库。但为此,我必须将 HTML 内容提供给 TCPDF 文件。现在我的问题是我应该如何从 PHP 文件中获取动态生成的表格的 HTML 并将此内容写入文本文件?任何人都可以在这方面帮助我吗?任何帮助将不胜感激。

4

1 回答 1

2

无需将表格直接显示到浏览器页面,只需将文本存储在变量中并回显它……然后您可以将 var 发送到 TCPDF 库。

$dyn_table = '<table border="1" cellpadding="3" cellspacing="1"><tr><td align="center">/td><th align="center">First Name</th><th align="center">Last Name</th><th align="center">Caste</th><th align="center">Residence address</th><th align="center">Education</th></tr>';

$i = 1;
while ($row = mysql_fetch_array($res)) {
    $member_no = $row['member_no'];
    $total_member = "SELECT COUNT(*) AS total_member FROM family_member_details WHERE member_no =" . $member_no;

    $total_res = $n->querySend($total_member);

    $row_total = mysql_fetch_array($total_res);

    $dyn_table .= '<tr><td align="center" rowspan="' . $row_total['total_member'] + 1 . '">' . $i . '</td><td align="center">' . $row['fname'] . '</td><td align="center">' . $row['lname'] . '</td><td align="center">' . $row['caste'] . '</td><td align="center">' . $row['residence_addr'] . '</td><td align="center">' . $row['education'] . '</td></tr>';

    $family_sql = "SELECT * from family_member_details WHERE member_no = $member_no";
    $family_res = $n->querySend($family_sql);

    while ($row1 = mysql_fetch_array($family_res)) {
        $dyn_table .= '<tr><td align="center">' . $row1['name'] . '</td><td align="center">' . $row1['name'] . '</td><td align="center">' . $row1['name'] . '</td><td align="center">' . $row1['name'] . '</td><td align="center">' . $row1['name'] . '</td></tr>';
    }
    $i++;
}
$dyn_table .= '</table>';
echo $dyn_table;

编辑

为了将此 html 发布到您的 TCPDF 库,我将使用 AJAX 来防止另一个页面请求/加载。我更喜欢使用JQuery,因为它极大地简化了这个过程。这是您可以做到的一种方法:

<input type="button" name="TCPDF" id="submitToTCPDF" />
<script type="text/javascript">
    var url = 'php/script/to/handle/post';
    var data = {'table_html': '<? echo $dyn_table; ?>'};

    $('#TCPDF').click(function(){
        $.ajax({
            type: "POST",
            url: url,
            data: data,
            success: function($result){
                // Do whatever after html is submitted


            }
        });
    });
</script>

您可以在此 StackOverflow 问题中阅读有关Jquery 的 AJAX 发布方法的更多信息。

于 2013-10-22T13:05:12.497 回答