Unable to do a calculation

Discussions related to customizing hooks. Hooks are documented at http://bigprof.com/appgini/help/advanced-topics/hooks/
bambinou
Veteran Member
Posts: 163
Joined: 2013-02-01 15:09

Unable to do a calculation

Post by bambinou » 2013-10-21 22:09

Hello,

No idea if this is a bug in the 5.20 version but I cannot do a simple calculation in a hook file.

My table is called calculations
My fields are:
units
end_reading
start_reading

in the hook/calculations.php file I did:

Code: Select all

	function calculations_before_insert(&$data, $memberInfo, &$args){

	$data['calculations.units'] = $data['calculations.start_reading'] * $data['calculations.end_reading']; 
       
		return TRUE;
	}

This did not work so I tried:

Code: Select all


	function calculations_before_insert(&$data, $memberInfo, &$args){

	$data['units'] = $data['start_reading'] * $data['end_reading']; 
       
		return TRUE;
	}


This did not work either, any idea why please? I have followed the help files correctly I believe.


Thank you,

Ben

bambinou
Veteran Member
Posts: 163
Joined: 2013-02-01 15:09

Re: Unable to do a calculation

Post by bambinou » 2013-10-28 20:50

Hi,

If anyone has an idea on why this calulcation do not work, please let me know as I really cannot advance with this project. Thank you.

Code: Select all

	 * Called before executing the insert query.
	 * 
	 * @param $data
	 * An associative array where the keys are field names and the values are the field data values to be inserted into the new record.
	 * For this table, the array items are: 
	 *     $data['start_date'], $data['start_reading'], $data['end_date'], $data['end_reading'], $data['units'], $data['cost']
	 * $data array is passed by reference so that modifications to it apply to the insert query.
	 * 
	 * @param $memberInfo
	 * An array containing logged member's info.
	 * @see http://bigprof.com/appgini/help/working-with-generated-web-database-application/hooks/memberInfo
	 * 
	 * @param $args
	 * An empty array that is passed by reference. It's currently not used but is reserved for future uses.
	 * 
	 * @return
	 * A boolean TRUE to perform the insert operation, or FALSE to cancel it.
	*/

	function calculations_before_insert(&$data, $memberInfo, &$args){

            $data['units'] = $data['start_reading'] * $data['end_reading'];
            
		return TRUE;
	}



The field stay blank. No calculations are being done.

bambinou
Veteran Member
Posts: 163
Joined: 2013-02-01 15:09

Re: Unable to do a calculation

Post by bambinou » 2013-10-29 21:20

Hi everyone,

I have followed this example and cannot work out why the calculations do not work, the data passed in the function is correct:

http://www.bigprof.com/appgini/help/adv ... ore_insert


If someone could give me a little hint please.......

Thanks,


Ben

User avatar
a.gneady
Site Admin
Posts: 1281
Joined: 2012-09-27 14:46
Contact:

Re: Unable to do a calculation

Post by a.gneady » 2013-11-03 14:21

Hi Ben,

Is the "units" field set as read-only or hidden in the detail view? If so, you should unhide it and/or set it as editable ... the changes in $data array are applied to the database only if the field is editable .. If you want the field to be read-only and at the same time be able to modify it through hooks, you should modify it using an SQL update statment rather than through the $data array.
:idea: AppGini plugins to add more power to your apps:
  • DataTalk is an innovative AppGini plugin based on ChatGPT that allows you to interact with your AppGini database using natural language questions, without writing any SQL. Check the demo video
  • Mass Update plugin: Update multiple records at once and improve your workflow efficiency.
  • Check our other plugins and get a generous discount of up to 30% when buying 2 or more plugins.

bambinou
Veteran Member
Posts: 163
Joined: 2013-02-01 15:09

Re: Unable to do a calculation

Post by bambinou » 2013-11-03 21:50

Hi Ahmed,

Thank you for the reply.
I deleted my last example as I could not get it to work and recreated a new one with no fields hidden and everything set as edible.
All the fields are set to "decimal".

Here is the code:

Code: Select all

function cost_after_insert($data, $memberInfo, &$args){
        $data['total'] = $data['units'] * $data['cost'];
		return TRUE;
	}
Nothing happens at all. The $data['total'] shows zero even after entering 5 and 20 in the other 2 fields.

Are you sure there is not a problem with the 5.20 version?


Thank you,

Ben

AhmedBR
AppGini Super Hero
AppGini Super Hero
Posts: 327
Joined: 2013-09-19 10:23

Re: Unable to do a calculation

Post by AhmedBR » 2013-11-04 10:16

Enter the above in BEFORE_INSERT or use the following in after_insert and it should work:

Code: Select all

		$id=$data['selectedID'];
		$vtotal=$data[units']*$data['cost'];
		sql("update cost set total='$vtotal' where id='$id'");
Do not forget to correct id as per you database field.

I am using the calculation in 5.2 no problem.
Hope this helps
AppGini 22.14 - xampp 3.3.0 - PHP 7.4.30 - Summary reports - Calendar - Mass update - Messages - AppGiniHelper

bambinou
Veteran Member
Posts: 163
Joined: 2013-02-01 15:09

Re: Unable to do a calculation

Post by bambinou » 2013-11-04 13:36

Hi AhmedBR,

Thank you so much for this, I will give it a try tonight.
The method you are using is directly acting in the mysql column but I also wanted to just have this operation working in the php output(as a function) as explained in this example, or is this example wrong?
http://www.bigprof.com/appgini/help/adv ... ore_insert

Thank you,

Ben

AhmedBR
AppGini Super Hero
AppGini Super Hero
Posts: 327
Joined: 2013-09-19 10:23

Re: Unable to do a calculation

Post by AhmedBR » 2013-11-04 15:39

Yes, you can, and that is what I said, you should use your code in BEFORE INSERT

Code: Select all

function cost_before_insert($data, $memberInfo, &$args){
        $data['total'] = $data['units'] * $data['cost'];
      return TRUE;
   }
I am doing this in an invoice and works very well.
If I understood correctly the after insert you cannot do it because
Called after executing the insert query (but before executing the ownership insert query)
you see everything is already inserted, then your $data['total'] will always be ZERO, but if you do it BEFORE INSERT as I mentioned and as in the example:
In this example, let's assume that our table contains the fields: unit_price, quantity and total. We want to automatically calculate the value of the total field by multiplying quantity and unit_price.
function tablename_before_insert(&$data, $memberInfo, &$args){
$data['total'] = $data['quantity'] * $data['unit_price'];

return TRUE;}
GOT IT?
AppGini 22.14 - xampp 3.3.0 - PHP 7.4.30 - Summary reports - Calendar - Mass update - Messages - AppGiniHelper

bambinou
Veteran Member
Posts: 163
Joined: 2013-02-01 15:09

Re: Unable to do a calculation

Post by bambinou » 2013-11-04 21:41

Hi AhmedBR,

I am sorry but you have lost me. You are saying to put this code before_insert, but my example shows the before_insert:

Code: Select all

function cost_[b]before_insert[/b]($data, $memberInfo, &$args){
        $data['total'] = $data['units'] * $data['cost'];
      return TRUE;
   }
Thank you.

Ben

AhmedBR
AppGini Super Hero
AppGini Super Hero
Posts: 327
Joined: 2013-09-19 10:23

Re: Unable to do a calculation

Post by AhmedBR » 2013-11-04 22:49

In your message it is AFTER INSERT, take a look:
http://forums.appgini.com/phpbb/viewtop ... =817#p1772
AppGini 22.14 - xampp 3.3.0 - PHP 7.4.30 - Summary reports - Calendar - Mass update - Messages - AppGiniHelper

bambinou
Veteran Member
Posts: 163
Joined: 2013-02-01 15:09

Re: Unable to do a calculation

Post by bambinou » 2013-11-05 04:25

Hi AhmedBR,

Thank you for the reply.
Sorry, yes this is because I have tried to add it in another place but if you look at the top of the thread, my first try was before_insert.

I have attached a basic appgini file with all the 3 required fields, even doing this, I cannot get the calculation to work and it does not make sense to me as it should be very simple.
Ahmed told me not to add the fields as hidden or read only or it will not work. I have respected this and still the calculation do not work, is there a bug?I have he latest 5.20 version.

You are welcome to give it a try if you have time.

Thanks AhmedBR.


Ben

AhmedBR
AppGini Super Hero
AppGini Super Hero
Posts: 327
Joined: 2013-09-19 10:23

Re: Unable to do a calculation

Post by AhmedBR » 2013-11-05 10:25

Ok, got you, we are on the same page now.

It is a bug, I changed my hook file from sql to check and did not work here also.
Till the bug gets fixed use the SQL version.

Have a nice day
AppGini 22.14 - xampp 3.3.0 - PHP 7.4.30 - Summary reports - Calendar - Mass update - Messages - AppGiniHelper

bambinou
Veteran Member
Posts: 163
Joined: 2013-02-01 15:09

Re: Unable to do a calculation

Post by bambinou » 2013-11-05 14:43

Hi AhmedBR,

Thank you so much for your reply.
I am glad you noticed it was a bug as I started to believe that the problem was from me....:-)

I hope Ahmed will see this one.

Thank you,


Ben

AhmedBR
AppGini Super Hero
AppGini Super Hero
Posts: 327
Joined: 2013-09-19 10:23

Re: Unable to do a calculation

Post by AhmedBR » 2013-11-08 21:22

I already told Ahmad about this, hopefully will be fixed on the bug fixes he is working on right now version 5.21
AppGini 22.14 - xampp 3.3.0 - PHP 7.4.30 - Summary reports - Calendar - Mass update - Messages - AppGiniHelper

User avatar
a.gneady
Site Admin
Posts: 1281
Joined: 2012-09-27 14:46
Contact:

Re: Unable to do a calculation

Post by a.gneady » 2013-11-09 22:55

Hi everyone,

I'm so sorry for the long delay re this issue ... I tried reproducing it on many scenarios but was never successful to reproduce it (as long as the field to modify was not hidden in detail view nor read-only) ... then after a second look at the code above I found the cause of the issue .. it's the function definition .. the line above that reads:

Code: Select all

function cost_before_insert($data, $memberInfo, &$args){
should be corrected to:

Code: Select all

function cost_before_insert(&$data, $memberInfo, &$args){
Notice the "&" operator before $data? This is what tells PHP to pass $data by reference rather than by value, which enables the function to modify $data in the calling code rather than locally ... I checked the code generated by AppGini now and the & operator is always there in all cases .. so, I'd guess that it got removed in Ben's code while making modifications and went unnoticed there after.
:idea: AppGini plugins to add more power to your apps:
  • DataTalk is an innovative AppGini plugin based on ChatGPT that allows you to interact with your AppGini database using natural language questions, without writing any SQL. Check the demo video
  • Mass Update plugin: Update multiple records at once and improve your workflow efficiency.
  • Check our other plugins and get a generous discount of up to 30% when buying 2 or more plugins.

AhmedBR
AppGini Super Hero
AppGini Super Hero
Posts: 327
Joined: 2013-09-19 10:23

Re: Unable to do a calculation

Post by AhmedBR » 2013-11-10 09:29

Hi Ahmad,

It is not working here, I did another test right now, and still does not work.
I think I know how you can reproduce this:
1. Download your invoice of 4.5
2. Regenerate the code by 5.2 e over write everything except the hook file invoice_items.php
3. use the following for before_insert:

Code: Select all

function invoice_items_before_insert(&$data, $memberInfo, &$args){
	        $data['price']= $data['unit_price']*$data['qty'];
		return TRUE;
	}
And the following for after_insert:

Code: Select all

	function invoice_items_after_insert($data, $memberInfo, &$args){

		return TRUE;
	}
As you can see the price stayed NULL:
Null.png
Null.png (9.76 KiB) Viewed 21749 times
It will not work!

This way you can investigate better.
AppGini 22.14 - xampp 3.3.0 - PHP 7.4.30 - Summary reports - Calendar - Mass update - Messages - AppGiniHelper

User avatar
a.gneady
Site Admin
Posts: 1281
Joined: 2012-09-27 14:46
Contact:

Re: Unable to do a calculation

Post by a.gneady » 2013-11-10 13:32

Thanks Ahmed for the detailed steps, but in the invoice project, the price field is set as read-only .. and this is one of the 2 cases where using $data in hooks won't update the field (the other case being if the field is set as hidden in the detail view) .. So, did you try the above steps after unchecking the read-only option for the price in AppGini?

Image
:idea: AppGini plugins to add more power to your apps:
  • DataTalk is an innovative AppGini plugin based on ChatGPT that allows you to interact with your AppGini database using natural language questions, without writing any SQL. Check the demo video
  • Mass Update plugin: Update multiple records at once and improve your workflow efficiency.
  • Check our other plugins and get a generous discount of up to 30% when buying 2 or more plugins.

AhmedBR
AppGini Super Hero
AppGini Super Hero
Posts: 327
Joined: 2013-09-19 10:23

Re: Unable to do a calculation

Post by AhmedBR » 2013-11-10 14:20

Cool, Good to know.
No, did not try that, I will try it now and let you know.

I would like to suggest something:
Not sure which version you are using here, but sure you will find one that matches your phpbb, is to add SOLVED BUTTON:
https://www.phpbb.com/community/viewtopic.php?t=1214795

It will be nice for everyone especially for you enabling you to follow which bug is still open.
AppGini 22.14 - xampp 3.3.0 - PHP 7.4.30 - Summary reports - Calendar - Mass update - Messages - AppGiniHelper

AhmedBR
AppGini Super Hero
AppGini Super Hero
Posts: 327
Joined: 2013-09-19 10:23

Re: Unable to do a calculation

Post by AhmedBR » 2013-11-10 14:39

Yes, removing the read only would work.

I have another suggestion:
In the generated comments of the hook file add the above cases as you wrote: "If a field is set as etc.", as I did not know that and for sure others did not as well.

Have a nice day
AppGini 22.14 - xampp 3.3.0 - PHP 7.4.30 - Summary reports - Calendar - Mass update - Messages - AppGiniHelper

User avatar
a.gneady
Site Admin
Posts: 1281
Joined: 2012-09-27 14:46
Contact:

Re: Unable to do a calculation

Post by a.gneady » 2013-11-10 14:49

Thanks for the confirmation .. I'll check the "Solved" button .. it makes a lot of sense.

I'll also add the suggested comment in the hooks files in the next release .. but you won't be able to see it unless you're generating the project into an empty folder because AppGini doesn't overwrite hook files even if you tell it to overwrite all files.
:idea: AppGini plugins to add more power to your apps:
  • DataTalk is an innovative AppGini plugin based on ChatGPT that allows you to interact with your AppGini database using natural language questions, without writing any SQL. Check the demo video
  • Mass Update plugin: Update multiple records at once and improve your workflow efficiency.
  • Check our other plugins and get a generous discount of up to 30% when buying 2 or more plugins.

bambinou
Veteran Member
Posts: 163
Joined: 2013-02-01 15:09

Re: Unable to do a calculation

Post by bambinou » 2013-11-10 15:01

Hi Ahamed(s) :-)))

Sorry for the delay in my reply, I was really busy....
I did uncheck the read only and still had the error, I will have to erase all my previous project and try again as I could not get it to work even after following your steps. I am at work right now and cannot verify it yet.

I will let you know.

Thank you,

Ben

User avatar
a.gneady
Site Admin
Posts: 1281
Joined: 2012-09-27 14:46
Contact:

Re: Unable to do a calculation

Post by a.gneady » 2013-11-10 15:40

Ben,

Besides unchecking read-only, also make sure that $data in the function definition has an & before it: &$data as explained above.
:idea: AppGini plugins to add more power to your apps:
  • DataTalk is an innovative AppGini plugin based on ChatGPT that allows you to interact with your AppGini database using natural language questions, without writing any SQL. Check the demo video
  • Mass Update plugin: Update multiple records at once and improve your workflow efficiency.
  • Check our other plugins and get a generous discount of up to 30% when buying 2 or more plugins.

bambinou
Veteran Member
Posts: 163
Joined: 2013-02-01 15:09

Re: Unable to do a calculation

Post by bambinou » 2013-11-11 00:57

Hi Ahmad,

Well this is a weird one. I have just downloaded your 5.21 version and generated this code:

Code: Select all

	 *     $data['units'], $data['cost'], $data['total']
	 * $data array is passed by reference so that modifications to it apply to the insert query.
	 * 
	 * @param $memberInfo
	 * An array containing logged member's info.
	 * @see http://bigprof.com/appgini/help/working-with-generated-web-database-application/hooks/memberInfo
	 * 
	 * @param $args
	 * An empty array that is passed by reference. It's currently not used but is reserved for future uses.
	 * 
	 * @return
	 * A boolean TRUE to perform the insert operation, or FALSE to cancel it.
	*/

	function cost_before_insert(&$data, $memberInfo, &$args){
        $data['total'] = $data['units'] * $data['cost']; 
		return TRUE;
	}
Now in the next funtion, I have this:

Code: Select all

	function cost_after_insert($data, $memberInfo, &$args){

		return TRUE;
	}


All the fields are set as "integer", is this correct?

Please see the result(picture attached)


Thank you,

Ben
Attachments
Untitled-3.png
Untitled-3.png (105.17 KiB) Viewed 21741 times
Untitled-2.png
Untitled-2.png (29.33 KiB) Viewed 21741 times

User avatar
a.gneady
Site Admin
Posts: 1281
Joined: 2012-09-27 14:46
Contact:

Re: Unable to do a calculation

Post by a.gneady » 2013-11-11 02:09

Hi Ben,

You attached the properties of the cost.unit field, what about cost.total? This is the one you want to make sure is not read-only or hidden in detail view.
:idea: AppGini plugins to add more power to your apps:
  • DataTalk is an innovative AppGini plugin based on ChatGPT that allows you to interact with your AppGini database using natural language questions, without writing any SQL. Check the demo video
  • Mass Update plugin: Update multiple records at once and improve your workflow efficiency.
  • Check our other plugins and get a generous discount of up to 30% when buying 2 or more plugins.

bambinou
Veteran Member
Posts: 163
Joined: 2013-02-01 15:09

Re: Unable to do a calculation

Post by bambinou » 2013-11-11 08:09

1.png
1.png (193.99 KiB) Viewed 21738 times
Hi Ahmed,

My apology, Yes it is also unticked. I also clicked on the table itself and there is nothing that hides it.
I think it is best for me to give you all the generated php files + the axp file in one winrar folder as I really do not understand what I am missing there as it should be so simple....I will send them to you by email as I cannot upload them here(too big).

I have removed my config file and the calculation code, this is the code that comes out of AppGini.
Last night I erased all the folders and files on the server, re-uploaded the exact same version I am giving to you now, re-added my calculations as pasted in the previous post and still, I could not get it to work.
I am attaching 2 more pictures, could it be something to do with the data type? I am sure it is working and I am missing something but what?
It is really weird........

Thank you,

Ben

ps:Really nice the "left, center and right" table column title settings in the v5.21, this is sooo cool :-)
Attachments
2.png
2.png (215.91 KiB) Viewed 21738 times

Post Reply