2011-12-15 8 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

+0

$ export_idをエコーし​​てループに入ることを確認できますか? forの引数を数えるのも良い考えではありません。 forループの前に$ count = count($ _ POST ['checkbox'])を設定すると、パフォーマンスが向上します。 – Robert

+0

$ export_idをエコーし​​ている - 部分的には、選択されているチェックボックスからIDが表示されていますが、それはなぜですか?しかし、それとは別に、例えばID 305を選択して「305」と表示された場合は、それは問題ありません。 – user990767

答えて

1

あなたの元のコードは、個別に(それを処理するために長いと面倒な方法を)checkboxed IDのそれぞれを照会して、個別ごとに結果をダンプしようとしていました行(あなたのためにうまくいくつもりはない)。

これを試してください。 (注:テストされていないが、開発するあなたに良い出発点を与える必要があり。)それは動作しませんでしたどのように

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(); 
+0

私はすべての出力を得ていません - $出力をエコーし​​て、値を更新するためにほぼ同じクエリを使用しています。チェックボックスもあり、うまく動作します。何が問題なのか分かりません。 – user990767

+0

var_dumpを使用して変数をチェックし、どこが間違っているかを確認します。私の答えの最初の部分について:クエリが問題ではない、問題はすべてのチェックボックスのクエリを実行していると、$結果変数をオーバーライドするので、実際にはループ内の最後のクエリの結果のみを取得します。 –

+0

あなたは私の最初の質問をチェックしてください、私は今使用しているコードで編集しました、これは今、すべての選択ボックスでも私が持っている唯一の問題は、ヘッダーを投稿しましたが、無駄です。出力間に改行を入れる方法を教えていただければ幸いです。 – user990767

関連する問題