I have a table "Table1" In this table is a field with a numeric value. Units. Current Value is 100. I have another table "Table2" that is a child of Table one. I have a field in the table called "UnitsUsed". I need to have the field in "Table1" updated and subtract the amount in "UnitsUsed" and the display the new amount.
Not sure how to start. Can someone assist me please?
Math/Update field after record is saved.
Re: Math/Update field after record is saved.
Hi,
you can do this in the hooks/<Table2>.php file. There you will find the functions <table2>_after_insert and <table2>_bevor_update and <table2>_after_update and <table2>_after_delete.
The data array has the values from your detailview. So update units in table1 via the sqlvalue function.
Something like
sqlvalue("UPDATE table1 set Units=Units-".$data['UnitsUsed']." where ### PUT HERE your matching between table1 and table2 ### = ". $data['' ## PUT here your table2 reference to table1 from your form ###'] );
for the insert.
Make sure your logic covers all aspects which can occure, especially if you allow updates.
you can do this in the hooks/<Table2>.php file. There you will find the functions <table2>_after_insert and <table2>_bevor_update and <table2>_after_update and <table2>_after_delete.
The data array has the values from your detailview. So update units in table1 via the sqlvalue function.
Something like
sqlvalue("UPDATE table1 set Units=Units-".$data['UnitsUsed']." where ### PUT HERE your matching between table1 and table2 ### = ". $data['' ## PUT here your table2 reference to table1 from your form ###'] );
for the insert.
Make sure your logic covers all aspects which can occure, especially if you allow updates.
Any help offered comes with the best of intentions. Use it at your own risk. In any case, please make a backup of your existing environment before applying any changes.
Re: Math/Update field after record is saved.
Ok so here is a screen shot of my two lists. Here is a copy of the command I entered into the Hooks file:
function BHISNotes_after_insert($data, $memberInfo, &$args){
sqlvalue("UPDATE Clients set BHISUnits=BHISUnits-".$data['UnitsUsed']." where client = ". $data['fullname'] );
return TRUE;
}
After I save the screen for the data entry seems to hang and when I click the X no math has been performed.
function BHISNotes_after_insert($data, $memberInfo, &$args){
sqlvalue("UPDATE Clients set BHISUnits=BHISUnits-".$data['UnitsUsed']." where client = ". $data['fullname'] );
return TRUE;
}
After I save the screen for the data entry seems to hang and when I click the X no math has been performed.
- Attachments
-
- 2018-07-23 (4).png (17.42 KiB) Viewed 6074 times
Re: Math/Update field after record is saved.
Hi,
can you try
$data['fullname'] is not available.
can you try
Code: Select all
sqlvalue("UPDATE Clients set BHISUnits=BHISUnits-".$data['UnitsUsed']." where id = ". $data['Client'] );
Any help offered comes with the best of intentions. Use it at your own risk. In any case, please make a backup of your existing environment before applying any changes.
Re: Math/Update field after record is saved.
Yes this work great. Next small but important question - How can I make the BHISUnits field in the first table read only to all users but admins? And if this is possible would it affect this hook from updating the field value?
Re: Math/Update field after record is saved.
Hi,
you can use this in the hooks/Clients.php
And it does not affect the other hook.
you can use this in the hooks/Clients.php
Code: Select all
function Clients_dv($selectedID, $memberInfo, &$html, &$args){
/* current user is not an admin? */
if($memberinfo['group'] != 'Admins'){
$html .= <<<EOC
<script>
\$j(function(){
\$j('#BHISUnits').prop('disabled', true);
})
</script>
EOC;
}
Any help offered comes with the best of intentions. Use it at your own risk. In any case, please make a backup of your existing environment before applying any changes.
Re: Math/Update field after record is saved.
I added the code as shown and when I browse to the page I get the following Error:
This page isn’t working
localhost is currently unable to handle this request.
HTTP ERROR 500
Attached is my clients.php
This page isn’t working
localhost is currently unable to handle this request.
HTTP ERROR 500
Attached is my clients.php
- Attachments
-
- 2018-07-24.png (64.66 KiB) Viewed 6042 times
Re: Math/Update field after record is saved.
Hi,
you need to add an addtional closing bracket } to end the Clinets_dv function.
So after EOC; you need } }
you need to add an addtional closing bracket } to end the Clinets_dv function.
So after EOC; you need } }
Any help offered comes with the best of intentions. Use it at your own risk. In any case, please make a backup of your existing environment before applying any changes.
Re: Math/Update field after record is saved.
I did catch that. Does it matter that the field BHISUnits is an Integer? " \$j('#BHISUnits').prop('disabled', true); "
Re: Math/Update field after record is saved.
No, why?
Any help offered comes with the best of intentions. Use it at your own risk. In any case, please make a backup of your existing environment before applying any changes.
Re: Math/Update field after record is saved.
Well I added the closing bracket but I am still getting the same error
- Attachments
-
- 2018-07-25.png (116.66 KiB) Viewed 6017 times
Re: Math/Update field after record is saved.
Hi,
I see the function definition 2 times in your screenshot, remove one pls.
I see the function definition 2 times in your screenshot, remove one pls.
Any help offered comes with the best of intentions. Use it at your own risk. In any case, please make a backup of your existing environment before applying any changes.
Re: Math/Update field after record is saved.
OK, I am so sorry this isn't working, I am sure it is something I must be doing. I have deleted the section and re added. Attached is the whole hooks file. Thank you for your help and patience. I am just learning and appreciate your help!
- Attachments
-
- 2018-07-25 (1).png (201.75 KiB) Viewed 6013 times
Re: Math/Update field after record is saved.
Sorry, but you still have the function defintion twice.
I re-add the code here as it should be:
make sure function Clients_dv exists only once.
I re-add the code here as it should be:
Code: Select all
function Clients_dv($selectedID, $memberInfo, &$html, &$args){
/* current user is not an admin? */
if($memberinfo['group'] != 'Admins'){
$html .= <<<EOC
<script>
\$j(function(){
\$j('#BHISUnits').prop('disabled', true);
})
</script>
EOC;
}
}
Any help offered comes with the best of intentions. Use it at your own risk. In any case, please make a backup of your existing environment before applying any changes.