Jump to content

Summarize Field Data


Recommended Posts

Hi Folks,

I've got a site with some contact and quote forms which get sent to my client and get saved as pages so the client has a record of the submitted forms. He'd like to analyse some of the form data. So for instance, there's a field that asks 'where did you hear about us?' with select options like 'Advertisement', 'Google', 'Recommendation', 'Magazine', etc. I'd like provide a page so the client could see how many respondents chose 'Google' vs 'Advertisement', etc. A simple tally would be fine, but then I could calculate percentages, maybe chart the results, etc.

My first thought was to use $pages->count('selector') to count the pages with a certain answer. I'd know what percentage that was by comparing it to the total of saved pages for that form. But would I need to do this for each question? Is there a better way to tally/count all the answers for a given field?

Link to comment
Share on other sites

If the select fields are Page fields you can use a function like this:

function calculateProportions($field_name) {
    $pages = wire('pages');
    $field = wire('fields')->get($field_name);
    // Only proceed if field is a Page field
    if($field->type->name !== 'FieldtypePage') return;
    // Get templates the field has been added to
    $tpls = $field->getFieldgroups();
    // We need a page object to supply to some upcoming methods
    $p = new \ProcessWire\Page();
    $inputfield = $field->getInputfield($p);
    // The options for the field
    $select_options = $inputfield->getSelectablePages($p);
    // Use whichever type of total suits you best...
    $total_pages = $pages->count("template=$tpls"); // pages that contain the field
    //$total_pages = $pages->count("$field_name!=''"); // pages where the field is not empty

    $out = array();
    foreach($select_options as $select_option) {
        $count = $pages->count("$field_name=$select_option");
        $proportion = $count / $total_pages;
        $out[$select_option->title] = $proportion;
    // Function returns an array: option title => proportion
    return $out;

And for each question (Page field)...

$results = calculateProportions('my_page_field_name');

If you are using FormBuilder with fields of the "Select" type you could modify the function to suit. I think you get the array of select options with...

$form = $forms->get('my_form_name');
$select_options = $form->get('my_select_field_name')->getOptions();


  • Like 3
Link to comment
Share on other sites

Thanks, Robin S! I'm using neither Page Fields nor the FormBuilder, but (relatively new) Options Field Type. But I just did a test with a Page Field to get my head around your code, and I think I get it. I've also read up on the Options field type and will take a stab at putting something together. Thanks for pointing me in the right directions.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Create New...