2011-12-15 79 views
1

我有一張顯示一些mysql數據的表格,每個條目都有一個複選框來選擇單個條目,現在我希望能夠將這些選定條目導出到xml或txt文件中,我試過這個:將mysql導出到txt/xml文件並下載它

<?php 
if ($_POST['exporttxt']) { 
    for ($i = 0; $i < count($_POST['checkbox']); $i++) { 
     $export_id = $checkbox[$i]; 

     $sql = "SELECT * FROM table WHERE id='$export_id'"; 
     $result = mysql_query($sql); 
    } 


    $output = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<root>\n"; 
    if ($result->num_rows > 0) { 
     while ($myrow = $result->fetch_assoc()) 
     { 
      $output .= "\t<row>\n"; 
      foreach ($myrow as $_name => $_value) 
      { 
       $output .= "\t\t<$_name>$_value</$_name>\n"; 
      } 
      $output .= "\t</row>\n"; 
     } 
    } 
    $output .= "</root>"; 
} 

header('content-type: text/xml'); 
header('content-disposition: attachment; filename=data_export.xml'); 
echo $output; 
exit; 

?> 

但這並沒有工作,任何提示?

我已經改變了代碼一點,我現在用的這個

<?php 
if ($_POST['exporttxt']) { 
for($i=0;$i<count($_POST['checkbox']);$i++){ 
    $export_id = $checkbox[$i]; 
$text = mysql_query("SELECT code FROM ticket WHERE id='$export_id'"); 
$text = mysql_fetch_assoc($text); 
$text = $text["code"]; 

ob_end_flush(); 
header("Content-type: text/plain"); 
header("Content-disposition: attachment;filename=\"filename.txt\""); 
    header("Content-Type: application/force-download"); 
    header("Content-Type: application/octet-stream"); 
    header("Content-Type: application/download"); 
    header("Content-Description: File Transfer"); 
    header("Content-Length: ".strlen($output).";\n"); 



echo($text); 


} 
} 


?> 

我現在得到我的屏幕上正確的輸出,但它不會給我下載的文件?

+0

怎麼沒工作? – Repox 2011-12-15 21:11:29

+0

你能迴應$ export_id以確保你進入循環。對於爭論中的計數也不是一個好主意。在for循環之前設置$ count = count($ _ POST ['checkbox']),您將獲得更好的性能。 – Robert 2011-12-15 21:13:42

回答

1

你的原代碼查詢每個單獨的checkboxed標識(長期和艱苦的方式來處理它),併爲每試圖單獨傾倒的結果排(不適合你)。

試試這個。 (注:未經檢驗的,但應該給你一個很好的起點,發展

if($_POST['exporttxt']){ 

    if(count($_POST['checkbox'])>0){ 

    // If the checkbox values are meant to all be integers, you might want to perform some validation/sanitisation/filtering here 
    // Up to you to do that 

    // Collapse the IDs from the checkboxes into a comma-delimited string 
    $export_ids = implode(',' , $_POST['checkbox']); 

    // Template the SQL Query 
    $sqlTpl = 'SELECT code FROM ticket WHERE id IN (%s)'; 
    // Compile the SQL Query String 
    $sqlStr = sprintf($sqlTpl , $export_ids); 

    // Execute the SQL Query 
    if(!($sqlRes = mysql_query($sqlStr))){ 
     // SQL Error - Log it, Handle it 
    }elseif(mysql_num_rows($sqlRes)==0) { 
     // No Rows Returned - Log it, Handle it 
    }else{ 
     // We have results - process them 
     $text = array(); 
     while($r = mysql_fetch_assoc($sqlRes)){ 
     // Looping through the returned rows, adding them to the $text array 
     $text[] = $r['code']; 
     } 
     // Collapse the $text array down into a normal string, with one element per line 
     $output = implode("\n" , $text); 

     // Output Handling from @narcisradu's answer 
     header("Pragma: public"); 
     header("Expires: 0"); 
     header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); 
     header("Cache-Control: private",false); 
     header("Content-Transfer-Encoding: binary;\n"); 
     header("Content-Disposition: attachment; filename=\"filename.txt\";\n"); 
     header("Content-Type: application/force-download"); 
     header("Content-Type: application/octet-stream"); 
     header("Content-Type: application/download"); 
     header("Content-Description: File Transfer"); 
     header("Content-Length: ".strlen($output).";\n"); 
     echo $output; 

     die; // Prevent any further output 
    } 

    }else{ 
    // No Checkboxes Checked 
    echo 'You must select one or more checkboxes to export something, muppet.'; 
    } 

} 
0

首先:

for($i=0;$i<count($_POST['checkbox']);$i++){ 
$export_id = $checkbox[$i]; 

$sql = "SELECT * FROM table WHERE id='$export_id'"; 
$result = mysql_query($sql);} 

這段代碼實際上做一個查詢中_POST [「複選框」]數組的每一個元素,但是,你正在使用的最後一個查詢的結果,你是不是解析每個查詢的結果。

請確保生成的XML格式正確並且實際上有一些數據,方法是直接在瀏覽器中顯示。

之後,你可以嘗試強制下載:

header("Pragma: public"); 
    header("Expires: 0"); 
    header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); 
    header("Cache-Control: private",false); 
    header("Content-Transfer-Encoding: binary;\n"); 
    header("Content-Disposition: attachment; filename=\"".urlencode(basename($file_name))."\";\n"); 
    header("Content-Type: application/force-download"); 
    header("Content-Type: application/octet-stream"); 
    header("Content-Type: application/download"); 
    header("Content-Description: File Transfer"); 
    header("Content-Length: ".strlen($output).";\n"); 
    ob_end_flush(); 
相關問題