2012-11-22 232 views
-1

我創建了一個PHP文件,顯示從MySQL數據庫的結果我們都創造像如何將保存爲按鈕的.html文件保存爲PHP?

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

但現在我想打在桌子底部的按鈕,像「保存報告',它將創建的表格保存爲HTML格式。

那麼它是如何做到的?

+0

'CTRL + S'我覺得呢? – EaterOfCode

+0

這沒什麼意義..到目前爲止你做了什麼?你說過你創建了一個PHP腳本來製作一個HTML表格,但接着詢問如何將PHP表格保存爲HTML格式?世界上沒有這樣的PHP表「 – George

+0

看到這個http://php.net/manual/en/function.fwrite.php 谷歌是你的朋友(Y) – Ace

回答

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']; 
} 

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

?>