how to Percentage of a specific field values

By: Sina Pars | Asked: 08/20/2024
ForumsCategory: Code Helphow to Percentage of a specific field values
Sina Pars asked 2 months ago

Hi there 
I am calculating the average of submitted forms using the following code with three conditions:

- Field ID > 26
- Field value > input
- Time range

Everything is working correctly. For example, using the shortcode below, I am calculating the average of forms submitted within a specified time range where the value in field number 50 matches a specified input value.

Now, I need to add another condition or filter to it so that we can filter the values based on another field in the form, which has ID 50 and contains people's email addresses, so that each person only sees the average data related to themselves and not all submitted forms.

Document Link:
https://formidableforms.com/knowledgebase/add-field-totals-and-statistics/#kb-percentage-of-a-specific-field-value

 

My Shortcode:

[frm-percent id=26 value="input"]

My Code is:

add_shortcode('frm-percent', 'frm_stats_percent');
function frm_stats_percent($atts){
// Start date: 10 days after the first day of the current month
$first_day_of_month = date('Y-m-01 00:00:00');
$start_date = date('Y-m-d H:i:s', strtotime($first_day_of_month . ' +10 days')); // End date: Current date and time
$end_date = wp_date("Y-m-d H:i:s", null ); $defaults = array(
'id' => false,
'user_id' => 'false',
'value' => false,
//'round' => 100,
'limit' => '',
'created_at_greater_than_or_equal_to' => $start_date,
'created_at_less_than_or_equal_to' => $end_date
);
extract(shortcode_atts($defaults, $atts));
var_dump ($defaults); // For debugging, display the calculated dates
echo 'Start Date: ' . $start_date . '<br>';
echo 'End Date: ' . $end_date . '<br>'; if (!$id) return; $type = 'count'; // Count the number of entries that match the target value
$value_count = FrmProStatisticsController::stats_shortcode(compact(
'created_at_greater_than_or_equal_to',
'created_at_less_than_or_equal_to',
'id',
'type',
'value',
'limit'
)); echo 'Total Count Based On Target Value Count: ' . $value_count . '<br>'; // Count the total number of entries based on the target field
$total_count = FrmProStatisticsController::stats_shortcode(compact(
'created_at_greater_than_or_equal_to',
'created_at_less_than_or_equal_to',
'id',
'type',
'limit'
)); echo 'Total Count Based on Target Field:' . $total_count . '<br>'; // Return the percentage of entries that match the target value
return round((($value_count / $total_count) * 100));
}
1 Answers
Victor Font answered 2 months ago
There's not enough information to go on to provide a definitive solution. The solution depends on how/where you are displaying the output. You have to retrieve the user's WordPress ID and pass it as a parameter to FrmProStatisticsController::stats_shortcode() so the results are filtered. If you are calculating one user's entries at a time, then pass with user id as a parameter or use user_id=current as the parameter. If you need to loop, you'll need custom SQL.

Making the Best WordPress Plugin even better - Together

Take on bigger projects with confidence knowing you have access to an entire community of Formidable Experts and Professionals who have your back when the going gets tough. You got this!
Join the community
crossarrow-right