• Resolved mackans

    (@mackans)


    Hi
    I have a form (CF7) and the submissions are stored in CFDB. Is it possible to use the count shortcode inside a PHP function and compare the output number against another number to decide whether or not to show the form? What I’m looking for is to hide the form when the number of submissions >600. This is the function that I’ve tried:

    <?php
    $sumregistrations = <?php echo do_shortcode('[cfdb-count title="Registration"]'); ?>;
    if ($sumregistrations > 600 ): ?>
        //HIDE THE FORM
    <?php else : ?>
        //SHOW THE FORM
    <?php endif; ?>

    I got some advice from a guy at stackoverflow and first and foremost he said I can’t use a php function inside another php function. Secondly, he said:

    …this shortcode outputs a string wrapped in HTML. Therefore, you cannot use it to compare against 600, as it will always return false. You should get the count value directly from the db table.

    Can you give some advice on how to achieve what I’m looking for? Or is it not possible at all?

    Thanks

Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Author Michael Simpson

    (@msimpson)

    Similar to this example: https://cfdbplugin.com/?page_id=367

    Something like this might do it:

    
    require_once(ABSPATH . 'wp-content/plugins/contact-form-7-to-database-extension/CFDBFormIterator.php');
    $exp = new CFDBFormIterator();
    $exp->export('Registration', array('show' => 'submit_time', 'trans' => 'CountField(submit_time)'));
    $count = 0;
    while ($row = $exp->nextRow()) {
        $count = $row['submit_time'];
    }
    
    Thread Starter mackans

    (@mackans)

    Thank you so much Michael! It works great! Here’s the final code that’s working, in case someone else need it:

    <?php require_once(ABSPATH . 'wp-content/plugins/contact-form-7-to-database-extension/CFDBFormIterator.php');
        $exp = new CFDBFormIterator();
        $exp->export('NAME OF THE FORM', array('show' => 'submit_time', 'trans' => 'CountField(submit_time)'));
        $count = 0;
        while ($row = $exp->nextRow()) {
            $count = $row['submit_time'];
        }
        if ($count > 600 ): ?>
            //Content to show if number of submissions are greater than 600.
        <?php else : ?>
            //Content to show if number of submissions are less than 600.
            //For example, show the form <?php echo do_shortcode('[contact-form-7 id="112" title="NAME OF THE FORM"]'); ?>
        <?php endif; ?>

    Thank you again!

    /Marcus

    Thread Starter mackans

    (@mackans)

    Hi again

    Your code above works great! I have another question now. How can I make the function to exclude rows that has a value in a certain column?

    For example, in the form I have a field that is optional to fill in. If the field is left blank I don’t want to include this submission (the row in the database) in the count function.

    • This reply was modified 9 years, 2 months ago by mackans.
    Plugin Author Michael Simpson

    (@msimpson)

    In the array you are passing to $exp->export, add

    "tfilter" => "yourfield==null"

    or whatever filter expression you prefer. “tfilter” expressions are evaluated prior to transform functions, “filter” expressions are evaluated after.

    Thread Starter mackans

    (@mackans)

    Thank you! It works!

Viewing 5 replies - 1 through 5 (of 5 total)

The topic ‘Use the count shortcode inside a PHP function’ is closed to new replies.