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