PLEASE NOTE: These forums are no longer utilized and are provided as an archive for informational purposes only. All support issues will be handled via email using our support ticket system. For more detailed information on this change, please see this blog post.

Counter

  1. Hi,

    i recently set up a form for a wedding asking people if they want to attend the wedding. The form has three choices, "yes", "no" and "maybe". in case a person chooses "yes", the forms asks how many adults will be brought, i.e. "2" (person + wife). The last field covers kids, but i don't want to count them, just adults.

    i would like to display a counter on the frontend, which updates itself live according to the total of persons who filled out the form.

    is this possible with gf?

    Posted 11 years ago on Saturday February 16, 2013 | Permalink
  2. It's possible. You can get the number of entries for a form fairly easily.

    http://pastie.org/2051574

    However, that will only count entries, not how many guests were submitted per entry.

    You're going to have to total up all the values for your 'yes' entries. I did something similar, although it was totaling money rather than people. The idea is the same. You just use the shortcode in a widget or somewhere else on your site to show the total:

    http://pastebin.com/NSsVZgt8

    Posted 11 years ago on Monday February 18, 2013 | Permalink
  3. Thank you Chris. Does it make a difference that it is not about adding the entry values for "yes", but the number of persons (2,3,4..) from several entries. Subsequently to "yes" people are asked to choose how many folks they will bring along.. that's the number i need.

    Posted 11 years ago on Monday February 18, 2013 | Permalink
  4. It does not make a difference. You will not be counting the number of yes responses, but totaling the values which are stored in the "number of guests" field, whatever that field is. You will sum the values in all the entries to arrive at your total. The code will be different but the approach will be the same.

    Posted 11 years ago on Monday February 18, 2013 | Permalink
  5. thanks, will try that out.

    Posted 11 years ago on Tuesday February 19, 2013 | Permalink
  6. I pasted the snippet into my functions.php, did a little customizing and set up the counter here: http://www.doro-und-nick.de/test/

    The counter displays, but does not show entries from the guest list form (form ID 1). The form is located here: http://www.doro-und-nick.de/gaesteliste/, Field-ID 2 asks for the number of guests (actually the frontend choices are "1+1", "1+2" and so on, but the backend values in the form are straight numbers like 2, 3 etc.).

    This is your code after my customizing:

    // http://pastebin.com/kHpaHQvi originally
    // http://www.gravityhelp.com/forums/topic/show-total-donations
    // usage: [gaesteliste form=01] where 37 is the form ID
    add_shortcode('guests', 'total_guests');
    function total_guests($atts) {
            $form_id = $atts['form'];
            // function to pull all the entries for one form
            $guests = RGFormsModel::get_leads($form_id);
            // start the total at zero
            $total = 0;
            // initialize a counter for the number of guests entries made
            $i = 0;
            // loop through all the returned results
            foreach ($guests as $amount) {
                    // add each guest attending to the total
                    // change 2 here to the field ID which holds the amount of atteding guests
                    $total += $amount[2];
                    // increment the counter so we know how many guest entries there are as well
                    $i++;
            }
            // change this to your locale, or, if your locale is already defined, you can remove this line
            setlocale(LC_MONETARY, 'de_DE');
            // do some formatting and return the html output from the shortcode
    	$output = "Nach bisherigem Stand haben " . money_format('%i', $total) . " Personen ihr Kommen zugesagt.";
            // just the string above will be returned.  You can style it here or where you are using the shortcode
            return $output;
    }
    // needed for the above to process the guests list shortcode in sidebar widget
    add_filter('widget_text', 'do_shortcode');

    1. Any suggestions why the form entries are not shown/added?

    2. How do I have to change the following line to only show the total number of guests without decimal places and currency? Sry, keep getting errors if i start changing it on my own..

    $output = "Nach bisherigem Stand haben " . money_format('%i', $total) . " Personen ihr Kommen zugesagt.";

    Posted 11 years ago on Tuesday February 19, 2013 | Permalink
  7. Btw sorry for the screwed code. Here is a link to

    http://pastebin.com/ic8p7H6P

    and i'll stay off from these backticks.

    Posted 11 years ago on Wednesday February 20, 2013 | Permalink
  8. #2 You can use this line:

    [php]
    $output = "Nach bisherigem Stand haben $total Personen ihr Kommen zugesagt.";

    You can also delete line 22 since you don't need the LOCALE.

    Posted 11 years ago on Saturday February 23, 2013 | Permalink
  9. Are you using the shortcode like this:

    [php]
    [guests form=1]

    If you enable WP_DEBUG in your installation, you might be able to spot where this is going wrong.
    http://codex.wordpress.org/WP_DEBUG

    Posted 11 years ago on Saturday February 23, 2013 | Permalink
  10. Hi Chris, thanks for your help. Indeed I was using the wrong shortcode. Additionally WP Debug displayed an error based on wrong customizing in line 41, where i had changed "$form_id = $atts['form'];" to "$form_id = $atts['1'];". After changing it works beautifully now. Here is the fixed snippet for the counter: http://pastie.org/6333047.

    Just a final question: My form asks for the number of adults (field id 2) and children (field id 5). How do I have to extend the snippet to get the value for field id 5 as well - not added to the value of field id 2 but separated. I would like to display the guest counter like "So far X adults and Y kids are attending the wedding".

    Posted 11 years ago on Monday February 25, 2013 | Permalink
  11. It looks like you're doing all the formatting in the widget to surround the numbers. It might be easier to put all that text in this function, so instead of just returning a number (and now you need two numbers) you could return everything, all formatted for output. You would return a string like "So far X adults and Y kids are attending the wedding" rather than a number of adults, which is what you are doing now. Take a look at this code: http://pastebin.com/BU61sbjr

    (I removed the $numeric_amount line since you were never using that variable anywhere)

    Posted 11 years ago on Tuesday February 26, 2013 | Permalink
  12. Hi Chris. This is brilliant, exactly what i was looking for. Thanks a lot for your awesome support!

    Posted 11 years ago on Tuesday February 26, 2013 | Permalink
  13. You're welcome.

    Posted 11 years ago on Saturday March 2, 2013 | Permalink
  14. Reopened so Nick can ask further questions here.

    Posted 11 years ago on Saturday March 30, 2013 | Permalink
  15. Thank you Chris for reopening the topic.

    The counter is used on http://www.doro-und-nick.de. The website is password-protected.

    guest-username: hochzeit
    password: 31026092

    Initially the counter worked beautifully and summed up all entries to the correct total. From the amount of round about 50 people in total (don't know exactly, it may have happened from the start), the counter kept adding new entries to the total, but not according to their actual number. According to the GF entries in WP backend we currently have 61 adults and 13 children attending the wedding, while the counter only displays 48 adults and 10 children.

    Here is a link to my functions.php. The Counter snippet starts at line 36. http://pastie.org/7159946

    If you need login data to FTP or WP backend (i.e. to check the form entries), please let me know.

    Cheers Nick

    Posted 11 years ago on Saturday March 30, 2013 | Permalink
  16. Nick, try replacing line 46 with this line:

    [php]
    $guests = RGFormsModel::get_leads($form_id, '', '', '', 0, 1000, null, null, false, null, null);

    The 1000 in there will be the limit to the number of records returned. It's 30 by default. Give that code a shot and see if it works accurately. If not, send me a WordPress admin login and I will take a look at it live. Thanks.

    Posted 11 years ago on Monday April 1, 2013 | Permalink
  17. Hi Chris, perfect. Thanks a lot!

    Posted 11 years ago on Monday April 1, 2013 | Permalink
  18. Glad that worked for you. Thanks for the update Nick.

    Posted 11 years ago on Tuesday April 2, 2013 | Permalink

This topic has been resolved and has been closed to new replies.