<?PHP
include_once("config.php");
$ok=role_check();
if (!$ok){
header("Location: login.php");
}
$sql=" select logonid, name,rolename,deptdesc,unitdesc from erpuser,erprole,erpunit,erpdept where erpuser.unitid=erpunit.unitid and erpuser.deptid=erpdept.deptid and erpuser.roleid=erprole.roleid order by logonid";
$result = mysql_query($sql) or die(mysql_error());
// Put the name of all fields
$out = "Logonid";
$out .= ",";
$out .= "Name";
$out .= ",";
$out .= "Role";
$out .= ",";
$out .= "Department";
$out .= ",";
$out .= "Unit";
$out .="\n";
// Add all values in the table
while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
$out .= trim("$row[0]") ;
$out .= ",";
$out .= trim("$row[1]");
$out .= ",";
$out .= trim("$row[2]");
$out .= ",";
$out .= trim("$row[3]");
$out .= ",";
$out .= trim("$row[4]");
$out .="\n";
}
// Output to browser
header('Content-type: text/richtext');
header("Content-Disposition: attachment; filename=users_exported.csv");
echo $out;