Questions and discussion about developing processes and programming in PHP, JavaScript, web services & REST API.
Forum rules: Please search to see if a question has already asked before creating a new topic. Please don't post the same question in multiple forums.
#795666
generateCode() creates random numbers which means that it could generate duplicates. If you want unique numbers, you need to know all the numbers that have previously been generated before so you can avoid it. You will have to save every number ever generated to a PM Table and check each time whether the number already exists in the table. The easier solution is to just keep a counter and increment by 1 each time, but that doesn't work if you want the numbers to look like they are random.

If keeping a global counter:
Create a PM Table "COUNTER" with the field NUM of type INT(12).
Then add one row to the table with the value of NUM set to 0.

Then use this trigger code in your process:
Code: Select all
$sql = "SELECT NUM FROM PMT_COUNTER";
$aResult = executeQuery($sql);
@#num = $aResult + 1;

//write the new number to the database:
$sql = "UPDATE PMT_COUNTER SET NUM=" . @#num;
executeQuery($sql);
If you want your number to appear random then you can use trigger code like this to generate the number and verify that number hasn't been generated before:
Code: Select all
do {
    //set to a large enough number of digits so you will never run out
    @@num = generateCode(5, 'NUMERIC'); 
    $sql = "SELECT NUM FROM PMT_COUNTER WHERE NUM=".@@num;
    $aResult = executeQuery($sql);
} while (count($aResult) > 0);

//add a new record in the table for the generated number:
$sql = "INSERT INTO PMT_COUNTER (NUM) VALUES (".@@num.")";
executeQuery($sql);
#816014
@@CaseCode variable Integer
my trigger either
@@CaseCode = generateCode(1, 'NUMERIC');
or
@@CaseCode = generateCode(10000, 'NUMERIC');

both makes 4 digits random numbers : 3029 , 0138 , 5123
is tested the 'Numeric' term seems working fine, but not the int size because it creates 4 digits. Any idea what did i miss?
:roll:

im going to create 2 digit integer (between 1-100) but when i make
@@CaseCode = generateCode(2, 'NUMERIC');
its still show 4 digit number
#816026
I checked the source code in the file gulliver/system/class.g.php and it sets a minimum of 4 digits:
Code: Select all
    public static function generateCode ($iDigits = 4, $sType = 'NUMERIC')
    {
        if (($iDigits < 4) || ($iDigits > 50)) {
            $iDigits = 4;
        }
        if (($sType != 'NUMERIC') && ($sType != 'ALPHA') && ($sType != 'ALPHANUMERIC')) {
            $sType = 'NUMERIC';
        }
        $aValidCharacters = array ('0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'
        );
        switch ($sType) {
            case 'NUMERIC':
                $iMin = 0;
                $iMax = 9;
                break;
            case 'ALPHA':
                $iMin = 10;
                $iMax = 35;
                break;
            case 'ALPHANUMERIC':
                $iMin = 0;
                $iMax = 35;
                break;
        }
        $sCode = '';
        for ($i = 0; $i < $iDigits; $i ++) {
            $sCode .= $aValidCharacters[rand( $iMin, $iMax )];
        }
        return $sCode;
    }
If you don't want this, then comment out the part that sets a limit in the number of characters, like this:
Code: Select all
    public static function generateCode ($iDigits = 4, $sType = 'NUMERIC')
    {
        //if (($iDigits < 4) || ($iDigits > 50)) {
        //    $iDigits = 4;
        //}
        if (($sType != 'NUMERIC') && ($sType != 'ALPHA') && ($sType != 'ALPHANUMERIC')) {
Want to create your own meme coin?

In the world of cryptocurrencies, a unique and exc[…]

The market for cryptocurrencies is demonstrating a[…]

What's SAP FICO?

Embarking on a dissertation can be one of the most[…]

Hello. For rental housing, there are software solu[…]