donelson07,
First of all, the above code won't work if the user is allowed to delete rows in the grid, nor will it work if the user is adding rows or the value of the ID field is autogenerated by MySQL. It also doesn't maintain the order of the rows in the grid.
Here is the trigger code that you should use to write grid to a table:
Code: Select all$dbConnection = 'workflow'; //set to ID of the DB Connection if using an external DB
$tableName = 'PMT_YOUR_TABLE'; //set to table name
// first clear the entire table:
executeQuery("DELETE FROM $tableName", $dbConnection);
//then write grid to the table:
foreach (@=yourGrid as $row) {
$id = $row['ID']; //assuming that the ID is an integer.
$firstName = mysql_real_escape_string($row['FIRST_NAME']);
$lastName = mysql_real_escape_string($row['LAST_NAME']);
$email = mysql_real_escape_string($row['EMAIL']);
//if a new row and MySQL should autogenerate the ID field:
if (empty($id) and $id !== '0' and $id !== 0) {
$sql = "INSERT INTO $tableName (FIRST_NAME, LAST_NAME, EMAIL) ".
"VALUES ('$firstName', '$lastName', '$email')";
}
else {
$sql = "INSERT INTO $tableName (ID, FIRST_NAME, LAST_NAME, EMAIL) ".
"VALUES ($id, '$firstName', '$lastName', '$email')";
}
@@result = executeQuery($sql, $dbConnection);
}
If you only have a 100 rows in your table, then you probably should worry about it, because MySQL is fast enough. If you have thousands, then it might be an issue, but there is no way to maintain the same order of rows in the grid as in the table. Is this important to you?
If you are really ordered about having to rewrite the entire table, then here is an alternative. What you can do is create a trigger before the DynaForm with the grid like this to create a copy of the grid, with keys based on the ID field:
Code: Select all@=OriginalGridById = array();
foreach (@=yourGrid as $row) {
//assuming that the ID field is unique.
@=OriginalGridById [ $row['ID'] = $row;
}
Then, create a second trigger after the DynaForm to compare the original grid with the updated grid and write any changes to the DB:
Code: Select all$dbConnection = 'workflow'; //set to ID of the DB Connection if using an external DB
$tableName = 'PMT_YOUR_TABLE'; //set to table name
@=OriginalGridById = array();
foreach (@=yourGrid as $row) {
//assuming that the ID field is unique.
@=OriginalGridById [ $row['ID'] = $row;
}
for ($i = 1; $i <= count(@=yourGrid); $i++) {
$row = @=yourGrid[$i];
$id = $row['ID'];
//if an existing row
if (key_exists($id, @=OriginalGridById)) {
//if the row has been changed, then update it in the database:
if (@=OriginalGridById[$id] != $row) {
$firstName = mysql_real_escape_string($row['FIRST_NAME']);
$lastName = mysql_real_escape_string($row['LAST_NAME']);
$email = mysql_real_escape_string($row['EMAIL']);
$sql = "UPDATE $tableName SET FIRST_NAME='$firstName, LAST_NAME='$lastName', EMAIL='$email' ";
@@result = executeQuery($query, $dbConnection);
}
}
else { //if a new row was added to the grid:
$firstName = mysql_real_escape_string($row['FIRST_NAME']);
$lastName = mysql_real_escape_string($row['LAST_NAME']);
$email = mysql_real_escape_string($row['EMAIL']);
//if ID field is empty and MySQL should autogenerate it:
if (empty($id) and $id !== '0' and $id !== 0) {
$sql = "INSERT INTO $tableName (FIRST_NAME, LAST_NAME, EMAIL) ".
"VALUES ('$firstName', '$lastName', '$email')";
}
else {
$sql = "INSERT INTO $tableName (ID, FIRST_NAME, LAST_NAME, EMAIL) ".
"VALUES ($id, '$firstName', '$lastName', '$email')";
}
@@result = executeQuery($sql, $dbConnection);
}
}
The problem with this code is that it doesn't handle the situation if the user can delete rows in the grid. Then you have no idea that the row was deleted, so you don't know to delete that record in the database. The solution if you don't want to delete the entire table and rewrite it, is to add javascript to the onDeleteRow event to record the IDs of the deleted rows and store them in a hidden field. Then add code to your trigger to delete those rows.