Insert Field Totals and Statistics

Knowledge BaseReports, Graphs, and StatisticsStatistics → Insert Field Totals and Statistics

Use this shortcode to display field totals and statistics in a page, post, View, or anywhere else shortcodes are accepted.

Usage

Insert the following shortcode on a page, post, View, or in any field in your form.

[frm-stats id=x type=count]

Replace x with the ID or key of the field you would like to display statistics for
Replace count with the type of statistics to display (all the different types are listed below)

Parameters

Required

  • id - ID or key of the field you would like to display statistics for
  • type - Specify the type of statistic you would like returned. If not specified, this defaults to total.
    • total: Adds up all the numeric values
    • count: Counts the number of entries
    • average or mean: Displays the average of numeric values
    • median: Displays the median of numeric values
    • star: Displays the average value in the format of a star rating graphic
    • maximum: Gets the maximum value from a set of values
    • minimum: Gets the minimum value from a set of values
    • unique: Counts the number of unique entries

Optional

  • user_id - Include only entries from a specific user in the statistic. This must be an integer or 'current'. To get a user ID integer, go into Users, select a user, and the user ID number will be in the address bar. Example: [frm-stats id=x type=count user_id=current]
  • round - The maximum number of decimal places to show. If not specified, up to 100 decimals will be displayed. Example: [frm-stats id=x type=total round=2]
  • limit - Set a maximum number of entries to use for this stat. Example: [frm-stats id=x type=count limit=5]
  • thousands_sep - Set the thousands separator for the displayed total. Example: [frm-stats id=x type=count thousands_sep=","]
  • entry_id - Add an entry ID or key to show statistics for a single entry, i.e. show the star rating for a single entry. Example: [frm-stats id=25 type=star entry_id=5]. In a View you can use [frm-stats id=25 type=star entry_id=[ id]]
  • field filters - Only include entries where a field in the same form is equal to, not equal to, greater than, less than, or contains a specific value.
    Note: you can use multiple field filters in the same shortcode to get more refined statistics.

    • Equals - Only include entries where a field is equal to a specific value. For example, you may get a total for field x when field 25 equals "Yes":
      [frm-stats id=x type=total 25="Yes"]

      Replace 25 with the ID of any field in the form. Replace "Yes" with any value.

    • Not equal - Only include entries where a field is NOT equal to a specific value.
      [frm-stats id=x type=total 25_not_equal="Yes"]

      Replace 25 with the ID of any field in the form. Replace "Yes" with any value.

    • Greater than - Only include entries where a field is greater than a specific value. This works well with numbers:
      [frm-stats id=x type=total 25_greater_than="10"]

      and also with dates:

      [frm-stats id=x type=total 25_greater_than="2015-03-01"]

      You may also use a dynamic date string:

      [frm-stats id=x type=total 25_greater_than="-1 week"]

      Replace 25 with the ID of a field in the form.

    • Less than - Only include entries where a field is less than a specific value. This works well with numbers:
      [frm-stats id=x type=total 25_less_than="10"]

      and also with dates:

      [frm-stats id=x type=total 25_less_than="2015-03-01"]

      You may also use a dynamic date string:

      [frm-stats id=x type=total 25_less_than="NOW"]

      Replace 25 with the ID of a field in the form.

    • Contains - Only include entries where a field contains a substring or value.
      [frm-stats id=x type=total 25_contains="Activity"]

      Replace 25 with the ID of any field in the form. Replace "Activity" with any value.

  • drafts - By default, the frm-stats shortcode does not count draft entries. You can choose to include only drafts, or both drafts and entries. This shortcode will count only drafts in the statistic:
    [frm-stats id=x drafts=1]

    This shortcode will count both drafts and completed entries in the statistic:

    [frm-stats id=x drafts=both]

Note: As of WordPress 4.2.3, < and > are no longer processed correctly. We will be replacing these options with a more standard format, but for now you can replace < with &lt; and > with &gt;

PHP Alternative

echo FrmProStatisticsController::stats_shortcode(array('id' => x, 'type' => 'count', 'user_id' => 'current'));

All of the parameters listed above can be used in this function as well.

Total entry count across all forms

This shortcode will get the total entry count across all forms for the current user. Add this function to your theme functions.php or to a new plugin, and add the shortcode [entry-count] in your page.

add_shortcode('entry-count','my_entry_count');
function my_entry_count($atts){
    global $user_ID;
    $ids = FrmDb::get_col( 'frm_items', array('user_id' => $user_ID));
    return count($ids);
}

 

Combined total for two fields

This creates a new shortcode [fields-stats] for use on your page. Your shortcode will look like this: [fields-stats ids="x,y,z"]. Replace x, y, and z with the IDs of the fields you want to total.

add_shortcode('fields-stats','my_fields_stats');
function my_fields_stats($atts){
    $defaults = array(
      'ids' => false,
    );
    $atts = array_merge($defaults, $atts);
    $ids = explode( ',', $atts['ids'] );
    unset( $atts['ids'] );

    $total = 0;
    foreach ( $ids as $id ) {
      $atts['id'] = $id;
      $total += FrmProStatisticsController::stats_shortcode( $atts );
    }
    return $total;
}

 

Percentage of a specific field value

After inserting this function in a new plugin or in your theme functions.php, add the shortcode [frm-percent id=x value="Option 1"] on your page.

add_shortcode('frm-percent', 'frm_stats_percent');
function frm_stats_percent($atts){
    $defaults = array(
    'id' => false, 'user_id' => false,
    'value' => false, 'round' => 100, 'limit' => ''
    );
    extract(shortcode_atts($defaults, $atts));
    if (!$id) return;
    $type = 'count';
    $value_count = FrmProStatisticsController::stats_shortcode(compact('id', 'type', 'value', 'user_id', 'limit'));
    $total_count = FrmProStatisticsController::stats_shortcode(compact('id', 'type', 'user_id', 'limit'));
    return round((($value_count / $total_count) * 100), $round);
}