Inserting random number into db field

This public forum is for user-to-user discussions of PHPMaker. Note that this is not support forum.
Post Reply
cdmak
User
Posts: 27

Inserting random number into db field

Post by cdmak »

Hello,

For some reason I just can't get some thing simple to work on "Row_Inserting" I'm trying to generate a random number and insert it into the field "idnumber" Do you have to set the fields a certain way and have the Add checkbox checked. I don't want the idnumber field to show and let the user put something in it, I want the random number inserted behind the scenes with the user unaware.

$idnumber = rand();
$rsnew["idnumber"] = "$idnumber";

Also on "Row_Updated" I can't get the new variable to insert
$x = "18";
$rsnew["total_time"] = "$x";

This is for a calculated total price variable
$total_price = "18.00";
$rsnew["total_price"] = "$total_price";

Thank You


oahmed
User
Posts: 7

Post by oahmed »

Hi,

Try this,

  1. Select the table you want to update from the table pane;
  2. In the Client scripts ->Table-Specific->Startup Script->Add/Copy page
    write this:
    document.getElementById('x_idnumber').style.visibility = "hidden";
    This will hide the idnumber field from the user at the start up page and the user won't be able to type in the id number

To generate a random number go to the Server Events->Table-Specific->Add/Copy page->Form_CustomValidate and put it this

function Form_CustomValidate(&$CustomError) {
// Return error message in CustomError

  $GLOBALS["YOUR_TABLENAME_HERE"]->idnumber->CurrentValue = time() . rand(10,100);
  
return TRUE;

}

The time().rand(5,100) uses the current time to generate random number for the idnumber

Hope this helps

Best,
O.


cdmak
User
Posts: 27

Post by cdmak »

Hello oahmed,

Thank you very much, I was able to hide the fields, but the random number does not work.

Isn't the Row_Inserting where I would put this, I'm a little confused because I read the manual and that said you could do calculations there before inserting the value into the database.

$idnumber = rand();
$rsnew["idnumber"] = "$idnumber";

It does not work even work if I assign a hard coded value to the variable.

$idnumber = "12345678";
$rsnew["idnumber"] = "$idnumber";

Thank You


oahmed
User
Posts: 7

Post by oahmed »

The random number works for me

Hi,

As the rand() method reads, time().rand(10,100). Please make sure your data type for the idnmumber is integer and its length is 10 or more.
You can maybe change the length of the rand(10,100) to any length you want provided that your data type's length is same or higher than the parameter in the rand() method

I recommend that you put it in the Form_CustomValidate in the Add/Copy Page of the Table-Specific (Server Events)

function Form_CustomValidate(&$CustomError) {
// Return error message in CustomError

$GLOBALS["YOUR_TABLENAME_HERE"]->idnumber->CurrentValue = time() . rand(10,100);

return TRUE;
}

Best, O.


cdmak
User
Posts: 27

Post by cdmak »

Thank you again oahmed,

Looking at your code I figured out what I was doing wrong. Please disregard my previous post, works perfect now.

// Row Inserting event
function Row_Inserting($rsold, &$rsnew) {
// Enter your code here
$idnumber = rand();
$rsnew["idnumber"] = "$idnumber";
// To cancel, set return value to FALSE
return TRUE; // MAKE SURE YOU PUT THIS LINE AT THE END OF YOUR CODE OR IT WILL NOT WORK!!!
}


Post Reply