Export query to excel file

Sorry yes excel instead of exe :blush:

I do have a problem though when outputting to excel using the code below, as I have a few fields that are ID’s in the database but what I need is the name associated with that value to show instead of the ID from a table called Group.

So in the code below where can I change the value of a field by using its value which is an ID to then go to that table and instead show the name associated with the value, before printing it all out in the excel file.

$query = $_SESSION['limitClauseAdv'];
function cleanData(&$str)
{
if($str == 't') $str = 'TRUE';
if($str == 'f') $str = 'FALSE';
if(preg_match("/^0/", $str) || preg_match("/^\+?\d{8,}$/", $str) || preg_match("/^\d{4}.\d{1,2}.\d{1,2}/", $str)) {
  $str = "'$str";
}
if(strstr($str, '"')) $str = '"' . str_replace('"', '""', $str) . '"';
$str = mb_convert_encoding($str, 'UTF-16LE', 'UTF-8');
}

// filename for download
$filename = "website_data_" . date('Ymd') . ".csv";

header("Content-Disposition: attachment; filename=\"$filename\"");
header("Content-Type: text/csv; charset=UTF-16LE");

$out = fopen("php://output", 'w');

$flag = false;

$result = sqlsrv_query($conn, $query) or die('Query failed!');
while(false !== ($row = sqlsrv_fetch_array($result, SQLSRV_FETCH_ASSOC))) {

 if(!$flag) {
  // display field/column names as first row
  fputcsv($out, array_keys($row), ',', '"');
  $flag = true;
 }
 //array_walk($row, 'cleanData');
//start
foreach ($row as $key => $value)
	{ 
 if ($value instanceof DateTime) 
	{ 
 $row[$key] = date_format($value, 'd/m/y'); 
	}
  }
//end
$row = str_replace(",", ",", $row);
$row = str_replace(".", ".", $row);
 fputcsv($out, array_values($row), ',', '"');
 }

fclose($out);
exit;