Location: PHPKode > projects > Clinical Handover Database > output/jobs_export.php
<?php 
ini_set("display_errors","1");
ini_set("display_startup_errors","1");
session_cache_limiter("none");
set_magic_quotes_runtime(0);

include("include/dbcommon.php");
include("include/jobs_variables.php");

if(!@$_SESSION["UserID"])
{ 
	$_SESSION["MyURL"]=$_SERVER["SCRIPT_NAME"]."?".$_SERVER["QUERY_STRING"];
	header("Location: login.php?message=expired"); 
	return;
}
if(!CheckSecurity(@$_SESSION["OwnerID"],"Export"))
{
	echo "<p>"."You don't have permissions to access this table"."<a href=\"login.php\">"."Back to login page"."</a></p>";
	return;
}


$mypage=1;
if(@$_REQUEST["type"]) 
{
	$conn=db_connect();
	$strSQL=$_SESSION[$strTableName."_sql"];
	LogInfo($strSQL);
//	pagination
	$nPageSize=0;
	$numrows=GetRowCount($strSQL);
	if(@$_REQUEST["records"]=="page" && $numrows)
	{
		$mypage=(integer)@$_SESSION[$strTableName."_pagenumber"];
		$nPageSize=(integer)@$_SESSION[$strTableName."_pagesize"];
		if($numrows<=($mypage-1)*$nPageSize)
			$mypage=ceil($numrows/$nPageSize);
		if(!$nPageSize)
			$nPageSize=$gPageSize;
		if(!$mypage)
			$mypage=1;

		$strSQL.=" limit ".(($mypage-1)*$nPageSize).",".$nPageSize;
	}
	$rs=db_query($strSQL,$conn);

	if(!ini_get("safe_mode"))
		set_time_limit(300);
	
	if(@$_REQUEST["type"]=="excel")
		ExportToExcel();
	else if(@$_REQUEST["type"]=="word")
		ExportToWord();
	else if(@$_REQUEST["type"]=="xml")
		ExportToXML();
	else if(@$_REQUEST["type"]=="csv")
		ExportToCSV();

	db_close($conn);
	return;
}

header("Expires: Thu, 01 Jan 1970 00:00:01 GMT"); 

include('libs/Smarty.class.php');
$smarty = new Smarty();
$smarty->display("jobs_export.htm");


function ExportToExcel()
{
	global $cCharset;
	header("Content-type: application/vnd.ms-excel");
	header("Content-Disposition: attachment;Filename=jobs.xls");

	echo "<html>";
	echo "<html xmlns:o=\"urn:schemas-microsoft-com:office:office\" xmlns:x=\"urn:schemas-microsoft-com:office:excel\" xmlns=\"http://www.w3.org/TR/REC-html40\">";
	
	echo "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=".$cCharset."\">";
	echo "<body>";
	echo "<table border=1>";

	WriteTableData();

	echo "</table>";
	echo "</body>";
	echo "</html>";
}


function ExportToWord()
{
	global $cCharset;
	header("Content-type: application/vnd.ms-word");
	header("Content-Disposition: attachment;Filename=jobs.doc");

	echo "<html>";
	echo "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=".$cCharset."\">";
	echo "<body>";
	echo "<table border=1>";

	WriteTableData();

	echo "</table>";
	echo "</body>";
	echo "</html>";
}

function ExportToXML()
{
	global $nPageSize,$rs,$strTableName,$conn;
	header("Content-type: text/xml");
	header("Content-Disposition: attachment;Filename=jobs.xml");
	if(!db_numrows($rs))
		return;
	echo "<?xml version=\"1.0\" standalone=\"yes\"?>\r\n";
	echo "<table>\r\n";
	$i=0;
	while((!$nPageSize || $i<$nPageSize) && $row=db_fetch_array($rs))
	{
		echo "<row>\r\n";
		$field=htmlspecialchars(XMLNameEncode("Patient Name"));
		echo "<".$field.">";
		echo htmlspecialchars(GetData($row,"Patient Name",""));
		echo "</".$field.">\r\n";
		$field=htmlspecialchars(XMLNameEncode("Consultant"));
		echo "<".$field.">";
		echo htmlspecialchars(GetData($row,"Consultant",""));
		echo "</".$field.">\r\n";
		$field=htmlspecialchars(XMLNameEncode("Date"));
		echo "<".$field.">";
		echo htmlspecialchars(GetData($row,"Date",""));
		echo "</".$field.">\r\n";
		$field=htmlspecialchars(XMLNameEncode("Type"));
		echo "<".$field.">";
		echo htmlspecialchars(GetData($row,"Type",""));
		echo "</".$field.">\r\n";
		$field=htmlspecialchars(XMLNameEncode("Status"));
		echo "<".$field.">";
		echo htmlspecialchars(GetData($row,"Status",""));
		echo "</".$field.">\r\n";
		$field=htmlspecialchars(XMLNameEncode("Name"));
		echo "<".$field.">";
		echo htmlspecialchars(GetData($row,"Name",""));
		echo "</".$field.">\r\n";
		$field=htmlspecialchars(XMLNameEncode("Result"));
		echo "<".$field.">";
		echo htmlspecialchars(GetData($row,"Result",""));
		echo "</".$field.">\r\n";
		$field=htmlspecialchars(XMLNameEncode("Notes"));
		echo "<".$field.">";
		echo htmlspecialchars(GetData($row,"Notes",""));
		echo "</".$field.">\r\n";
		echo "</row>\r\n";
		$i++;
	}
	echo "</table>\r\n";
}

function ExportToCSV()
{
	global $rs,$nPageSize,$strTableName,$conn;
	header("Content-type: application/csv");
	header("Content-Disposition: attachment;Filename=jobs.csv");

	if(!db_numrows($rs))
		return;

	$totals=array();

	
// write header
	$outstr="";
	if($outstr!="")
		$outstr.=",";
	$outstr.= "\"Patient Name\"";
	if($outstr!="")
		$outstr.=",";
	$outstr.= "\"Consultant\"";
	if($outstr!="")
		$outstr.=",";
	$outstr.= "\"Date\"";
	if($outstr!="")
		$outstr.=",";
	$outstr.= "\"Type\"";
	if($outstr!="")
		$outstr.=",";
	$outstr.= "\"Status\"";
	if($outstr!="")
		$outstr.=",";
	$outstr.= "\"Name\"";
	if($outstr!="")
		$outstr.=",";
	$outstr.= "\"Result\"";
	if($outstr!="")
		$outstr.=",";
	$outstr.= "\"Notes\"";
	echo $outstr;
	echo "\r\n";

// write data rows
	$iNumberOfRows = 0;
	while((!$nPageSize || $iNumberOfRows<$nPageSize) && $row=db_fetch_array($rs))
	{
		$outstr="";
		if($outstr!="")
			$outstr.=",";
			$format="";
		$outstr.='"'.htmlspecialchars(GetData($row,"Patient Name",$format)).'"';
		if($outstr!="")
			$outstr.=",";
			$format="";
		$outstr.='"'.htmlspecialchars(GetData($row,"Consultant",$format)).'"';
		if($outstr!="")
			$outstr.=",";
			$format="Short Date";
		$outstr.='"'.htmlspecialchars(GetData($row,"Date",$format)).'"';
		if($outstr!="")
			$outstr.=",";
			$format="";
		$outstr.='"'.htmlspecialchars(GetData($row,"Type",$format)).'"';
		if($outstr!="")
			$outstr.=",";
			$format="";
		$outstr.='"'.htmlspecialchars(GetData($row,"Status",$format)).'"';
		if($outstr!="")
			$outstr.=",";
			$format="";
		$outstr.='"'.htmlspecialchars(GetData($row,"Name",$format)).'"';
		if($outstr!="")
			$outstr.=",";
			$format="";
		$outstr.='"'.htmlspecialchars(GetData($row,"Result",$format)).'"';
		if($outstr!="")
			$outstr.=",";
			$format="";
		$outstr.='"'.htmlspecialchars(GetData($row,"Notes",$format)).'"';
		echo $outstr;
		echo "\r\n";
		$iNumberOfRows++;
	}

//	display totals
	$first=true;

}


function WriteTableData()
{
	global $rs,$nPageSize,$strTableName,$conn;
	if(!db_numrows($rs))
		return;
// write header
	echo "<tr>";
	if($_REQUEST["type"]=="excel")
	{
		echo '<td style="width: 100" x:str>'.PrepareForExcel("Patient Name").'</td>';
		echo '<td style="width: 100" x:str>'.PrepareForExcel("Consultant").'</td>';
		echo '<td style="width: 100" x:str>'.PrepareForExcel("Date").'</td>';
		echo '<td style="width: 100" x:str>'.PrepareForExcel("Type").'</td>';
		echo '<td style="width: 100" x:str>'.PrepareForExcel("Status").'</td>';
		echo '<td style="width: 100" x:str>'.PrepareForExcel("Name").'</td>';
		echo '<td style="width: 100" x:str>'.PrepareForExcel("Result").'</td>';
		echo '<td style="width: 100" x:str>'.PrepareForExcel("Notes").'</td>';
	}
	else
	{
		echo "<td>Patient Name</td>";
		echo "<td>Consultant</td>";
		echo "<td>Date</td>";
		echo "<td>Type</td>";
		echo "<td>Status</td>";
		echo "<td>Name</td>";
		echo "<td>Result</td>";
		echo "<td>Notes</td>";
	}
	echo "</tr>";

	$totals=array();
// write data rows
	$iNumberOfRows = 0;
	while((!$nPageSize || $iNumberOfRows<$nPageSize) && $row=db_fetch_array($rs))
	{
		echo "<tr>";
	if($_REQUEST["type"]=="excel")
		echo '<td x:str>';
	else
		echo '<td>';

		$format="";
			if($_REQUEST["type"]=="excel")
			echo PrepareForExcel(GetData($row,"Patient Name",$format));
		else
			echo htmlspecialchars(GetData($row,"Patient Name",$format));
	echo '</td>';
	if($_REQUEST["type"]=="excel")
		echo '<td x:str>';
	else
		echo '<td>';

		$format="";
			if($_REQUEST["type"]=="excel")
			echo PrepareForExcel(GetData($row,"Consultant",$format));
		else
			echo htmlspecialchars(GetData($row,"Consultant",$format));
	echo '</td>';
	echo '<td>';

		$format="Short Date";
			if($_REQUEST["type"]=="excel")
			echo PrepareForExcel(GetData($row,"Date",$format));
		else
			echo htmlspecialchars(GetData($row,"Date",$format));
	echo '</td>';
	if($_REQUEST["type"]=="excel")
		echo '<td x:str>';
	else
		echo '<td>';

		$format="";
			if($_REQUEST["type"]=="excel")
			echo PrepareForExcel(GetData($row,"Type",$format));
		else
			echo htmlspecialchars(GetData($row,"Type",$format));
	echo '</td>';
	if($_REQUEST["type"]=="excel")
		echo '<td x:str>';
	else
		echo '<td>';

		$format="";
			if($_REQUEST["type"]=="excel")
			echo PrepareForExcel(GetData($row,"Status",$format));
		else
			echo htmlspecialchars(GetData($row,"Status",$format));
	echo '</td>';
	if($_REQUEST["type"]=="excel")
		echo '<td x:str>';
	else
		echo '<td>';

		$format="";
			if($_REQUEST["type"]=="excel")
			echo PrepareForExcel(GetData($row,"Name",$format));
		else
			echo htmlspecialchars(GetData($row,"Name",$format));
	echo '</td>';
	if($_REQUEST["type"]=="excel")
		echo '<td x:str>';
	else
		echo '<td>';

		$format="";
			if($_REQUEST["type"]=="excel")
			echo PrepareForExcel(GetData($row,"Result",$format));
		else
			echo htmlspecialchars(GetData($row,"Result",$format));
	echo '</td>';
	if($_REQUEST["type"]=="excel")
		echo '<td x:str>';
	else
		echo '<td>';

		$format="";
			if($_REQUEST["type"]=="excel")
			echo PrepareForExcel(GetData($row,"Notes",$format));
		else
			echo htmlspecialchars(GetData($row,"Notes",$format));
	echo '</td>';
		echo "</tr>";
		$iNumberOfRows++;
	}

}

function XMLNameEncode($strValue)
{	
	$search=array(" ","#","'","/","\\","(",")",",","[","]","+","\"","-","_","|","}","{","=");
	return str_replace($search,"",$strValue);
}

function PrepareForExcel($str)
{
	$ret = htmlspecialchars($str);
	if (substr($ret,0,1)== "=") 
		$ret = "&#61;".substr($ret,1);
	return $ret;

}
?>
Return current item: Clinical Handover Database