Use an array to store field(s) dynamically from Form to table

I have some code that I use in a form to perform a Save of the current record. I chose this method as it gives me total flexibility:

//* 01.00.000 10/26/2018 07/12/2019 Initial release of this Event Code Block

//***************************************************************************************************
//* UPDATE GLOBAL RECORD POINTER VARIABLES *
//***************************************************************************************************
[var_iRecordNumber] = {Field_CodeSequence};

//****************************************************************************************************
//* PREPARE FORM FIELD(S) FOR TABLE RECORD INSERT *
//****************************************************************************************************
{ID} = {Field_ID};
{CodeSequence} = {Field_CodeSequence};
{DateAdded} = {Field_DateAdded};
{DateModified} = {Field_DateModified};
{Name} = {Field_Name};
{Description} = {Field_Description};

//***************************************************************************************************
//* INSERT NEW RECORD INTO TABLE *
//***************************************************************************************************
// SQL Statement Parameters
$insert_table = [var_sProjectAppTable]; // Table name
$insert_fields = array( // Field list array
‘DateAdded’ => “’{DateAdded}’”,
‘DateModified’ => “’{DateModified}’”,
‘CodeSequence’ => “’{CodeSequence}’”,
‘Name’ => “’{Name}’”,
‘Description’ => “’{Description}’”,
);

// Build SQL INSERT Statement
$insert_sql = ‘INSERT INTO ’ . $insert_table
. ’ (’ . implode(’, ‘, array_keys($insert_fields)) . ‘)’
. ’ VALUES (’ . implode(’, ', array_values($insert_fields)) . ‘)’;

// Execute SQL Statement
sc_exec_sql($insert_sql);

//****************************************************************************************************
//* MESSAGE PROMPT TO DISPLAY *
//****************************************************************************************************
[var_oFormPropertyArray][‘FormMessagePrompt’][0] = “RecordInserted”;

//****************************************************************************************************
//* SET FORM STATE TO “VIEW” *
//****************************************************************************************************
[prop_sFormStateEditMode] = “View”;
[var_oFormPropertyArray][‘FormnStateEdit’] = [prop_sFormStateEditMode];

//****************************************************************************************************
//* START THE SELECTED APPLICATION *
//****************************************************************************************************
sc_redir([var_sProjectAppName] . “_Controller.php”, CodeSequence=[var_iRecordNumber], “_self”);


Is there a way I could use an array for the two block:
a) PREPARE FORM FIELD(S) FOR TABLE RECORD INSERT
b) INSERT NEW RECORD INTO TABLE

In particular the first block (a). If I can use an array for this then this code can be dynamically created saving time. Yes I could have Scriptcase do this, but there is a specific reason I want to control this portion of processing. And then I would like to use (b) to update the database. The thing is getting the values directly from the form fields into the array I would use in (a) above.

Could anyone show me an example??

Basic idea would be:
//****************************************************************************************************
//* PREPARE FORM FIELD(S) FOR TABLE RECORD INSERT *
//****************************************************************************************************
foreach(oFieldArray as oFields)
{
}

This loop would read the fields and their values from the form…

{ID} = {Field_ID};
{CodeSequence} = {Field_CodeSequence};
{DateAdded} = {Field_DateAdded};
{DateModified} = {Field_DateModified};
{Name} = {Field_Name};
{Description} = {Field_Description};

//***************************************************************************************************
//* INSERT NEW RECORD INTO TABLE *
//***************************************************************************************************
foreach(oFieldTableArray as sField)
{
}
// SQL Statement Parameters
$insert_table = [var_sProjectAppTable]; // Table name

This loop would generate the code for the array below…
$insert_fields = array( // Field list array
‘DateAdded’ => “’{DateAdded}’”,
‘DateModified’ => “’{DateModified}’”,
‘CodeSequence’ => “’{CodeSequence}’”,
‘Name’ => “’{Name}’”,
‘Description’ => “’{Description}’”,
);

I can use Global variables, but I’d rather use a dynamic array if possible.

I’d really appreciate it. :slight_smile: