Forums

Setting uid cloumn characters limit

raygold 04 May, 2011
Im mysql db table Chonoforms creates a new column called "uid";If I'm correct uid column assign a unique id every user that submit the form.
Can I limit the number of characters of "uid",setting the varchar limit,eg to 10?
GreyHead 04 May, 2011
Hi raygold,

Yes I think you can.

But note that the UID isn't necessarily unique (though it's very unlikely that it isn't); and shortening it increases the chances of a duplicate.

If you really need a unique id then you should generate your own and check it for uniqueness.

Bob
raygold 04 May, 2011
How can I generate unique id?
Shoul I edit Chronoforms or database table option?
GreyHead 04 May, 2011
Hi raygold,

Please see this article from The ChronoForms Book - that includes the code you need.

Bob
raygold 04 May, 2011
Thanks I found your article very helpful.
Since I'm not interesting to add a bar code,but only generate an unique user id for every user that submit the form,I simplified the procedure in three steps as follows:

1. I added this code in the "On Submit before" box:
<?php
if ( ! $mainframe->isSite() ) { return; }
JRequest::setVar('ident', generateIdent());

/*
 function to generate a random alpha-numeric code
 using a specified pattern
  *
 * @param $pattern string
 * @return string
 */
function generateIdent($pattern='AA9999A')
{
  $alpha = array("A","B","C","D","E","F","G","H",
    "J","K","L","M","N","P","Q","R","S","T","U","V","W",
    "X","Y","Z");
  $digit = array("1","2","3","4","5","6","7","8","9");
  $return = "";
  $pattern_array = str_split($pattern, 1);
  foreach ( $pattern_array as $v ) {
    if ( is_numeric($v) ) {
      $return .= $digit[array_rand($digit)];
    } elseif ( in_array(strtoupper($v), $alpha) ) {
      $return .= $alpha[array_rand($alpha)];
    } else {
      $return .= " ";
    }
  }
  return $return;
}
?>

2. The following code in the Email template(sent to user):
<div>Your code: {ident}</div>

3. And this code in the "After submit text" box:
<div>Your registration code is: {ident}</div>


Could you tell me if the steps above are correct or If I forgotten something?
GreyHead 04 May, 2011
Hi raygold,

That looks good.

Just to underline for other readers: like the uid, that code will not necessarily give you a unique id. If it is vital that it is unique then you need to store issued codes and add a few extra lines to check the the generated value hasn't been used before.

Bob
raygold 04 May, 2011
Thanks for the clarification.🙂
raygold 05 May, 2011
Hi again,
I have another question: how is possible store the "ident" item in form db table?
Should I set form code or database table?
GreyHead 05 May, 2011
Hi raygold,

Normally I'd save the form results and include the id as a column in that table using the DB Connection. If you aren't saving the form results you can still create a little table and just save the id.

Bob
raygold 05 May, 2011
I have set form to save results in db table.
Should I add the "ident" id manually,through tables manager feature?
GreyHead 05 May, 2011
Hi raygold,

Yes add an new column either with the Table Manager or with PHPMyAdmin.

Bob
This topic is locked and no more replies can be posted.