Skip to main content
Topic: How To Set a Field Value Based on Another From Table (Read 1831 times) previous topic - next topic

How To Set a Field Value Based on Another From Table

Hello,
I want to set a field value on a Page, Before Add, and it's not working well. I know am missing something.

The field 'unitid' on the page is to be set to max(unitid)+1 from table 'deptunits' where 'dept' in 'deptunits' = current 'dept' specified on the page.
My current codes are:

$res = $db->rawQuery("SELECT * FROM deptunits WHERE dept='$dept' ORDER BY UnitID DESC LIMIT 1");
$modeldata['UnitID'] = $res['UnitID'] + 1;

I need current syntax that could help achieve this on the Before Add event.

BR


Re: How To Set a Field Value Based on Another From Table

Reply #2
Helllo robertr,

Thanks for your reply, am using MySQL and I use auto increment well.

This is however different. The field unitid counts from 1 grouped by dept. That is, for each dept it starts from 1 so in the table you would have:
DEPT, Unitid
ADMIN, 1
ADMIN, 2
ACCT, 1
ACCT, 2
ACCT, 3
SALES, 1
SALES, 2
SALES, 3
etc.