0

I have a php file(add_member.php). I'm creating a dynamic table by executing SQL query. The code of this file is as follows:

$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>

Now upon clicking on a button I want to create the same table into PDF file. For this purpose I decided to use TCPDF library. But for it I've to provide the HTML content to TCPDF file. Now my issue is how should I get the HTML of a dynamically generated table from PHP file and write this content to the text file? Can anyone please help me in this regard? Any help would be highly appreciated.

Anand Jaju
  • 458
  • 3
  • 12
  • 28

1 Answers1

2

Instead of displaying your table directly to the browser page, simply store the text in a variable and echo it...then you can send the var to the TCPDF library.

$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;

EDIT

In order to post this html to your TCPDF library, I would use AJAX to prevent another page request/load. I prefer to use JQuery as it simplifies this process immensely. Here is one way you could do it:

<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>

You can read more about Jquery's AJAX post method in this StackOverflow question.

Community
  • 1
  • 1
Pastor Bones
  • 7,183
  • 3
  • 36
  • 56
  • suppose after echo $dyn_table; there is then how to send that var to tcpdf library onclick of a button? – Anand Jaju Oct 23 '13 at 06:03
  • I updated my answer to help show how to go about performing an AJAX request as further shown in the link at the bottom of the answer. – Pastor Bones Oct 23 '13 at 13:37