Skip to content

Instantly share code, notes, and snippets.

@ckotak
Created November 1, 2012 00:47
Show Gist options
  • Save ckotak/3990939 to your computer and use it in GitHub Desktop.
Save ckotak/3990939 to your computer and use it in GitHub Desktop.
<?php
Class CSV {
function __construct() {
$link = mysql_connect('localhost', 'role_models', 'n38sl1*k3');
$db = mysql_select_db('role_models', $link);
header("Content-type: text/csv");
header("Content-Disposition: attachment; filename=pledges.csv");
}
public function outputcsv($fields) {
$sep = "";
foreach($fields as $field) {
$field = '"'.str_replace('"', '\"', $field).'"';
$line .= $sep.$field;
$sep = ",";
}
$line .= "\r\n";
return $line;
}
public function query_pledges() {
$q = 'SELECT `pledges`.`fb_first_name` AS first_name,
`pledges`.`fb_last_name` AS last_name, `pledges`.`user_email` AS email,
CASE
WHEN `pledges`.`user_recieve_updates` = 0 THEN "no"
ELSE "yes"
END AS opt_in,
`pledges`.`add_datetime` AS date_added,
`pledges`.`complete_datetime` AS date_completed,
CASE
WHEN `pledges`.`completed` = 0 THEN "no"
ELSE "yes"
END AS completed,
FROM `pledges`
ORDER BY `pledges`.`id` ASC';
$res = mysql_query($q);
$row = mysql_fetch_assoc($res);
$line = $this->outputcsv(array_keys($row)); // column names
while($obj = mysql_fetch_object($res)) {
$line .= $this->outputcsv($obj); // rows
}
return $line;
}
}
$csv = new CSV();
echo $csv->query_pledges();
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment