HOW TO make another operation after adding/updating etc.?

Talk and info about common issues

Moderator: alexandrleonenko

administrator
Site Admin
Site Admin
Posts: 6011
Joined: Jan 7th, '09, 23:18
Contact:

HOW TO make another operation after adding/updating etc.?

Postby administrator » Mar 21st, '09, 10:56

Sometimes we need to make another operation after we've successfully added a new row, updated existing record or deleting them.
How can we do it? It's very simply!

1st way.
-------
You can always rewrite AddRow(), UpdateRow() and DeleteRow() methods in DataGrid class according to your needs.


2nd way.
--------
For example: we have a table, called tasks (many tasks for one project) and after we've add a new task we want to update a project_schedule table witch contains a rows with project_id and task_id (this table links projects with tasks). We simply add next rows after $dgrid->bind();

Lets see how it works:

First of all you need to receive mode and record ID:
Remember you need to use prefix_ of your DataGrid, defined in $unique_prefix = "prefix_";

Code: Select all

// add new row into project_schedule table
$mode = (isset($_REQUEST[$unique_prefix.'mode'])) ? $_REQUEST[$unique_prefix.'mode'] : '';
$rid = (isset($_REQUEST[$unique_prefix.'rid'])) ? $_REQUEST[$unique_prefix.'rid'] : '';
$pid = (isset($_REQUEST[$unique_prefix.'pid'])) ? $_REQUEST[$unique_prefix.'pid'] : '';


Now find this line of code

Code: Select all

$dgrid->Bind();


and put after that line next code:

Code: Select all

if(($mode == "update") && ($rid == "-1") && $dgrid->IsOperationCompleted()){
   $sql = "INSERT INTO project_schedule (project_id, task_id) VALUES (".$pid.",".$dgrid->rid.") ";
   mysql_query($sql);
   if(mysql_insert_id() > 0){
        // it's OK
   }else{
       echo "Error!";
   }
}


and now for updating operation:

Code: Select all

if(($mode == "update") && ($rid != "-1") && $dgrid->IsOperationCompleted()){
     // your operation here...
}


and for deleting:

Code: Select all

if(($mode == "delete") && $dgrid->IsOperationCompleted()){
     // your operation here...
}


---------------------------------------
TIP: Sometimes, If you make additional operation with datagrid, you need to refresh the page to display it with updated data.
We can do it in some ways.

1. You just add JavaScript code after all additional operations:
echo "<script> document.location.href='image_grid.php?img_mode=add'; </script>";

2. You write

A.
// handle all actions, but don't show datagrid
$dgrid->bind(false);

B.
then you write your own operations:
add, delete, update or anything you want

C.
// refresh the page (show updated datagrid)
$dgrid->bind(true);

Return to “ApPHP DataGrid {HOW TO}”