set_events not working for on_insert

Questionsset_events not working for on_insert
santosh asked 9 years ago

Hi,

The code below updates the date when edited, so on_update is working correctly. However no update the fields remain blank. Can you please help?

<?php
/**
* PHP Grid Component
*
* @author Abu Ghufran <[email protected]> – http://www.phpgrid.org
* @version 1.5.2
* @license: see license.txt included in package
*/

// include db config
include_once("../config.php");

// set up DB
mysql_connect(PHPGRID_DBHOST,PHPGRID_DBUSER,PHPGRID_DBPASS);
mysql_select_db(PHPGRID_DBNAME);

// include and create object
include(PHPGRID_LIBPATH."inc/jqgrid_dist.php");

$g = new jqgrid();

// set few params
$grid["caption"] = "Displaying Ozone Status";
//$grid["shrinkToFit"] = false;
$grid["autowidth"] = true; // expand grid to screen width
//$grid["width"] = "1200";
$grid["multiselect"] = true;
$grid["ignoreCase"] = true; // do case insensitive sorting
$grid["altRows"] = true;
$grid["altclass"] = "myAltRowClass";

// export XLS file
// export to excel parameters
$grid["export"] = array("format"=>"pdf", "filename"=>"my-file", "sheetname"=>"test");

$g->set_options($grid);

$e["on_insert"] = array("add_modifier", null, true);
$e["on_update"] = array("update_modifier", null, true);
$g->set_events($e);

function update_modifier($data)
{
require("../init.php");

mysql_query("UPDATE p_ozone SET modifyDate= DATE_FORMAT(NOW(),'%Y-%m-%d %H:%i:%s') WHERE id = {$data["id"]}");
mysql_query("UPDATE p_ozone SET modifier= ' " . $user->name . " ' WHERE id = {$data["id"]}");

}

function add_modifier($data)
{
require("../init.php");
mysql_query("UPDATE p_ozone SET modifyDate= DATE_FORMAT(NOW(),'%Y-%m-%d %H:%i:%s') WHERE id = {$data["id"]}");
mysql_query("UPDATE p_ozone SET modifier= ' " . $user->name . " ' WHERE id = {$data["id"]}");
}

$g->set_actions(array(
"add"=>true, // allow/disallow add
"edit"=>true, // allow/disallow edit
"delete"=>false, // allow/disallow delete
"rowactions"=>true, // show/hide row wise edit/del/save option
"export"=>false, // show/hide export to excel option
"autofilter" => true, // show/hide autofilter for search
"search" => "advance" // show single/multi field search condition (e.g. simple or advance)
)
);

// this db table will be used for add,edit,delete
$g->table = "p_ozone";

$cols = array();

// you can customize your own columns …
$col = array();
$col["name"] = "id";
$col["title"] = "id";
$col["width"] = "40"; // width on grid
if (editGrid != 'NO'){
$col["editable"] = true;
}
$cols[] = $col;

$col = array();
$col["name"] = "Phase";
$col["title"] = "Phase";
$col["width"] = "40"; // width on grid
if (editGrid != 'NO'){
$col["editable"] = true;
}
$cols[] = $col;

$col = array();
$col["name"] = "Atoll_ID";
$col["title"] = "Atoll_ID";
$col["width"] = "60"; // width on grid
if (editGrid != 'NO'){
$col["editable"] = true;
}
$cols[] = $col;

$col = array();
$col["name"] = "Site_Name";
$col["title"] = "Site_Name";
if (editGrid != 'NO'){
$col["editable"] = true;
}
$cols[] = $col;

$col = array();
$col["name"] = "Ozone_Site_ID";
$col["title"] = "Ozone_Site_ID";
$col["width"] = "80"; // width on grid
if (editGrid != 'NO'){
$col["editable"] = true;
}
$cols[] = $col;

$col = array();
$col["name"] = "O2_Site_ID";
$col["title"] = "O2_Site_ID";
$col["width"] = "80"; // width on grid
if (editGrid != 'NO'){
$col["editable"] = true;
}
$cols[] = $col;

$col = array();
$col["name"] = "H3G_Site_ID";
$col["title"] = "H3G_Site_ID";
$col["width"] = "80"; // width on grid
if (editGrid != 'NO'){
$col["editable"] = true;
}
$cols[] = $col;

$col = array();
$col["title"] = "status";
$col["name"] = "Status";
$col["search"] = true;
$col["width"] = "80"; // width on grid
if (editGrid != 'NO'){
$col["editable"] = true;
}
$col["edittype"] = "select"; // render as select
# fetch data from database, with alias k for key, v for value
$col["editoptions"] = array("value"=>":;Dropped:Dropped;Ozone:Ozone;Ozoned:Ozoned;Not Ozoned:Not Ozoned");
//$col["formatter"] = "select"; // display label, not value
$cols[] = $col;

$col = array();
$col["name"] = "modifier";
$col["title"] = "Modifier";
$col["editable"] = false;
$cols[] = $col;

$col = array();
$col["name"] = "modifyDate";
$col["title"] = "modifyDate";
$col["editable"] = false;
$col["width"] = "100"; // width on grid
$cols[] = $col;

// pass the cooked columns to grid
$g->set_columns($cols);

// generate grid output, with unique grid name as 'list1'
$out = $g->render("list1");
?>

6 Answers
Abu Ghufran answered 9 years ago

Can you explain little more about your statement:
"However no update the fields remain blank"

santosh answered 9 years ago

When I add a record. The modifier and date field are not updated. They remain blank. When I edit a field the date updates

Abu Ghufran answered 9 years ago

On insert event, the $data[id] value of record is not available as it is called before insert sql.
You can use on_after_insert, and it should work as expected.

$e["on_after_insert"] = array("add_modifier", null, true);

santosh answered 9 years ago

Thanks, that works for insert now.

In the above code I am also updating two fields modifier and modify date when any field is edited. I am using function update_modifier($data) for this.

The function modifies these fields but new values show only when the grid is refreshed manually. Is there any way to update these two fields when any other field is updated?

Abu Ghufran answered 9 years ago

As it is showing updated values after refresh, this means it's being updated in db.

To force inline edit do a complete refresh, you can set:

$grid["reloadedit"] = true;
$g->set_options($grid);

santosh answered 9 years ago

works perfect! thanks

Your Answer

5 + 12 =

Login with your Social Id:

OR, enter

Attach code here and paste link in question.
Attach screenshot here and paste link in question.



How useful was this discussion?

Click on a star to rate it!

Average rating 0 / 5. Vote count: 0

No votes so far! Be the first to rate it.

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?