-1

I've created a PHP file which shows the results from a MySQL database as we all create like in

echo "<table><tr><td>";
...
echo "</td></tr></table>";

But now I want to make a button at the bottom of the table, something like 'save report', which saves the created table into HTML format.

So how could it be done?

4

2 回答 2

2

您可以使用以下脚本:

index.php
在 index.php 中,您有 HTML 表。

<?php

$contents = "<table><tr><td>A</td><td>B</td></tr><tr><td>One</td><td>Two</td></tr><tr><td>Three</td><td>Four</td></tr></table>"; // Put here the source code of your table.

?>
<html>
    <head>
        <title>Save the file!</title>
    </head>
    <body>

        <?php echo $contents; ?>

        <form action="savefile.php" method="post">
            <input type="hidden" name="contents" value="<?php echo htmlspecialchars($contents); ?>">
            <input type="submit" value="Save file" />
        </form>
    </body>
</html>

savefile.php
然后使用文件 savefile.php 弹出浏览器的下载对话框来保存文件。

<?php

if ($_SERVER['REQUEST_METHOD'] == "POST") {
    header('Content-type: text/html');
    header('Content-Disposition: attachment; filename="table.html"');

    echo $_POST['contents'];
}

?>
于 2012-11-22T11:23:37.830 回答
1

我想你想将 PHP/MySQL 生成的报告保存为 HTML 文件?

<?php

// Open file for writing
$fileHandle = fopen("/DestinationPath/DestinationFile.html", "w");

// Dump File
$head = "<html><head><title>my reports</title></head><body>\n";
fwrite($fileHandle, $head);
$sql = mysql_query("Your sql query here");
while ($result = mysql_fetch_assoc($sql)) {
    $line = $result['yourmysqlfield']."\n";
    fwrite($fileHandle, $line);

}
$foot = "</body></html>\n";
fwrite($fileHandle, $foot);

// Close File
close($fileHandle);

?>
于 2012-11-22T11:34:34.307 回答