how to talior made the code no by auto increase field

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

how to talior made the code no by auto increase field

Post by peoplecreative »

hi, i am the beginner.
table:
id int(2)
code vchar(10)

i want to get the value of field of id, that is an auto increase field.
then insert to code with some prefix value, for example:
id = 18
code = ABC0018DEF

HOW I CAN DO IN PHPMAKER, and where can add this code?
much thanks.


scs
User
Posts: 694

Post by scs »

In Row_Inserted,
$new_added_id = $GLOBALS["conn"]->Insert_ID();
$code = $your_prefix . sprintf("%04d", $new_added_id) . $your_suffix;
ew_Execute(".... perform update here with where condition ...");


peoplecreative
User
Posts: 24

Post by peoplecreative »

thanks first....

How I can refer and get the value from the form current value in FUNCTION Row_inserted?
Because I find the field values are all empty.

Table:
rsa_certificate_id INT(2) Auto Increase
rsa_policy_id vchar(10)
rsa_certificate_no vchar (10)

Data:
rsa_policy_id = 'SDCPG14000123T'

Expectation:
When Add a NEW record, using NEW rsa_certificate_id (for example = 13) and grap the current value of FORM field of rsa_policy_id (Dropbox)
then I want to write a code that can combine
1) the string of rsa_policy_id from first 5 to last 2, which is 14000123
2) new rsa_certificate_id, which is 13
to new String 140001230013, and then insert into the record, so I try to write the following code.

function Row_Inserting($rsold, &$rsnew) {
// Enter your code here
$cert_no = substr($this->rsa_policy_id->CurrentValue, 5, 8) . sprintf("%04d", $this->rsa_certificate_id->CurrentValue);
$this->rsa_certificate_no->SetDbValueDef($rsnew, $cert_no, "", FALSE);
// To cancel, set return value to FALSE
return TRUE;


mobhar
User
Posts: 11755

Post by mobhar »

Please note that there is no "CurrentValue" context in "Row_Inserting" server event. Use "$rsnew" instead. Or, you may consider to use @scs' suggestion above.


peoplecreative
User
Posts: 24

Post by peoplecreative »

thanks, because adding new ID has already done in function AddRow($rsold = NULL), I can't do it again.
But I also need to get the value of the form, how can I do with "$rsnew"


peoplecreative
User
Posts: 24

Post by peoplecreative »

It does not work even change to $rsnew:
$cert_no = $rsnew["rsa_policy_id"] . $rsnew["rsa_certificate_id"];
$this->rsa_certificate_no->SetDbValueDef($rsnew, $cert_no, "", FALSE);

===========
Table:
rsa_certificate_id INT(2) Auto Increase
rsa_policy_id vchar(10)
rsa_certificate_no vchar (10)

Expectation:
When Add a NEW record, using NEW rsa_certificate_id (for example = 13) and grap the current value of FORM field of rsa_policy_id (Dropbox)
then I want to write a code that can combine
1) the string of rsa_policy_id from first 5 to last 2, which is 14000123
2) new rsa_certificate_id, which is 13
to new String 140001230013, and then insert into the record, so I try to write the following code.


danielc
User
Posts: 1601

Post by danielc »

Please note that you can get your autoincrement field's value in Row_Inserted server event but not Row_Inserting server event. Write your sql to update the record (use ew_Execute). So, follow as scs suggested:

scs wrote:
In Row_Inserted,$new_added_id = $GLOBALS["conn"]->Insert_ID();$code = $your_prefix
. sprintf("%04d", $new_added_id) . $your_suffix;ew_Execute(".... perform update
here with where condition ...");

See example (ew_Execute) on Server event and Client script in help file.


peoplecreative
User
Posts: 24

Post by peoplecreative »

yes, really much thanks.

Besides auto increase field, I don't how to how to grap the value of current field of dropbox.
the sql to update need to combine auto increase field & the form field value ($rsnew["rsa_policy_id"] but i found it is NULL) in Row_Inserted


scs
User
Posts: 694

Post by scs »

Change $code to $rsnew["rsa_policy_id"] and then echo it.

Your will see the result for $rsnew["rsa_policy_id"] (but value might not update to table)

I think you don't really understand the concept of Row_Inserted($rsold, &$rsnew).

Read 'Server Events and Client Scripts' to get more info.


peoplecreative
User
Posts: 24

Post by peoplecreative »

// Row Inserted event
function Row_Inserted($rsold, &$rsnew) {
//echo "Row Inserted"
ew_Execute ("UPDATE rsa_policy_plan_certificate SET rsa_policy_plan_certificate.rsa_certificate_no = (SELECT SUBSTRING(rsa_policy_master.rsa_policy_no,8,6) FROM rsa_policy_master
INNER JOIN rsa_policy_plan_certificate ON rsa_policy_master.rsa_policy_id =
rsa_policy_plan_certificate.rsa_policy_id WHERE rsa_policy_plan_certificate.rsa_certificate_id=" . $rsnew["rsa_certificate_id"] . ")
WHERE rsa_policy_plan_certificate.rsa_certificate_id=" . $rsnew["rsa_certificate_id"]);
}

======================================
Failed to execute SQL. Error: Duplicate entry '' for key 'rsa_certificate_no'


danielc
User
Posts: 1601

Post by danielc »

You can not have duplicate value for the key. Use var_dump to check your select statement for rsa_certificate_no. See php.net/manual/en/function.var-dump.php.


Post Reply