Last active
December 25, 2018 06:22
-
-
Save ganmahmud/9424b87be5ca0681511357130c6779c7 to your computer and use it in GitHub Desktop.
Form data to CSV
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class CSVExport | |
{ | |
/** | |
* Constructor | |
*/ | |
public function __construct() | |
{ | |
if(isset($_GET['download_report'])) | |
{ | |
$csv = $this->generate_csv(); | |
header("Pragma: public"); | |
header("Expires: 0"); | |
header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); | |
header("Cache-Control: private", false); | |
header("Content-Type: application/octet-stream"); | |
header("Content-Disposition: attachment; filename=\"muestra.csv\";" ); | |
header("Content-Transfer-Encoding: binary"); | |
echo $csv; | |
exit; | |
} | |
// Add extra menu items for admins | |
add_action('admin_menu', array($this, 'admin_menu')); | |
// Create end-points | |
add_filter('query_vars', array($this, 'query_vars')); | |
add_action('parse_request', array($this, 'parse_request')); | |
} | |
/** | |
* Add extra menu items for admins | |
*/ | |
public function admin_menu() | |
{ | |
add_menu_page('Download Report', 'Download Muestra Reports', 'manage_options', 'download_report', array($this, 'download_report'),"dashicons-welcome-view-site"); | |
} | |
/** | |
* Allow for custom query variables | |
*/ | |
public function query_vars($query_vars) | |
{ | |
$query_vars[] = 'download_report'; | |
return $query_vars; | |
} | |
/** | |
* Parse the request | |
*/ | |
public function parse_request(&$wp) | |
{ | |
if(array_key_exists('download_report', $wp->query_vars)) | |
{ | |
$this->download_report(); | |
exit; | |
} | |
} | |
/** | |
* Download report | |
*/ | |
public function download_report() | |
{ | |
echo '<div class="wrap">'; | |
echo '<div id="icon-tools" class="icon32"> | |
</div>'; | |
echo '<a href='.site_url().'/wp-admin/admin.php?download_report'.' <h2>Download Muestra</h2>'; | |
//$url = site_url(); | |
} | |
/** | |
* Converting data to CSV | |
*/ | |
public function generate_csv() | |
{ | |
global $wpdb; | |
$csv_output = ''; | |
$table = 'wp-muestra'; | |
$query = "SHOW COLUMNS FROM ".$wpdb->prefix."muestra"; | |
$result = $wpdb->get_results($query); | |
$i = 0; | |
if (count($result) > 0) { | |
foreach ($result as $key => $value) { | |
$csv_output = $csv_output . $value->Field.","; | |
$csv_output .= "\n"; | |
} | |
$query = "SELECT * FROM ".$wpdb->prefix."muestra"; | |
$values = $wpdb->get_results($query); | |
if (count($values)>0) { | |
foreach ($values as $key => $value) { | |
foreach ($value as $k => $v) { | |
// echo "<pre>"; | |
// print_r($v); | |
// echo "</pre>"; | |
$csv_output .= $v.","; | |
} | |
$csv_output .= "\n"; | |
} | |
} | |
return $csv_output; | |
} | |
} | |
// Instantiate a singleton of this plugin | |
$csvExport = new CSVExport(); | |
class CSVExportParticipa | |
{ | |
/** | |
* Constructor | |
*/ | |
public function __construct() | |
{ | |
if(isset($_GET['download_participa_report'])) | |
{ | |
$csv = $this->generate_csv(); | |
header("Pragma: public"); | |
header("Expires: 0"); | |
header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); | |
header("Cache-Control: private", false); | |
header("Content-Type: application/octet-stream"); | |
header("Content-Disposition: attachment; filename=\"participa.csv\";" ); | |
header("Content-Transfer-Encoding: binary"); | |
echo $csv; | |
exit; | |
} | |
// Add extra menu items for admins | |
add_action('admin_menu', array($this, 'admin_menu')); | |
// Create end-points | |
add_filter('query_vars', array($this, 'query_vars')); | |
add_action('parse_request', array($this, 'parse_request')); | |
} | |
/** | |
* Add extra menu items for admins | |
*/ | |
public function admin_menu() | |
{ | |
add_menu_page('Download Report', 'Download Participa Reports', 'manage_options', 'download_participa_report', array($this, 'download_participa_report'),"dashicons-welcome-view-site"); | |
} | |
/** | |
* Allow for custom query variables | |
*/ | |
public function query_vars($query_vars) | |
{ | |
$query_vars[] = 'download_participa_report'; | |
return $query_vars; | |
} | |
/** | |
* Parse the request | |
*/ | |
public function parse_request(&$wp) | |
{ | |
if(array_key_exists('download_participa_report', $wp->query_vars)) | |
{ | |
$this->download_participa_report(); | |
exit; | |
} | |
} | |
/** | |
* Download report | |
*/ | |
public function download_participa_report() | |
{ | |
echo '<div class="wrap">'; | |
echo '<div id="icon-tools" class="icon32"> | |
</div>'; | |
echo '<a href='.site_url().'/wp-admin/admin.php?download_participa_report'.' <h2>Download Participa</h2>'; | |
//$url = site_url(); | |
} | |
/** | |
* Converting data to CSV | |
*/ | |
public function generate_csv() | |
{ | |
global $wpdb; | |
$csv_output = ''; | |
$table = 'wp-participa'; | |
$query = "SHOW COLUMNS FROM ".$wpdb->prefix."participa"; | |
$result = $wpdb->get_results($query); | |
$i = 0; | |
if (count($result) > 0) { | |
foreach ($result as $key => $value) { | |
// echo "<pre>"; | |
// print_r( $value->Field); | |
$csv_output = $csv_output . $value->Field.","; | |
// echo "</pre>"; | |
} | |
$csv_output .= "\n"; | |
} | |
$query = "SELECT * FROM ".$wpdb->prefix."participa"; | |
$values = $wpdb->get_results($query); | |
if (count($values)>0) { | |
foreach ($values as $key => $value) { | |
foreach ($value as $k => $v) { | |
// echo "<pre>"; | |
// print_r($v); | |
// echo "</pre>"; | |
$csv_output .= $v.","; | |
} | |
$csv_output .= "\n"; | |
} | |
} | |
return $csv_output; | |
} | |
} | |
// Instantiate a singleton of this plugin | |
$csvExport = new CSVExportParticipa(); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment