forked from joshiket/citilAutomation
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcertification2Excel.php
More file actions
71 lines (70 loc) · 2.57 KB
/
certification2Excel.php
File metadata and controls
71 lines (70 loc) · 2.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
<?php
function cleanData(&$str)
{
$str = preg_replace("/\t/", "\\t", $str);
$str = preg_replace("/\r?\n/", "\\n", $str);
if(strstr($str, '"')) $str = '"' . str_replace('"', '""', $str) . '"';
}
function generateResponse($error, $msg, $data)
{
$msgArr['error'] = $error;
if($data)
$msgArr['data'] = $msg;
else
$msgArr['msg'] = $msg;
return json_encode($msgArr);
} // generateResponse()
function getAllCertifications()
{
try
{
$query = "SELECT cProfName, cprovName, certiExam , certiExamDesc, certifiedOn, validTill, cerytiExpires, Expired FROM wdbt.vwcertifications";
$config = array( PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::ATTR_EMULATE_PREPARES => false);
$con = new PDO("mysql:host=localhost; dbname = wdbt;charset=utf8", "root" ,"root" ,$config);
$stmt = $con->prepare($query);
$stmt->execute();
if($stmt->rowCount()>0)
{
$result = $stmt->fetchAll(PDO::FETCH_ASSOC);
$msg = generateResponse(FALSE, json_encode($result),TRUE);
return $msg;
}
else
{
$msg = generateResponse(TRUE, "No records found.", FALSE);
//return $msg;
echo "No records found.";
}
}
catch(PDOException $e)
{
$msg = generateResponse(TRUE, $e->getMessage(),TRUE);
return $msg;
}//catch
}
$msg = getAllCertifications();
$certifications = json_decode($msg);
//print_r($certifications);
if(is_object($certifications))
{
if(!$certifications->error)
{
$certifications = $certifications->data;
$certifications = json_decode($certifications,TRUE);
//print_r($certifications);
$filename = "certification_data" . ".xls";
header("Content-Disposition: attachment; filename=\"$filename\"");
header("Content-Type: application/vnd.ms-excel");
$flag = false; // to render headers
foreach($certifications as $row) {
if(!$flag) {
// display field/column names as first row
echo implode("\t", array_keys($row)) . "\r\n";
$flag = true;
}
array_walk($row, __NAMESPACE__ . '\cleanData');
echo implode("\t", array_values($row)) . "\r\n";
}
}
}
?>