Using form ID with Calc element

emcguire

Member
I thought I had this working, but it's not working now for some reason. I have a calc element that should be looking at the form id and simply adding 100 to it. so if the ID of the form is 27 - the calc should return 127.


my calc is :

PHP:
return (int) '{nab_trade_forms___id_raw}' + 100;

I also tried it with no "raw" and also tried it with {rowid} , also tried it with creating a $myCalc variable just like the doc example

no matter what I do - it returns "100" as the value. It's like it can't see the form ID? I've read the docs on this, I'm pretty sure my syntax in correct.

settings are

Only Calc On Save = Yes
Ajax Calc = no
Calc on Load = No

If I "edit" the form after I submit it, the calc then calculates and puts in the right number, Can it not work with the form ID on submission?
 
You don't have a rowid in new records, it's only created during (after) submission.
(Imagine two users opening a new form at the same time...)
 
So I'm assuming then that this element calculates before submission? So there's no way to do what I'm trying to do and I should prolly try another way.
 
Well, the calc element runs during submission. But the rowid is only known AFTER submission, once the data has already been written to the database. The rowid is the value of the auto-incrementing primary key on your table, which is only assigned as the row is written to the database.

So you'll have to do this in a form PHP plugin, running onAfterProcess, and update the field manually with your own query. So just use a simple field rather than a calc, and do something like ...

Code:
$myDb = JFactory::getDbo();
$myQuery = $myDb->getQuery(true);
$myQuery
   ->update('yourtable');
   ->set('yourfield = id + 100')
   ->where('id = ' . $myDb->quote($formModel->formData['rowid']));
$myDb->setQuery($myQuery);
// remove this line after testing
var_dump((string) $myQuery); exit;
$myDb->execute();

Run it once an sanity check that query (it'll just produce an otherwise blank page with the query on it), and assuming the query looks good, remove the var_dump line and run it again.

-- hugh
 
We are in need of some funding.
More details.

Thank you.

Members online

Back
Top