header('Content-type: application/vnd.ms-excel');
<?php
header('Content-type: application/vnd.ms-excel');
$output = '';//create variable for holding the output
$sql = $modx->db->query( 'SELECT * FROM `ebmonthlyweb_user_attributes_extended` LIMIT 0, 1000');//define the DB query, in this case the table is named 'tblprojects', add WHERE AND etc. as needed
$resultArray = $modx->db->makeArray( $sql );//put it into an array
foreach($resultArray as $item)//go through each record in the array
header("content-type: plain/text");
header("content-disposition: attachment; filename=$downloadfile");
header("content-Transfer-Encoding: binary");
header("Pragma: no-cache");
header("Expires: 0");
{//start looping through the data array
$params['gender']=$item['gender'];
$params['name']=$item['fullname'];
$params['businessname']=$item['username'];
$params['sector']=$item['business_type'];
$params['subscriber']=$item['subscriber'];//set the placeholders, the $params sets the name of the placeholder, the $item is the column name from the DB table
$params['address']=$item['address'];
$params['city']=$item['city'];
$params['county']=$item['state'];
$params['postcode']=$item['zip'];
$params['country']=$item['country'];
$params['phone']=$item['phone'];
$params['fax']=$item['fax'];
$params['email']=$item['email'];
$output.=$modx->parseChunk('databaseTpl', $params, '[+', '+]');//define the chunk name and process the content, here it's ProjectTpl which has the placeholders for the data
}//finish looping through the data array
return $output;//now return the output from the processed chunk
?><?php
header('Content-type: application/vnd.ms-excel');
header("content-type: plain/text");
header("content-disposition: attachment; filename=$downloadfile");
header("content-Transfer-Encoding: binary");
header("Pragma: no-cache");
header("Expires: 0");
$output = '';//create variable for holding the output
$sql = $modx->db->query( 'SELECT * FROM `ebmonthlyweb_user_attributes_extended` LIMIT 0, 1000');//define the DB query, in this case the table is named 'tblprojects', add WHERE AND etc. as needed
$resultArray = $modx->db->makeArray( $sql );//put it into an array
foreach($resultArray as $item)//go through each record in the array
{//start looping through the data array
$params['gender']=$item['gender'];
$params['name']=$item['fullname'];
$params['businessname']=$item['username'];
$params['sector']=$item['business_type'];
$params['subscriber']=$item['subscriber'];//set the placeholders, the $params sets the name of the placeholder, the $item is the column name from the DB table
$params['address']=$item['address'];
$params['city']=$item['city'];
$params['county']=$item['state'];
$params['postcode']=$item['zip'];
$params['country']=$item['country'];
$params['phone']=$item['phone'];
$params['fax']=$item['fax'];
$params['email']=$item['email'];
$output.=$modx->parseChunk('databaseTpl', $params, '[+', '+]');//define the chunk name and process the content, here it's ProjectTpl which has the placeholders for the data
}//finish looping through the data array
return $output;//now return the output from the processed chunk
?><?php
// MySQL table name, you choose
$table = "modx_web_user_attributes";
exportMysqlToCsv($table);
function exportMysqlToCsv($table,$filename = 'export.csv')// Output filename, you choose
{
$csv_terminated = "\n";
$csv_separator = ",";
$csv_enclosed = '"';
$csv_escaped = "\\";
$sql_query = "select * from $table";
// Gets the data from the database
$result = mysql_query($sql_query);
$fields_cnt = mysql_num_fields($result);
$schema_insert = '';
for ($i = 0; $i < $fields_cnt; $i++)
{
$l = $csv_enclosed . str_replace($csv_enclosed, $csv_escaped . $csv_enclosed,
stripslashes(mysql_field_name($result, $i))) . $csv_enclosed;
$schema_insert .= $l;
$schema_insert .= $csv_separator;
} // end for
$out = trim(substr($schema_insert, 0, -1));
$out .= $csv_terminated;
// Format the data
while ($row = mysql_fetch_array($result))
{
$schema_insert = '';
for ($j = 0; $j < $fields_cnt; $j++)
{
if ($row[$j] == '0' || $row[$j] != '')
{
if ($csv_enclosed == '')
{
$schema_insert .= $row[$j];
} else
{
$schema_insert .= $csv_enclosed .
str_replace($csv_enclosed, $csv_escaped . $csv_enclosed, $row[$j]) . $csv_enclosed;
}
} else
{
$schema_insert .= '';
}
if ($j < $fields_cnt - 1)
{
$schema_insert .= $csv_separator;
}
} // end for
$out .= $schema_insert;
$out .= $csv_terminated;
} // end while
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-Length: " . strlen($out));
// Output to browser with appropriate mime type, you choose ;)
header("Content-type: text/x-csv");
//header("Content-type: text/csv");
//header("Content-type: application/csv");
//header('Content-type: application/vnd.ms-excel');
header("Content-Disposition: attachment; filename=$filename");
echo $out;
exit;
}
?>