-
Notifications
You must be signed in to change notification settings - Fork 138
/
Copy pathexport-to-excel.php
59 lines (44 loc) · 1.76 KB
/
export-to-excel.php
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
<?php
header('Access-Control-Allow-Origin: *');
header('Content-Type: application/json');
include_once ('../backend/config/database2.php');
include_once ('Interns.php');
//Instantiate DB and connect
$database = new Database();
$db = $database->connect();
//Instantiate Intern object
$intern = new Interns($db);
//Get Intern query
$result = $intern->read();
//get row count
$num = $result->rowCount();
//check intern
if ($num > 0){
$delimiter = ",";
$filename = "interns_" . date('Y-m-d') . ".csv";
//create a file pointer
$f = fopen('php://memory', 'w');
//set column headers
$fields = array('S/N', 'Name', 'Email', 'Phone', 'Link to Portfolio','Link to Cv','Years of Experience','Interest','Location','Employment Status', 'About');
fputcsv($f, $fields, $delimiter);
$x = 1;
while ($row = $result->fetch(PDO::FETCH_ASSOC)) {
extract($row);
//$lineData = array($row['intern_id'], $row['name'], $row['email'], $row['phone_no'], $row['link_to_portfolio'],$row['link_to_cv'],$row['years_of_experience'],$row['interest'],$row['current_location'],$row['employment_status'], $row['about']);
$lineData = array($x++, $name, $email, $phone_no, $link_to_portfolio, $link_to_cv, $years_of_experience, $interest, $current_location, $employment_status, $about);
fputcsv($f, $lineData, $delimiter);
}
//move back to beginning of file
fseek($f, 0);
//set headers to download file rather than displayed
header('Content-Type: text/csv');
header('Content-Disposition: attachment; filename="' . $filename . '";');
//output all remaining data on a file pointer
fpassthru($f);
exit;
}else{
//No post
echo json_encode(
array('message' => 'No Intern Found')
);
}