Location: PHPKode > scripts > phpRetrieve > phpRetrieve/exportXML.php
<?php
session_start();

	set_time_limit(60*30);

	//Get config
	include('config.php');

	//connect to the database
	mysql_connect('localhost',$user,$password);
	@mysql_select_db($database) or die( "Unable to select database");
	
	$tbl = $_SESSION['session_Table'];
	$fld_result = mysql_query("SHOW COLUMNS FROM ".$tbl);	
	
	if(isset($_SESSION['session_Criteria'])){
		$arr = $_SESSION['session_Criteria'];
	}
	
	Include('getMySQL.php');
	
	$myQuery .= " Limit 0, $maxOutputs";
	$result = mysql_query($myQuery) or die (header('Location: index.php?view=SQL&msg=Error in query '.mysql_error())); 
	$num=mysql_numrows($result);

	//xml layout
	$Xcols = mysql_num_fields($result);
	$Xrows = $num;
	include('xml_layout.php');
	
	//set report name
	$file = "Output/".date("Y_m_d_H_i_s").".xml";
		
	//Get Field Names
	$fieldNames="<Row ss:Index=\"6\">\n";
	$j = 0;
	while ($j < mysql_num_fields($result)) {
		$meta = mysql_fetch_field($result, $j);
		if (!$meta) {
			echo "No column headings found<br />\n";
			exit;
		}
	
		$fieldNames .= "<Cell ss:StyleID=\"s21\"><Data ss:Type=\"String\">".$meta->name."</Data><NamedCell ss:Name=\"_FilterDatabase\"/></Cell>\n";
		$j++;
	}

	$fieldNames .= "</Row>\n";
	
	//Open file and output header
	if(isset($Title)){
	}
	else {
		$Title = "XML Extract";
	}
	$bookHeader = bookHeader('phpRetrieve');
	$sheetHeader = sheetHeader(1, $Xrows, $Xcols);
	$sheetTitle = titles($Title);
	//$header = $bookHeader.$sheetHeader.$sheetTitle;
	$header = $bookHeader.$sheetHeader;
	$f = fopen($file, "w"); 
	fwrite($f, $header); 
	
	//Set column widths
	$i=1;
	$allColWidths = "";
	while($i <= $Xcols){
		$allColWidths = $allColWidths."<Column ss:Width=\"".$dfColWidth."\"/>\n";
		$i++;
	}
	fwrite($f, $allColWidths);
	fwrite($f, $sheetTitle);
	fwrite($f, $fieldNames);  

	//Now Get data
	$i = 0;
	while ($i < $num){
		
		$vals = "<Row>\n";
		//Loop field names
		$j = 0;
		while ($j < mysql_num_fields($result)) {
			$meta = mysql_fetch_field($result, $j);
			$field = $meta->name;
			$vl = mysql_result($result,$i,$field);
			$stl = "Default";
			$tp = "String";
			$vlExists = True;
			if($meta->type == "int"){ $tp = "Number"; }
			if($meta->type == "date"){
				$tp = "DateTime";
				if($vl<"1899-12-31"){ //Check if null
					$vlExists = False;
					$stl="s22";
				}
				else { //Else send date and time
					$vl = $vl."T00:00:00.000";
					$stl="s22"; 
					$tp="DateTime";
				}
			}
			if($meta->type == "time"){
				$vl = "1899-12-31T".$vl.".000";
				$stl="s23"; 
				$tp="DateTime";
			}
			
			if($vlExists == False){  //Check for nulls
				$vals .= "<Cell ss:StyleID=\"$stl\"><NamedCell ss:Name=\"_FilterDatabase\"/></Cell>\n";
			}
			else {
				$vals .= "<Cell ss:StyleID=\"$stl\"><Data ss:Type=\"$tp\">".$vl."</Data><NamedCell ss:Name=\"_FilterDatabase\"/></Cell>\n";
			}
			
			$j++;
		}

		$vals = $vals."</Row>\n";
		fwrite($f, $vals);
		$i++;
	}

	$sheetFooter = sheetFooter($dfUser, $Title, $dfOrientation, $Xrows, $Xcols );
	fwrite($f, $sheetFooter);
	fwrite($f, "</Workbook>");
	fclose($f); 

	if(isset($zipExports)){
		if($zipExports == "Y"){
			
			Include('zipIt.php');
			
			$newFile = zipIt($file);
			if($newFile != $file){
				$file = $newFile;
			}
		}
	}
	
	//Clean up Output folder
	$path = 'Output/';
	if ($handle = opendir($path)) {
		while (false !== ($filex = readdir($handle))) {
			if ((time()-filectime($path.$filex)) > 86400) {  
				@unlink($path.$filex);
			}
		}
	}
	
	header("Content-type: application/octet-stream");
	header("Content-Disposition: attachment; filename=".$file);
	readfile($file);
	
?>
Return current item: phpRetrieve