increment in new row

Questionsincrement in new row
Daniel Mera asked 6 years ago

When I add a new row to the grid, in one of my fields which is number, im trying to auto-increment it using the last number I had in the last row, How can i get that last row value and how can i set it to be a defaultValue.

Thanx for the help.
Dan

1 Answers
Abu Ghufran answered 6 years ago

Hello,

For that, you need to use 'on_insert' event for a custom callback function.

In that function callback, you can have a select sql, that will fetch last id from database, and set an incremented id in the field before actual insert takes place.

// params are array(<function-name>,<class-object> or <null-if-global-func>,<continue-default-operation>)
// if you pass last argument as true, functions will act as a data filter, and insert/update will be performed by grid

$e["on_insert"] = array("add_client", null, true);
$grid->set_events($e);

function add_client($data)
{
// fetch last id using mysql_query, be it in var $new_id

// now just set it for database insert
$data["params"]["myfield"] = $new_id;
}

Your Answer

6 + 8 =

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 / 5. Vote count:

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

Let us improve this post!