Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Gravity Forms Function

Posted on 2011-02-23
13
Medium Priority
?
2,419 Views
Last Modified: 2012-05-11
I am trying to setup a form for our users to use to submit their insertion orders.  One of the features I am trying to ad to this form is automatic ID numbering for each order.  For instance we have three creative services departments.  Each order needs to be identified with the first run date of the product, followed by a letter that defines what department it is being built from and a unique  3 digit number.  

So a user would select "Department 1" then select the first run date from a date chooser field, and then the third filed would spit out a number such as this 022411s352.  Where the "s" designates Department 1.  I have come up with a php script that when added to the functions.php of my template will generate this number for me with today's date but I can't figure out how to get it to populate the date from the previous chosen field.  Any help would be greatly appreciated!




add_filter("gform_field_value_uuid1", "get_unique");

function get_unique() {

    $prefix = "s"; // update the prefix here

    do {
        $unique = mt_rand();
        $unique = substr($unique, 0, 3);
        $unique = date("mdy", strtotime('now')) . $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 = 1; // 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;
}

Open in new window

0
Comment
Question by:icarus004
  • 5
  • 4
  • 2
  • +1
13 Comments
 
LVL 11

Expert Comment

by:level9wizard
ID: 34963227
Are you looking for mysql_insert_id()? See: http://php.net/manual/en/function.mysql-insert-id.php
0
 
LVL 2

Author Comment

by:icarus004
ID: 34963500
Not exactly.  I need the number to be generated before the form ever gets submitted into the database.  Say there are 3 fields on the form.  Field 1 is the department, Field 2 is the date chooser, and filed 3 is the ad number.  First you select the department to provide the letter, then in filed 2 you select the first run date of the ad.  Then I need field 3 to take the information from the first 2 fileds and put them together to form an ad id number.
0
 
LVL 11

Expert Comment

by:level9wizard
ID: 34963718
This could be achieved with javascript click events via jQuery. I will try and post an example shortly.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 111

Accepted Solution

by:
Ray Paseur earned 1000 total points
ID: 34969490
Are you interested in generating sequence numbers or random numbers?  In the string 022411s352 are you considering using 022411 to mean February 24, 2011?  If so, you might want to learn about the ISO-8601 standard for dates, which would argue powerfully against using the MDY sequence.  The YMD sequence collates and sorts in meaningful ways.  MDY does not.

To check for a unique number, you do not want to use SELECT.  Instead you want to use INSERT.  This will ensure that any suggested unique number becomes a part of the data base immediately.  Trap MySQL_ErrNo() and test for 1062.  If the INSERT fails because of 1062, you tried to insert a non-unique value, and you need to call the number generator again.  Otherwise, you can use the value you just inserted.  If you do the SELECT and INSERT separately, you run the risk that your table will lose integrity due to script races.

From the look of things above, your numbering scheme will collapse if any department gets more than 999 orders in a day.  As a businessman, I would not want to hear that my data systems cannot handle this kind of growth.  And as a practical matter, the random generation of three digit numbers will have a very long "tail" as you populate the numbers set from 000 to 999.  Number collisions will become prevalent as more holes are filled in.  The risk of script timeout will grow exponentially.  So you might want to rethink the whole numbering strategy, and perhaps use a four (or more) character field and a 31 letter alphabet to build the vocabulary of pseudo-random strings.  The number of such unique words is computed by pow($length,strlen($alphabet)); and this is about 4.6E18 if the length of the resulting string is 4 and the alphabet is ABCDEFGHJKMNPQRSTUVWXYZ23456789.  Note the omission of ambiguous-looking characters like I, l, O, 0, etc.
0
 
LVL 11

Assisted Solution

by:level9wizard
level9wizard earned 1000 total points
ID: 35030289
I think mysql_insert_id() is probably going to be what you want - forget my comments regarding jquery, there's no need to have the ID formation happen client side (even still, I was more thinking that this would solely be used for displaying purposes), but as Ray_Paseur put it: number collisions become more prevalent as more layers exist between the database that writes this ID.

mysql_insert_id() will let you reliably build a new [unique] id based on the last (auto increment) ID in the database.

That said, if this simpler still, and all you're really trying to do is combine several strings (concatenation), you would just do something like:
...

$field1 = mysql_real_escape_string($_POST['field1']);
$field2 = mysql_real_escape_string($_POST['field2']); // OR for example, $field2 = mysql_insert_id();
$field3 = mysql_real_escape_string($_POST['field3']);

$ID = $field1 . $field2 . $field3;

...

Sorry if I'm not fully understanding - hope this helps.
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 35031886
mysql_insert_id() is unique to the data base level.  But I was guessing that since the original question included a random number generator, or asker knew that he wanted a random number and not a sequential auto_increment id.  Maybe we will get clarification on that point.
0
 
LVL 2

Author Comment

by:icarus004
ID: 35037063
I am needing a random generated 3 digit ID number with information from previous form filds (a selected date and letter that designates ad type and department) appended to the beginning of the number.
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 35037148
Random number of not more than 3 digits:
$r = mt_rand(0, 999);

Padded to 3 characters:
str_pad($r, 3, '0', STR_PAD_LEFT);
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 35037232
You might be asking yourself, "What are my risks of number collisions in the randomization process?"  To help you understand that, you need to think in terms of the probability that any given number has NOT been taken yet.  You might want to install this little script and run it a few times to see how quickly the collisions start to occur.  It is possible, though unlikely that the first two random numbers could be the same number.  It is almost inevitable that you will have a collision before you get to the 80th random number.
<?php // RAY_true_random_1000.php
ini_set('display_errors', TRUE);
error_reporting(E_ALL);


// DEMONSTRATE THE TIME IT TAKES TO FILL IN AN ARRAY OF 1000 ELEMENTS WITH RANDOM NUMBERS


date_default_timezone_set('America/Chicago');
echo "<pre>";

// AN ARRAY TO BE FILLED WITH NUMBERS
$array = array();
$n = 0;
while ($n < 1000)
{
    $array[$n] = FALSE;
    $n++;
}

// AN ARRAY OF TIMES
$timex = array();

// SHOW WHAT WE CAN GET DONE IN TEN SECONDS
set_time_limit(10);
$n = 0;
$k = 0;
while ($n < 1000)
{
    // GENERATE A RANDOM NUMBER
    $r = mt_rand(0, 999);

    // IS THIS NUMBER A NEW RANDOM NUMBER?
    if ($array[$r] === FALSE)
    {
        // YES - SAVE IT
        $array[$r] = str_pad($r, 3, '0', STR_PAD_LEFT);
        $t = microtime(TRUE);
        $timex[] = $t;
        $n++;

        // SHOW THE POSITION, THE NUBER OF TRIES IT TOOK, THE NUMBER AND THE TIMER
        echo PHP_EOL . "$n $k {$array[$r]} $t";
    }
    else
    {
        // NO, ALREADY HAVE THIS NUMBER
        $k++;
    }
}

Open in new window

0
 
LVL 11

Expert Comment

by:level9wizard
ID: 35037479
[icarus004]>>I am needing a random generated 3 digit ID number with information from previous form filds

That is simply the PHP function Ray mentioned, combined with concatenation from the two other $_POST fields as I showed above. Since you're doing this on a form (assuming post) you can show the concatenated string on the result page (provided all other conditions are met and the save should be successful to the database).


Another way to force a unique number, when a finite range is involved (0 to 999), is to concatenate in a date/timestamp.

So using the PHP method Ray mentioned, you could do something like:

$r = mt_rand(0, 999) . time();


0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 35037657
Actually, line 10 of the original code snippet shows how to do concatenation.

$unique = date("mdy", strtotime('now')) . $prefix . $unique;


But that said, I still stand by the recommendations at http:#34969490
0
 
LVL 70

Expert Comment

by:Jason C. Levine
ID: 36508823
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
0

Featured Post

[Webinar] Cloud Security

In this webinar you will learn:

-Why existing firewall and DMZ architectures are not suited for securing cloud applications
-How to make your enterprise “Cloud Ready”, and fix your aging DMZ architecture
-How to transform your enterprise and become a Cloud Enabler

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
There is a massive demand for content on the web right now, and it doesn't look like it's going to stop any time soon. But, if you are running a business blog, it's not just enough to offer your audience lots of content. It needs to be high-quality…
The purpose of this video is to demonstrate how to set up the permalinks on a WordPress Website. This will be demonstrated using a Windows 8 PC. Go to your WordPress login page. This will look like the following: mywebsite.com/wp-login.php : Go t…
The is a quite short video tutorial. In this video, I'm going to show you how to create self-host WordPress blog with free hosting service.
Suggested Courses

916 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question