• Resolved intownrunner

    (@eskwire)


    Thank you so much for this plugin and howto on creating a mail counter function. It has been very helpful.

    I was able to create a mail counter by using the plugin and following this guide:
    https://sevenspark.com/tutorials/how-to-create-a-counter-for-contact-form-7

    Now they are requesting leading zeros to the printed number. The number length should be six digits
    000001, 000002, …. 000100, 000101 … etc.

    What is the best way to accomplish this?
    How would I modify the code in function.php to get this to display?

    Thanks so much!

Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Author sevenspark

    (@sevenspark)

    Glad to hear it’s been helpful!

    Check out the PHP function str_pad https://php.net/manual/en/function.str-pad.php

    I think you’ll want to modify this function

    function cf7dtx_counter(){
        $val = get_option( CF7_COUNTER, 0) + 1;  //Increment the current count
        return $val;
    }

    To something like this

    function cf7dtx_counter(){
        $val = get_option( CF7_COUNTER, 0) + 1;  //Increment the current count
        $val = str_pad( $val , 6, '0' , STR_PAD_LEFT ); //Pad with 0s
        return $val;
    }

    Hope that helps!

    Thread Starter intownrunner

    (@eskwire)

    Thanks for the superfast reply and suggestion!

    I had been doing some testing and this seems to work also.

    function cf7dtx_counter(){
    	$val = str_pad(get_option( CF7_COUNTER, 0) + 1,6,"0",STR_PAD_LEFT); //Increment the current count
        return $val;

    I am not a PHP expert but learning as much as I can.

    Is one solution considered better than the other?
    You have single quotes around the zero and I have double. Could I use single quotes?

    Again thanks so much for the solution! — Steven

    Plugin Author sevenspark

    (@sevenspark)

    Hi Steven,

    The two are equivalent. Doing it in 1 vs 2 lines is more of a stylistic choice. One advantage of 2 lines is that it is more readable, which is why I chose to present it that way. Doing it in 1 line may have some negligible performance advantages, nothing worth worrying about in this case at all. So it’s really just your preference.

    There is a difference between single quotes and double quotes in PHP. In this case it doesn’t matter, but in general using single quotes is more efficient and preferable if you aren’t using $variables inside the string.

    Have a good one! ??

    Chris

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Add Leading Zeros to Mail Counter’ is closed to new replies.