samedi 20 avril 2019

How generate multiple UUID based on quantity field?

I'm having trouble adapting this Snippet to Gravity Forms. This Snippet works as follows:

Each entry generates a unique ID (UUID) based on Snippet's default settings.

Original Code:

            add_filter("gform_field_value_uuid", "get_unique");

            function get_unique() {
                $prefix = "SLP2017-"; // update the prefix here 
                do {
                    $unique = mt_rand();
                    $unique = substr($unique, 0, 8);
                    $unique = $prefix . $unique;     
                }
                while (!check_unique($unique));
                return $unique;
            }
            function check_unique($unique) {
                global $wpdb;
                $table = $wpdb->prefix . 'rg_lead_detail';
                $form_id = 1; // update to the form ID your unique id field belongs to
                $field_id = 10; // update to the field ID your unique id is being prepopulated in
                $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;
            }

What I'm trying to do is this:

Generate 'X' UUID based on the quantity field (other field) of gravity forms.

Example: The value of the field quantity is 20 so the Snippet has to generate: 20 differents UUID.


However in my attempts with "FOR" the maximum I could do was to generate a two different UUIDs but only returning the echo and not registering the UUIDs in the database and for some reason does not return the quantity that I put, in the example below I put 10 and it only returns me 2.

My Code:

            add_filter("gform_field_value_uuid", "get_unique");

            function get_unique() {
                $prefix = "SLP2017-"; // update the prefix here
                $quantity = 10; //quantity test to generate two diffente UUID's     
                do {
                    $unique = mt_rand();
                    $unique = substr($unique, 0, 8);
                    $unique = $prefix . $unique;
                    for($x = 1; $x < $quantity; $x++){
                       $unique;
                    }
                    echo $unique . ' | ';        
                }
                while (!check_unique($unique));
                return $unique;
            }
            function check_unique($unique) {
                global $wpdb;
                $table = $wpdb->prefix . 'rg_lead_detail';
                $form_id = 1; // update to the form ID your unique id field belongs to
                $field_id = 10; // update to the field ID your unique id is being prepopulated in
                $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;
            }

If any good soul can help me, I appreciate so much :)




Aucun commentaire:

Enregistrer un commentaire