I tried the following code for exporting csv, xls, txt & xml files from the mysql database.
<?php
include("includes/config.php");
if($_POST["frmDownloadFiles"]){
$output = "";
$line_termineted="
";
if( $_POST["frmDownloadFiles"] =="CSV") {
$field_termineted= ",";
}
if( $_POST["frmDownloadFiles"] =="XLS") {
$field_termineted= "\t";
}
if ($_POST["frmDownloadFiles"] =="TXT") {
$field_terminated= " ";
}
if ($_POST["frmDownloadFiles"] =="XML") {
$field_terminated= "\t";
}
$enclosed='';
$escaped="\\";
$export_schema = "Name".$field_termineted."Code".$field_termineted."Email".$field_termineted."Designation".$field_termineted."Salary";
$dataQuery = doSelectCsv();
//$handle = fopen($dataQuery, "w+");
// while ($strBookData = fputcsv($handle, 10000, ",")) { // To get Array from CSV
// $strDatas[] = $strBookData;
// }
// printArray($strDatas); exit;
$strDatas = array();
$strDatas = $dataQuery;
//printArray($strDatas); exit;
$output.= $export_schema;
//printArray($field_termineted);
$p=0;
for($k=0; $k<count($strDatas); $k++) {
$p++;
if( $_POST["frmDownloadFiles"] =="CSV") {
$output.= $line_termineted;
}
if( $_POST["frmDownloadFiles"] =="XLS") {
$output.= $line_termineted;
}
if( $_POST["frmDownloadFiles"] =="TXT") {
$output.= $line_termineted;
}
echo "<employee_details>";
if( $_POST["frmDownloadFiles"] =="XML") {
echo "<Row>";
echo "<name>" . $strDatas[$k]['0'] . "</name>";
echo "<code>" . $strDatas[$k]['1'] . "</code>";
echo "<mail>" . $strDatas[$k]['2'] . "</mail>";
echo "<designation>" . $strDatas[$k]['3'] . "</designation>";
echo "<salary>" . $strDatas[$k]['4'] . "</salary>";
echo "</Row>";
}
echo "</employee_details>";
$output.=$enclosed.$strDatas[$k]['0'].$enclosed.$field_termineted;
$output.=$enclosed.$strDatas[$k]['1'].$enclosed.$field_termineted;
$output.=$enclosed.$strDatas[$k]['2'].$enclosed.$field_termineted;
$output.=$enclosed.$strDatas[$k]['3'].$enclosed.$field_termineted;
$output.=$enclosed.$strDatas[$k]['4'].$enclosed.$field_termineted;
}
header("Content-Description: File Transfer");
if( $_POST["frmDownloadFiles"] =="CSV"){
header("Content-Type: application/csv");
header("Content-Disposition: attachment; filename=report".date("d_m_Y_H_i_s").".csv");
}
if( $_POST["frmDownloadFiles"] =="XLS") {
header("Content-Type: application/vnd.ms-excel");
header("Content-disposition: attachment; filename=report".date("d_m_Y_H_i_s").".xls");
}
if( $_POST["frmDownloadFiles"] =="TXT") {
header("Content-Type: application/txt");
header("Content-disposition: attachment; filename=report".date("d_m_Y_H_i_s").".txt");
}
if( $_POST["frmDownloadFiles"] =="XML") {
header("Content-Type: application/xml");
header("Content-disposition: attachment; filename=report".date("d_m_Y_H_i_s").".xml");
}
header("Content-Transfer-Encoding: binary");
header("Expires: 0");
header("Cache-Control: must-revalidate");
header("Pragma: public");
header("Content-Length: ".strlen($output));
ob_clean();
flush();
echo $output;
exit;
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Export Files</title>
<link href="css/export.css" rel="stylesheet" type="text/css"/>
</head>
<body>
<form id="frmEmployee" name="frmEmployee" enctype="multipart/form-data" method="post" action="" onsubmit="return validation();">
<div class="all">
<div class="alls">
<div class="main">
<div class="inner">
<div class="top">
<p> </p>
<div class="text" align="center">
<p class="det">DOWNLOAD FILES</p>
</div>
<p> </p>
</div>
<p> </p>
<p> </p>
<div class="nnn">
<div class="name">Download CSV file:</div>
<div class="field">
<label>
<input type="submit" name="frmDownloadFiles" id="frmDownloadFiles" value="CSV" class="subb" />
</label>
</div>
<p> </p>
</div>
<p> </p>
<div class="nnn">
<div class="name">Download excel file:</div>
<div class="field">
<label>
<input type="submit" name="frmDownloadFiles" id="frmDownloadFiles" value="XLS" class="subb" />
</label>
</div>
<p> </p>
</div>
<p> </p>
<div class="nnn">
<div class="name">Download text file:</div>
<div class="field">
<label>
<input type="submit" name="frmDownloadFiles" id="frmDownloadFiles" value="TXT" class="subb" />
</label>
</div>
<p> </p>
</div>
<p> </p>
<div class="nnn">
<div class="name">Download xml file:</div>
<div class="field">
<label>
<input type="submit" name="frmDownloadFiles" id="frmDownloadFiles" value="XML" class="subb" />
</label>
</div>
<p> </p>
</div>
</div>
<p> </p>
</div>
</div>
</div>
</form>
</body>
</html>
The first three works correctly, but xml cannot export from database. Please find my error and help me. Thanks in advance