0

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>
Kenny
  • 177
  • 2
  • 3
  • 14
  • So you are looking to export the data from the online table, and keep a copy on your local machine? if so how do you store the data online, what database are you using? – Tom Sep 10 '13 at 11:33
  • @Tom I use a database, let's call it workplaces. So every user can see a list of the workplaces. So I can put the database online from `PHPMyAdmin`, but I want users can save the database. – Kenny Sep 10 '13 at 11:42

2 Answers2

1

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");
?>
1

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

davidkonrad
  • 83,997
  • 17
  • 205
  • 265
  • EDIT: I found the mistake, thank you very much, this works, but my Adobe Reader can't open this, this is the last problem :) – Kenny Sep 10 '13 at 12:02
  • @Kenny, you must run it as a normal PHP-script, eg `http://localhost/test.php` or whatever your path is. I really doubt you get the result above, since nothing is echoed to the browser by PHP :) Its just generating af string of HTML and pass that to mPDF. – davidkonrad Sep 10 '13 at 12:08
  • The first mistake was that I typed `` instead of ` – Kenny Sep 10 '13 at 12:12
  • Are you using ubuntu and recently upgraded Adobe Reader? Mine was screwed up too - `sudo apt-get install nscd` did the trick (dont ask me why, it just solved the problem :). – davidkonrad Sep 10 '13 at 12:18
  • try to copy paste the code above, and this code only, into a new `.php`-file, and be sure there is no blanks before and after ` ?>`. As far as I know, mPDF do not have any problems with bad PDF'ing, operating systems or things like that. This is why I'll recommend it. – davidkonrad Sep 10 '13 at 12:21
  • No, I'm using Windows 7 - I'm working on another computer - and I upgraded it. I'll search for it on the Internet, but your answer is the solution to my problem, thank you very much! – Kenny Sep 10 '13 at 12:26