Location: PHPKode > projects > Clinical Handover Database > output/patients_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/patients_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("patients_export.htm");


function ExportToExcel()
{
	global $cCharset;
	header("Content-type: application/vnd.ms-excel");
	header("Content-Disposition: attachment;Filename=patients.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=patients.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=patients.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("Primary"));
		echo "<".$field.">";
		echo htmlspecialchars(GetData($row,"Primary",""));
		echo "</".$field.">\r\n";
		$field=htmlspecialchars(XMLNameEncode("Patient Name"));
		echo "<".$field.">";
		echo htmlspecialchars(GetData($row,"Patient Name",""));
		echo "</".$field.">\r\n";
		$field=htmlspecialchars(XMLNameEncode("Date of Birth"));
		echo "<".$field.">";
		echo htmlspecialchars(GetData($row,"Date of Birth",""));
		echo "</".$field.">\r\n";
		$field=htmlspecialchars(XMLNameEncode("Unit Number"));
		echo "<".$field.">";
		echo htmlspecialchars(GetData($row,"Unit Number",""));
		echo "</".$field.">\r\n";
		$field=htmlspecialchars(XMLNameEncode("Sex"));
		echo "<".$field.">";
		echo htmlspecialchars(GetData($row,"Sex",""));
		echo "</".$field.">\r\n";
		$field=htmlspecialchars(XMLNameEncode("Ward"));
		echo "<".$field.">";
		echo htmlspecialchars(GetData($row,"Ward",""));
		echo "</".$field.">\r\n";
		$field=htmlspecialchars(XMLNameEncode("Bed"));
		echo "<".$field.">";
		echo htmlspecialchars(GetData($row,"Bed",""));
		echo "</".$field.">\r\n";
		$field=htmlspecialchars(XMLNameEncode("Consultant"));
		echo "<".$field.">";
		echo htmlspecialchars(GetData($row,"Consultant",""));
		echo "</".$field.">\r\n";
		$field=htmlspecialchars(XMLNameEncode("Code"));
		echo "<".$field.">";
		echo htmlspecialchars(GetData($row,"Code",""));
		echo "</".$field.">\r\n";
		$field=htmlspecialchars(XMLNameEncode("Category"));
		echo "<".$field.">";
		echo htmlspecialchars(GetData($row,"Category",""));
		echo "</".$field.">\r\n";
		$field=htmlspecialchars(XMLNameEncode("Admission Date"));
		echo "<".$field.">";
		echo htmlspecialchars(GetData($row,"Admission Date",""));
		echo "</".$field.">\r\n";
		$field=htmlspecialchars(XMLNameEncode("Diagnosis"));
		echo "<".$field.">";
		echo htmlspecialchars(GetData($row,"Diagnosis",""));
		echo "</".$field.">\r\n";
		$field=htmlspecialchars(XMLNameEncode("Allergies"));
		echo "<".$field.">";
		echo htmlspecialchars(GetData($row,"Allergies",""));
		echo "</".$field.">\r\n";
		$field=htmlspecialchars(XMLNameEncode("Notes"));
		echo "<".$field.">";
		echo htmlspecialchars(GetData($row,"Notes",""));
		echo "</".$field.">\r\n";
		$field=htmlspecialchars(XMLNameEncode("Nurses"));
		echo "<".$field.">";
		echo htmlspecialchars(GetData($row,"Nurses",""));
		echo "</".$field.">\r\n";
		$field=htmlspecialchars(XMLNameEncode("Social Services"));
		echo "<".$field.">";
		echo htmlspecialchars(GetData($row,"Social Services",""));
		echo "</".$field.">\r\n";
		$field=htmlspecialchars(XMLNameEncode("Team"));
		echo "<".$field.">";
		echo htmlspecialchars(GetData($row,"Team",""));
		echo "</".$field.">\r\n";
		$field=htmlspecialchars(XMLNameEncode("Ward Round Notes"));
		echo "<".$field.">";
		echo htmlspecialchars(GetData($row,"Ward Round 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=patients.csv");

	if(!db_numrows($rs))
		return;

	$totals=array();

	
// write header
	$outstr="";
	if($outstr!="")
		$outstr.=",";
	$outstr.= "\"Primary\"";
	if($outstr!="")
		$outstr.=",";
	$outstr.= "\"Patient Name\"";
	if($outstr!="")
		$outstr.=",";
	$outstr.= "\"Date of Birth\"";
	if($outstr!="")
		$outstr.=",";
	$outstr.= "\"Unit Number\"";
	if($outstr!="")
		$outstr.=",";
	$outstr.= "\"Sex\"";
	if($outstr!="")
		$outstr.=",";
	$outstr.= "\"Ward\"";
	if($outstr!="")
		$outstr.=",";
	$outstr.= "\"Bed\"";
	if($outstr!="")
		$outstr.=",";
	$outstr.= "\"Consultant\"";
	if($outstr!="")
		$outstr.=",";
	$outstr.= "\"Code\"";
	if($outstr!="")
		$outstr.=",";
	$outstr.= "\"Category\"";
	if($outstr!="")
		$outstr.=",";
	$outstr.= "\"Admission Date\"";
	if($outstr!="")
		$outstr.=",";
	$outstr.= "\"Diagnosis\"";
	if($outstr!="")
		$outstr.=",";
	$outstr.= "\"Allergies\"";
	if($outstr!="")
		$outstr.=",";
	$outstr.= "\"Notes\"";
	if($outstr!="")
		$outstr.=",";
	$outstr.= "\"Nurses\"";
	if($outstr!="")
		$outstr.=",";
	$outstr.= "\"Social Services\"";
	if($outstr!="")
		$outstr.=",";
	$outstr.= "\"Team\"";
	if($outstr!="")
		$outstr.=",";
	$outstr.= "\"Ward Round 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,"Primary",$format)).'"';
		if($outstr!="")
			$outstr.=",";
			$format="";
		$outstr.='"'.htmlspecialchars(GetData($row,"Patient Name",$format)).'"';
		if($outstr!="")
			$outstr.=",";
			$format="Short Date";
		$outstr.='"'.htmlspecialchars(GetData($row,"Date of Birth",$format)).'"';
		if($outstr!="")
			$outstr.=",";
			$format="";
		$outstr.='"'.htmlspecialchars(GetData($row,"Unit Number",$format)).'"';
		if($outstr!="")
			$outstr.=",";
			$format="";
		$outstr.='"'.htmlspecialchars(GetData($row,"Sex",$format)).'"';
		if($outstr!="")
			$outstr.=",";
			$format="";
		$outstr.='"'.htmlspecialchars(GetData($row,"Ward",$format)).'"';
		if($outstr!="")
			$outstr.=",";
			$format="";
		$outstr.='"'.htmlspecialchars(GetData($row,"Bed",$format)).'"';
		if($outstr!="")
			$outstr.=",";
			$format="";
		$outstr.='"'.htmlspecialchars(GetData($row,"Consultant",$format)).'"';
		if($outstr!="")
			$outstr.=",";
			$format="";
		$outstr.='"'.htmlspecialchars(GetData($row,"Code",$format)).'"';
		if($outstr!="")
			$outstr.=",";
			$format="";
		$outstr.='"'.htmlspecialchars(GetData($row,"Category",$format)).'"';
		if($outstr!="")
			$outstr.=",";
			$format="Short Date";
		$outstr.='"'.htmlspecialchars(GetData($row,"Admission Date",$format)).'"';
		if($outstr!="")
			$outstr.=",";
			$format="";
		$outstr.='"'.htmlspecialchars(GetData($row,"Diagnosis",$format)).'"';
		if($outstr!="")
			$outstr.=",";
			$format="";
		$outstr.='"'.htmlspecialchars(GetData($row,"Allergies",$format)).'"';
		if($outstr!="")
			$outstr.=",";
			$format="";
		$outstr.='"'.htmlspecialchars(GetData($row,"Notes",$format)).'"';
		if($outstr!="")
			$outstr.=",";
			$format="";
		$outstr.='"'.htmlspecialchars(GetData($row,"Nurses",$format)).'"';
		if($outstr!="")
			$outstr.=",";
			$format="";
		$outstr.='"'.htmlspecialchars(GetData($row,"Social Services",$format)).'"';
		if($outstr!="")
			$outstr.=",";
			$format="";
		$outstr.='"'.htmlspecialchars(GetData($row,"Team",$format)).'"';
		if($outstr!="")
			$outstr.=",";
			$format="";
		$outstr.='"'.htmlspecialchars(GetData($row,"Ward Round 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("Primary").'</td>';
		echo '<td style="width: 100" x:str>'.PrepareForExcel("Patient Name").'</td>';
		echo '<td style="width: 100" x:str>'.PrepareForExcel("Date of Birth").'</td>';
		echo '<td style="width: 100" x:str>'.PrepareForExcel("Unit Number").'</td>';
		echo '<td style="width: 100" x:str>'.PrepareForExcel("Sex").'</td>';
		echo '<td style="width: 100" x:str>'.PrepareForExcel("Ward").'</td>';
		echo '<td style="width: 100" x:str>'.PrepareForExcel("Bed").'</td>';
		echo '<td style="width: 100" x:str>'.PrepareForExcel("Consultant").'</td>';
		echo '<td style="width: 100" x:str>'.PrepareForExcel("Code").'</td>';
		echo '<td style="width: 100" x:str>'.PrepareForExcel("Category").'</td>';
		echo '<td style="width: 100" x:str>'.PrepareForExcel("Admission Date").'</td>';
		echo '<td style="width: 100" x:str>'.PrepareForExcel("Diagnosis").'</td>';
		echo '<td style="width: 100" x:str>'.PrepareForExcel("Allergies").'</td>';
		echo '<td style="width: 100" x:str>'.PrepareForExcel("Notes").'</td>';
		echo '<td style="width: 100" x:str>'.PrepareForExcel("Nurses").'</td>';
		echo '<td style="width: 100" x:str>'.PrepareForExcel("Social Services").'</td>';
		echo '<td style="width: 100" x:str>'.PrepareForExcel("Team").'</td>';
		echo '<td style="width: 100" x:str>'.PrepareForExcel("Ward Round Notes").'</td>';
	}
	else
	{
		echo "<td>Primary</td>";
		echo "<td>Patient Name</td>";
		echo "<td>Date of Birth</td>";
		echo "<td>Unit Number</td>";
		echo "<td>Sex</td>";
		echo "<td>Ward</td>";
		echo "<td>Bed</td>";
		echo "<td>Consultant</td>";
		echo "<td>Code</td>";
		echo "<td>Category</td>";
		echo "<td>Admission Date</td>";
		echo "<td>Diagnosis</td>";
		echo "<td>Allergies</td>";
		echo "<td>Notes</td>";
		echo "<td>Nurses</td>";
		echo "<td>Social Services</td>";
		echo "<td>Team</td>";
		echo "<td>Ward Round Notes</td>";
	}
	echo "</tr>";

	$totals=array();
// write data rows
	$iNumberOfRows = 0;
	while((!$nPageSize || $iNumberOfRows<$nPageSize) && $row=db_fetch_array($rs))
	{
		echo "<tr>";
	echo '<td>';

		$format="";
			echo htmlspecialchars(GetData($row,"Primary",$format));
	echo '</td>';
	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>';
	echo '<td>';

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

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

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

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

		$format="";
			if($_REQUEST["type"]=="excel")
			echo PrepareForExcel(GetData($row,"Bed",$format));
		else
			echo htmlspecialchars(GetData($row,"Bed",$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>';
	if($_REQUEST["type"]=="excel")
		echo '<td x:str>';
	else
		echo '<td>';

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

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

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

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

		$format="";
			if($_REQUEST["type"]=="excel")
			echo PrepareForExcel(GetData($row,"Allergies",$format));
		else
			echo htmlspecialchars(GetData($row,"Allergies",$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>';
	if($_REQUEST["type"]=="excel")
		echo '<td x:str>';
	else
		echo '<td>';

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

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

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

		$format="";
			if($_REQUEST["type"]=="excel")
			echo PrepareForExcel(GetData($row,"Ward Round Notes",$format));
		else
			echo htmlspecialchars(GetData($row,"Ward Round 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