Skip to content

Instantly share code, notes, and snippets.

@ano
Last active May 16, 2020 21:32
Show Gist options
  • Select an option

  • Save ano/55226f999bab0366bac0e8ec54a90a07 to your computer and use it in GitHub Desktop.

Select an option

Save ano/55226f999bab0366bac0e8ec54a90a07 to your computer and use it in GitHub Desktop.
Generates SQL code for Formidable Form Entries
<?php
/**
* The Formidable-SQL plugin
*
* Generates SQL code for Formidable Form Entries.
*
* @link ano.tisam.com
* @since 1.0.0
* @package Formidable_Sql
*
* @wordpress-plugin
* Plugin Name: Formidable-SQL
* Plugin URI: https://whupi.com
* Description: Generates SQL code for Formidable Form Entries
* Version: 1.0.0
* Author: Anonga Tisam
* Author URI: ano.tisam.com
* License: GPL-2.0+
* License URI: http://www.gnu.org/licenses/gpl-2.0.txt
* Text Domain: formidable-sql
* Domain Path: /languages
*/
/**
* Generate SQL Query for Formidable form Entries
*/
function generate_sql($fields){
$sql = "\tSELECT \n\t\twp_frm_items.id AS id";
$in = array();
$i = 0;
//For each field, write the code to generate the part of the query that selects the forms column value and name
foreach($fields as $field){
$field_ids[$i] = $field->id;
$name = preg_replace("/[^a-zA-Z0-9]+/", "", $field->name);
$sql .= ",
MAX(
IF(
wp_frm_item_metas.field_id = {$field->id},
wp_frm_item_metas.meta_value,
''
)
) {$name}";
$i++;
}
$mydata = 'a:2:{i:0;s:2:"18";i:1;s:2:"19";}';
$mydata = unserialize($mydata);
var_dump($mydata);
var_dump($field_ids);
//write the code to generate the SQL to generate the part of the WHERE part of the query
$in = implode(",",$field_ids);
$sql .= "
FROM
wp_frm_items,
wp_frm_item_metas
WHERE wp_frm_items.id = wp_frm_item_metas.item_id
AND wp_frm_item_metas.field_id IN ({$in})
GROUP BY wp_frm_items.id";
return $sql;
}
function print_sql($sql){
echo "<textarea id='sql_code' class='sql_code' style='
width: 600px;
height: 800px;
padding-bottom: 20px;
margin-bottom: 100px;
background-color: #222;
color: lightgreen;
font-family: monospace;
padding: 2em 0;
margin: 0 16em;
'>{$sql}</textarea>";
}
function generate_formidable_entries_sql() {
global $wpdb;
$form_id = (isset($_GET['id'])) ? (int) $_GET['id'] : (int) $_GET['form'];
$fields = $wpdb->get_results("SELECT * FROM `wp_frm_fields` WHERE `form_id` = {$form_id}");
if(isset($_GET['form']) && !($_GET['frm_action'] === 'edit')){
$sql = generate_sql($fields);
print_sql($sql);
}
}
add_action( 'admin_footer', 'generate_formidable_entries_sql' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment