Question

At first, I want to excuse me for my English, but I have a problem

I create a table form a database in this way:

//the connection goes right, so I don't put it into my code

<table id="database">
<tr>
    <th>Title1</th>
    <th>Title2</th>
    <th>Title3</th>
    <th>Title4</th>
</tr>
<?php
    $result = mysql_query("SELECT * FROM `table`");
    while($row = mysql_fetch_array($result))
    {
        echo "<tr>";
            echo "<td>".$row['Column1']."</td>";
            echo "<td>".$row['Column2']."</td>";
            echo "<td>".$row['Column3']."</td>";
            echo "<td>".$row['Column4']."</td>";
        echo "</tr>";
    }
?>
</table>

The problem is that I'm trying to put these data into file, with the reason to save these columns local on my computer, because I want to delete the table online.

I already tried TCPDF and html2pdf

It's not obligated to put these in a pdf, but I want these just saved.

I hope you can help me.

UPDATE: There are 2 good solutions given. But I had a problem with my Acrobat Reader so I chose this one:

<?php
    ob_start();
?>
<table id="database">
    <tr>
        <th>Title1</th>
        <th>Title2</th>
        <th>Title3</th>
        <th>Title4</th>
    </tr>
<?php
    $result = mysql_query("SELECT * FROM `table`");
    while($row = mysql_fetch_array($result))
    {
        echo "<tr>";
            echo "<td>".$row['Column1']."</td>";
            echo "<td>".$row['Column2']."</td>";
            echo "<td>".$row['Column3']."</td>";
            echo "<td>".$row['Column4']."</td>";
        echo "</tr>";
    }
?>
</table>
<?php
    $output = ob_get_contents();
    $filename = "column".time().".xls";
    file_put_contents($filename, $output) or die("can't write to the file");
?>
<a href="<?php echo $filename; ?>">Print</a>
Was it helpful?

Solution

You can simply use below code of you want to save as HTML table, or you can export your data from SQL and then import to your local SQL server.

<?php
ob_start();
?>
<table id="database">
<tr>
    <th>Title1</th>
    <th>Title2</th>
    <th>Title3</th>
    <th>Title4</th>
</tr>
<?php
    $result = mysql_query("SELECT * FROM `table`");
    while($row = mysql_fetch_array($result))
    {
        echo "<tr>";
            echo "<td>".$row['Column1']."</td>";
            echo "<td>".$row['Column2']."</td>";
            echo "<td>".$row['Column3']."</td>";
            echo "<td>".$row['Column4']."</td>";
        echo "</tr>";
    }
?>
</table>
<?php
$output = ob_get_contents();
$filename = "column".time().".txt";
file_put_contents($filename, $output) or die("can't write to the file");
?>

OTHER TIPS

For easy PDF, I would strongly suggest mPDF.

http://www.mpdf1.com/mpdf/index.php

  1. Download mPDF

  2. Unpack it on your localserver

  3. Here is a working example, based on your code.


<?
//path to your newly installed mPDF
include('/mpdf/mpdf.php');

$html='
<table id="database">
<tr>
    <th>Title1</th>
    <th>Title2</th>
    <th>Title3</th>
    <th>Title4</th>
</tr>';

//$result = mysql_query("SELECT * FROM `table`");
//here just a test
$rows=array();
$rows['Column1']='A';
$rows['Column2']='B';
$rows['Column3']='C';
$rows['Column4']='D';

//while($row = mysql_fetch_array($result))  {
//here just a test
foreach ($rows as $row)  {
    $html.="<tr>";
    $html.="<td>".$row['Column1']."</td>";
    $html.="<td>".$row['Column2']."</td>";
    $html.="<td>".$row['Column3']."</td>";
    $html.="<td>".$row['Column4']."</td>";
    $html.="</tr>";
}
$html.='</table>';

$mpdf=new mPDF('c');
$mpdf->debug = true;
$mpdf->WriteHTML($html);
$mpdf->Output('test','D');
?>

calling this script will produce a nice PDF with the name test.php stored on your download-path.


enter image description here

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top