1. Fabrik 3.9 has been released. If you have updated Joomla to 3.9, this is a required update.
    Dismiss Notice

[Solved] Manage the uniqueness of a random string automatically

Discussion in 'Community' started by georgie, Oct 8, 2018.

  1. georgie

    georgie Member

    Level: Community
    Hello

    I want to generate a random string of 14 characters automatically during a form submission.
    OK, I do this with success, in a Default value (Eval) on a simple field:
    Code (Text):
    return substr(str_shuffle(str_repeat('0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ', mt_rand(1,14))),1,14);
    But I want to be sure of the uniqueness of my string. So I can not just use isunique plugin, because I would do this automaticallyand hidden.
    Is it possible in Eval?

    Thank you

    Georges
     
  2. cheesegrits

    cheesegrits Support Gopher Staff Member

    Level: Community
    georgie likes this.
  3. georgie

    georgie Member

    Level: Community
    Thank, I tried, but I have always the message 'Something went wrong!', even deleting 'these 3 lines':
    Code (Text):
    if ($oops++ > 10) {
          return "Something went wrong!";
       }
    Also my SQL system seems to want:
    Code (Text):
    ->where('my_field LIKE '
    Instead of:
    Code (Text):
    ->where('my_field= '
    This is not a problem, but I do not understand why it returns a error.

    Georges
     
  4. troester

    troester Well-Known Member Staff Member

    Level: Community
    Can you post your complete code?
    The same error message is generated in the exception (so better use two different messages).
     
    georgie likes this.
  5. cheesegrits

    cheesegrits Support Gopher Staff Member

    Level: Community
    Can you dump the query (when it's using the =) ...

    var_dump((string)$myQuery);exit;

    ... just after the setQuery(), and see if there's some obvious problem with it? I can't see anything in my original code that would cause a problem.

    If you can't see the problem, paste it here.

    -- hugh
     
    georgie likes this.
  6. georgie

    georgie Member

    Level: Community
    Hey! Yes thank you, dumping and testing the query, I understand:
    My problem was not linked to the "=" or other syntax, but about the name of my random field.

    Indeed I would do this to customize the Acymailing subscribe form myself. The field "key".

    But this name seems to be a key word in MySQL, so just adding the table name, it works:

    THX
     
  7. cheesegrits

    cheesegrits Support Gopher Staff Member

    Level: Community
    Yeah, it's always best to use $myDb->quoteName('your_field_name') ...

    Code (Text):

    ->where($myDb->quoteName('key') . ' = ' . $myDb->quote($myString));
     
    ... or you can include the table name .... $myDb->nameQuote('your_table_name.your_field_name').

    I have a bad habit of not doing that, although I pretty much know what field names are key words and will need it.

    -- hugh
     
    georgie likes this.
  8. georgie

    georgie Member

    Level: Community
    Perfect, thank you!I note, "quoteName", OK.

    However, when discussing with the Acymailing support, it appears that the management of this field in Acymailing is useless when creating a new subscriber, and that the value of this field will be automatically created by the extension when necessary.
    Which makes even easier to do what I want to do. I am going to modify my doc accordingly.

    Thank you

    Georges
     
  9. cheesegrits

    cheesegrits Support Gopher Staff Member

    Level: Community
    Ah, OK. You didn't mention Acymailing before.

    -- hugh
     
    georgie likes this.
  10. georgie

    georgie Member

    Level: Community

Share This Page