FacetWP template for generating csv of result set

mgibbs189

{{gist file=”download-member-report.php” lang=”php”}}
<?php
/*
/*
Plugin Name: Download member report as csv
Plugin URI: http://
Description: This plugin is called when the ‘download csv’ button is clicked on the Member Reports page containing FacetWP search
Version: 1.0
Author: clare@greenbee-web.com
Author URI: http://www.greenbee-web.com/
License: GPL2
License URI: https://www.gnu.org/licenses/gpl-2.0.html
Text Domain: wporg
Domain Path: /languages

Called from form submit in FacetWP template

All it does it write a csv directly to the browser for download.
The csv data is passed from the FacetWP template “Member reporting”, containing member-reporting-loop.php

*/
if($_POST){
if(isset($_POST[‘member_csv_data’])){

header(‘Content-Type: application/excel’);
header(‘Content-Disposition: attachment; filename=”upmasanational-member-data.csv”‘);

$data = unserialize($_POST[‘member_csv_data’]);
write_csv($data);

}else{
return;
}

}else{

return;
}

function write_csv($data){

$out = fopen(‘php://output’, ‘w’);
foreach ( $data as $line ) {
fputcsv($out, $line);
}

fclose($out);

}

?>

{{/gist}}{{gist file=”member-reporting-loop.php” lang=”php”}}
<?php
$fields = array(“first_name”, “last_name”, “batch_year”, “state”, “member_type”, “member_status”, “paid_year”, “paid_amount”, “chapter_name”, “speciality”);
$csv_data = array();
$column_headers = array(“First name”,”Last name”,”Class”,”State”,”Member type”,”Member status”,”Paid year”,”Paid amount”,”Chapter”,”Speciality”,”Address”,”Cell phone”,”Home phone”,”Office phone”,”Email”);
array_push($csv_data,$column_headers);
?>
<table class=”member-table” style=”width:90%;”>
<tr><th>First name</th><th>Last name</th><th>Class</th><th>State</th><th>Member type</th><th>Member status</th><th>Paid year</th><th>Paid amount</th><th>Chapter</th><th>Speciality</th></tr>
<?php while ( have_posts() ): the_post(); ?>
<?php if( get_the_title()):

$address = get_post_meta( get_the_ID(), ‘address’, true);
$city = get_post_meta( get_the_ID(), ‘city’, true);
if($city != ”){
$address .= ” “. $city.”,”;
}
$address .= ” “. get_post_meta( get_the_ID(), ‘state’, true);
$address .= ” “. get_post_meta( get_the_ID(), ‘postal_code’, true);

$home_phone = get_post_meta( get_the_ID(), ‘home_phone’, true);
if ($home_phone){
$home_phone = ” | Home: “.$home_phone;
}
$cell_phone = get_post_meta( get_the_ID(), ‘cell_phone’, true);
if ($cell_phone){
$cell_phone = ” | Cell: “.$cell_phone;
}
$office_phone = get_post_meta( get_the_ID(), ‘office_phone’, true);
if ($office_phone){
$office_phone = ” | Office: “.$office_phone;
}

$email = get_post_meta( get_the_ID(), ’email’, true);
if ($email){
$email = ‘ | <a href=”mailto:’.$email.'”>’.$email.'</a>’;
}

$contact_details_link = ‘<a href=”#” class=”show-member-details-link”>▷ </a>’;
$contact_details_text = $address.$cell_phone.$home_phone.$office_phone.$email;
?>

<tr>
<?php
$csv_row = array();
foreach ($fields as $key => $value){
$field = get_post_meta( get_the_ID(), $value, true);

if($key == ‘0’){ //toggle link goes in first column
if(strlen(trim($contact_details_text)) != 0){
echo “<td>”.$contact_details_link.” “.$field.”</td>”; //show the toggle link if contact details exist
}else{
echo “<td style=’padding-left:20px;’>”.$field.”</td>”; //else don’t show it
}
}else{ //normal column
echo “<td>”.$field.”</td>”; //else don’t show it
}

array_push($csv_row,$field); //put this field in the csv row
}
?>

</tr>
<tr class=”member-details” style=”display:none;”>
<td colspan=”10″ style=’padding-left:20px;’>
<?php echo $address; ?> <?php echo $cell_phone; ?> <?php echo $home_phone; ?> <?php echo $office_phone; ?> <?php echo $email ?>
</td>
</tr>

<?php
array_push($csv_row, $address, $cell_phone, $home_phone, $office_phone, $email); //add contact details to csv row
array_push($csv_data,$csv_row); //put this row in the csv set

?>
<?php endif; ?>
<?php endwhile; ?>
</table>

<?php $csv_download_script = plugins_url() . ‘/download-upmasa-member-report-csv/download-upmasa-member-report-csv.php’;?>

<p>Download a csv spreadsheet of these results:
<form method=”post” action=”<?php echo $csv_download_script; ?>”>
<input type=”hidden” name=”member_csv_data” value='<?php echo serialize($csv_data); ?>’>
<button type=”submit”>Download csv</button>
</form>
</p>
{{/gist}}