functions - Add sequential number to a Gravityforms form

admin2025-06-07  2

I have multiple forms. Based on those forms I need a hidden field populated with a prefix and a sequential number. Each form has a hidden field. That field can be filled dynamically and has a paramater of 'uuid'.

I have the following code... any suggestions on how to change the following code? This snippet works but it uses a random number (mt_rand), I prefer [prefix]-00001, [prefix]-00002, etc.

In addition it would be even better when every form has his own prefix. But that beyond my php skills. Preferably sequential for every form, example:

Form A with a prefix 'prd' ([prefix]-00001)

  • prd-00001
  • prd-00002

Form B with a prefix 'lead' ([prefix]-00001)

  • lead-00001
  • lead-00002

Thanks for any thoughts and ideas. Keep in mind that my php skills are pretty basic.

Paul

add_filter("gform_field_value_uuid", "get_sequential_nr");
function get_sequential_nr(){
    $prefix = "set_prefix_here";
    do {
        $unique = mt_rand();
        $unique = substr($unique, 0, 5);
        $unique = $prefix . $unique;
    } while (!check_unique_nmbr($unique));
    return $unique;
}
function check_unique_nmbr($unique) {
    global $wpdb;
    $table = $wpdb->prefix . 'rg_lead_detail';
    $result = $wpdb->get_var("SELECT value FROM $table WHERE form_id = '$form_id' AND field_number = '$field_id' AND value = '$unique'");
    if(empty($result))
        return true;
    return false;
}

I have multiple forms. Based on those forms I need a hidden field populated with a prefix and a sequential number. Each form has a hidden field. That field can be filled dynamically and has a paramater of 'uuid'.

I have the following code... any suggestions on how to change the following code? This snippet works but it uses a random number (mt_rand), I prefer [prefix]-00001, [prefix]-00002, etc.

In addition it would be even better when every form has his own prefix. But that beyond my php skills. Preferably sequential for every form, example:

Form A with a prefix 'prd' ([prefix]-00001)

  • prd-00001
  • prd-00002

Form B with a prefix 'lead' ([prefix]-00001)

  • lead-00001
  • lead-00002

Thanks for any thoughts and ideas. Keep in mind that my php skills are pretty basic.

Paul

add_filter("gform_field_value_uuid", "get_sequential_nr");
function get_sequential_nr(){
    $prefix = "set_prefix_here";
    do {
        $unique = mt_rand();
        $unique = substr($unique, 0, 5);
        $unique = $prefix . $unique;
    } while (!check_unique_nmbr($unique));
    return $unique;
}
function check_unique_nmbr($unique) {
    global $wpdb;
    $table = $wpdb->prefix . 'rg_lead_detail';
    $result = $wpdb->get_var("SELECT value FROM $table WHERE form_id = '$form_id' AND field_number = '$field_id' AND value = '$unique'");
    if(empty($result))
        return true;
    return false;
}
Share Improve this question edited Aug 1, 2018 at 12:29 Kortschot asked Aug 1, 2018 at 12:22 KortschotKortschot 1171 silver badge7 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

Have you looked at the Gravity Forms Unique ID Wiz? This allows you to specify a prefix for each form that gets added to the Entry ID.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1749226065a317416.html

最新回复(0)