• I’m building my site on WordPress and as a beginner I’m doing a proof of concept which will focus on entering data into a form and seeing if I can retrieve and display the results.

    I can see within mysql where the data is being saved. The providers of the form – Ninja Forms, suggest that I use a function. That function would return a PHP array containing all of the submissions that it found. You’d then need to loop through those and display them using PHP.

    My questions are:

    1) Where do I place this function? For testing purposes can I put it simply within the index.php file?

    2) Once I’ve placed my function, what do I need to do in terms of creating a loop?

    3) How do I display the results from the loop?

    Here is a copy of the function:

    $args = array(
    ‘form_id’ => 4, // Only include submission from the form with an ID of 4
    ‘user_id’ => 5, // Only include submissions from the user with an ID of 5
    3 => ‘test’ // Only include submissions where the field with an ID of 3 has a user value of ‘test’
    );
    $sub_results = ninja_forms_get_subs( $args ); // This is an array of submission results.

    Thank You

Viewing 1 replies (of 1 total)
  • Moderator bcworkz

    (@bcworkz)

    You don’t really have a function there, it’s code that’s executed when encountered. The real function is ninja_forms_get_subs(). But that’s OK, the code belongs on the template file where you want to have the results displayed. Unless this has to do with posts, I would suggest placing the code on a custom page template.

    Once the template is created, add a new page based on this template. Give the page a title, you do not need to add any content. Any time someone wants to see the results, they request this page.

    The template, including the loop and output of form content, would look something like this:

    <?php /*
    Template Name: Form Results
    */
    get_header();
    $args = array('form_id' => 4, 'user_id' => 5, 3 => 'test');
    $sub_results = ninja_forms_get_subs( $args );
    foreach ($sub_results as $row ) {
       echo "First Field: $sub->first_field";
       // echo all fields as required
       echo "Last Field: $sub->last_field<br>";
    }
    get_footer();

    You would need to adjust the field references to the actual names. You probably will want to add various stylistic HTML content such as headers, divs, lists, etc. but this is the gist of it.

    If this loop needs to run at various places on your site, you could make the loop and get_subs into a true function. Then call this function with whichever $args are appropriate and all the looping, styling, and echoing are all handled by the function. Such functions would go on your theme’s functions.php file.

Viewing 1 replies (of 1 total)
  • The topic ‘Arrays and Functions Query’ is closed to new replies.