thaidomizil Posted December 15, 2011 Share Posted December 15, 2011 Hi, i have a table which shows some mysql data, every entry has a checkbox to select individual entries, now i want to be able to export those selected entries into a xml or txt file, i tried this: <?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; ?> but that doesn't seem to work. Any hints ? Quote Link to comment https://forums.phpfreaks.com/topic/253259-export-mysql-to-txt-xml-file-and-download-it/ Share on other sites More sharing options...
scootstah Posted December 15, 2011 Share Posted December 15, 2011 Maybe if you define "doesn't work" we would be able to help you. Quote Link to comment https://forums.phpfreaks.com/topic/253259-export-mysql-to-txt-xml-file-and-download-it/#findComment-1298260 Share on other sites More sharing options...
thaidomizil Posted December 15, 2011 Author Share Posted December 15, 2011 Definition of doesn't work: it doesn't do anything. Is that accurate enough ? Quote Link to comment https://forums.phpfreaks.com/topic/253259-export-mysql-to-txt-xml-file-and-download-it/#findComment-1298266 Share on other sites More sharing options...
kicken Posted December 15, 2011 Share Posted December 15, 2011 For one, mysql_query doesn't return an object, but your trying to use it's result as an object. Second, your $result variable is only going to contain the data for the last select, all the rest are ignored. Third, you can do everything in a single query rather than using a loop like your doing. Put all the id's in an IN clause in the query. Quote Link to comment https://forums.phpfreaks.com/topic/253259-export-mysql-to-txt-xml-file-and-download-it/#findComment-1298272 Share on other sites More sharing options...
thaidomizil Posted December 15, 2011 Author Share Posted December 15, 2011 Hello kicken, thanks for your reply, i noticed the second point you mentioned but couldn't really figure out why that was happening / or how to fix it? to third: it would be great if you could give me an example. Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/253259-export-mysql-to-txt-xml-file-and-download-it/#findComment-1298276 Share on other sites More sharing options...
thaidomizil Posted December 15, 2011 Author Share Posted December 15, 2011 I've got it to show the proper output atleast with all select boxes working, but it won't download the .txt file, i've tried with this headers and code: <?php if ($_POST['exporttxt']) { for($i=0;$i<count($_POST['checkbox']);$i++){ $export_id = $checkbox[$i]; $text = mysql_query("SELECT code FROM psc 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); } } ?> Like i said, the output itself is correct, but it won't offer me to download the .txt file. Quote Link to comment https://forums.phpfreaks.com/topic/253259-export-mysql-to-txt-xml-file-and-download-it/#findComment-1298296 Share on other sites More sharing options...
scootstah Posted December 15, 2011 Share Posted December 15, 2011 You are ending the output buffer and clearing it, so $text is going to output nothing. Try this instead: $text = ob_get_clean(); 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); Quote Link to comment https://forums.phpfreaks.com/topic/253259-export-mysql-to-txt-xml-file-and-download-it/#findComment-1298297 Share on other sites More sharing options...
thaidomizil Posted December 15, 2011 Author Share Posted December 15, 2011 You are ending the output buffer and clearing it, so $text is going to output nothing. Try this instead: The output on the screen is correct, but it still doesn't offer me any download, also tried with the code you suggested, same result. Quote Link to comment https://forums.phpfreaks.com/topic/253259-export-mysql-to-txt-xml-file-and-download-it/#findComment-1298301 Share on other sites More sharing options...
scootstah Posted December 15, 2011 Share Posted December 15, 2011 If you are sending those headers there shouldn't be any output on the screen. Quote Link to comment https://forums.phpfreaks.com/topic/253259-export-mysql-to-txt-xml-file-and-download-it/#findComment-1298304 Share on other sites More sharing options...
thaidomizil Posted December 15, 2011 Author Share Posted December 15, 2011 Then something else is wrong ? If i select some rows and press my submit button, it submits the form, and shows me the output of what i selected i.e some codes which i actually want to write to the .txt file, i definitely included the headers. Quote Link to comment https://forums.phpfreaks.com/topic/253259-export-mysql-to-txt-xml-file-and-download-it/#findComment-1298305 Share on other sites More sharing options...
scootstah Posted December 15, 2011 Share Posted December 15, 2011 In my code as well? Because in your code, you call ob_end_flush which is going to dump the buffer and output the results. Since there is output, the headers wouldn't have been sent and thus nothing will download. But in my code, there is no output (unless it's coming from somewhere else). I'm guessing you just don't have errors on so you didn't notice all the errors from calling headers after output. Quote Link to comment https://forums.phpfreaks.com/topic/253259-export-mysql-to-txt-xml-file-and-download-it/#findComment-1298308 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.