Gravity Forms get field entries

7.7k views Asked by At

A logged in user fills out a form several times. From his entries, I'm attempting to get all of his inputs for a specific field and put them into a PHP array.

For the sake of simplicity, assume the form has ID 10 with the first field called 'SomeField' and the user was logged in (and is still logged in) for all entries.

Here's my best attempt at creating an array of all SomeField entries from the user:

get_currentuserinfo();
$searchCriteria = array(
    array(
        'key' => 'created_by',
        'value' => $current_user->user_login
    ),
    array(
        'key' => '1',
        'value' => 'SomeField'
    ),
);

$form = GFAPI::get_entries( 10, $searchCriteria );
echo print_r($form);

Unfortunately, this print_r appears to display an empty array. I believe my searchCriteria is somehow incorrect.

2

There are 2 answers

2
user23058230 On

I found it's easier to omit the second parameter ($searchCriteria) and simply use $form[0]['1'] for example which will display the first field of the first entry of the specified form.

0
Steve On

I found with user23058230 great solution in the second answer I always got the latest form created, which worked well for new signups but not later on.

Assuming here that the form ID is number 1, you can search it with a for loop using the entry_id which is available in the user's login array as $current_user->entry_id

No search query is needed other than the form you want returned - in this case form #1.

You can search other items using the keys you can see from your array dump.

Great solution which with the addition of this code solved my problem.

$form = GFAPI::get_entries( 1, '');

$what_i_want = 0;

    for( $i = 0; $i < count($form); $i++ ){

        if( $form[$i]['id'] == $current_user->entry_id ){

        // the item I want
        $what_i_want = $form[$i]['22'];

        //echo " I " . $form[$i]['id'] . " F " . $form[$i]['22'] . " i " . $i . " T " . $what_i_want . '<br />';

        break;
        }

    }