Skip to content

Instantly share code, notes, and snippets.

@Shelob9

Shelob9/caldera-total.php

Last active Oct 4, 2017
Embed
What would you like to do?
<?php
/**
* Get total value of all entries in a Caldera Forms field by ID
*/
global $wpdb;
$table = $wpdb->prefix . 'cf_form_entry_values';
//IMPORTANT - change field ID to your field's ID
$r = $wpdb->get_results( "SELECT SUM( `value` ) FROM $table WHERE `field_id` = 'fld_9727773'", ARRAY_A );
if( isset( $r[0], $r[0]['SUM( `value` )'])){
$total = $r[0]['SUM( `value` )'];
}else{
$total = 0;
}
[my_cf_shortcode form="cf123456789" field="fld_9727773"]
<?php
/**
* Create a custom shortcode to display total value of a field's entries above the form
*/
add_shortcode( 'my_cf_shortcode', function( $atts ){
$args = shortcode_atts( array(
'form' => null,
'field' => null,
), $atts );
if( empty( $args[ 'form' ] ) || empty( $args[ 'field' ] ) ){
return;
}
$field = caldera_forms_very_safe_string( $args[ 'field' ] );
global $wpdb;
$table = $wpdb->prefix . 'cf_form_entry_values';
$r = $wpdb->get_results( $wpdb->prepare( "SELECT SUM( `value` ) FROM $table WHERE `field_id` = '%s'", $field ), ARRAY_A );
if( isset( $r[0], $r[0]['SUM( `value` )'])){
$total = $r[0]['SUM( `value` )'];
}else{
$total = 0;
}
//IMPORTANT - Customize display of total here -
$out = '<div class="my-cf-total-display">So far the total raised is ' . $total . '</div>';
$out .= Caldera_Forms::render_form( $args[ 'form' ] );
return $out;
});
@shawnhooper

This comment has been minimized.

Copy link

@shawnhooper shawnhooper commented Oct 4, 2017

global $wpdb;

$total = (int) $wpdb->get_var( $wpdb->prepare( "SELECT IFNULL(SUM(value),0) FROM {$wpdb->prefix}cf_form_entry_values} WHERE field_id = '%s'", caldera_forms_very_safe_string( $args[ 'field' ] ) ) );

Should do it.

You can replace lines 13 - 21 with the above 2 lines.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.