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: 162
Joined: 2013-02-01 15:09

Re: Unable to do a calculation

Post by bambinou » 2013-11-18 21:00

Hi all,

I can't believe I have been trying to do this for soooo long without success....

I tried this from AhmedBR:


my 4 table fields form mysql are:
id
unit
price
total

The math should be:
Toal = price * unit;

Code: Select all

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

      $id=$data['id'];
      $vtotal=$data['units']*$data['price'];
      sql("update total set total='$vtotal' where id='$id'");
      return TRUE;
	}
Please help....I wish I could work it out but both solutions do not work for me.
I tried many different variations without success.

Thank you,

Ben

AhmedBR
Veteran Member
Posts: 187
Joined: 2013-09-19 10:23

Re: Unable to do a calculation

Post by AhmedBR » 2013-11-19 09:51

There are some errors in your code, try this, should work (just recheck the table and field names I wrote for you):

Code: Select all

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

		$id=$data['selectedID'];
		$vtotal=$data['units']*$data['price'];
		sql("update cost set total='$vtotal' where id='$id'");

		return TRUE;
	}

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

Re: Unable to do a calculation

Post by bambinou » 2013-11-23 10:39

Hi All,

I wanted to update you on this as I was really going to give up......

This time I took the things a bit further and it worked straight away.

1)I deleted fully the database
2)I delete fully Appgini from the computer
3)Re-downloaded the latest version
4)Recreated a database
5)Ran the script

6) Added this simple code:

Code: Select all

	function meter_records_before_insert(&$data, $memberInfo, &$args){
        $data['total'] = $data['units'] * $data['cost'];
		return TRUE;
	}

And it worked straight away.

Why not before? Well it is a mystery, I have no clue...................................A bug somewhere.

Well I can now move on with the project,,,,,can't believe it!


Thanks for your support everyone:-))


Ben

tuxor
Veteran Member
Posts: 48
Joined: 2014-06-15 00:09

Re: Unable to do a calculation

Post by tuxor » 2014-10-14 04:51

You need to use php 5.4 or after. check that!

toconnell
Veteran Member
Posts: 190
Joined: 2013-04-09 19:29

Re: Unable to do a calculation

Post by toconnell » 2014-10-29 15:32

Just hire Uday.. he can do it in about 10 minutes.

See this forum topic: http://forums.appgini.com/phpbb/viewtop ... f=7&t=1202
he has many references. He is so good!

ronwill
Veteran Member
Posts: 31
Joined: 2015-08-08 10:12

Re: Unable to do a calculation

Post by ronwill » 2016-08-03 20:50

I think the 'Bug' is still alive and well even after all this time! I've downloaded the sample from the website for 'simple invoice' and after a day trying to get it to calculate a 'total' and reading all the above, I have given up!
I wish if something is going to be given out as a sample that it would be hard tested first for bugs, etc. to at least make sure it works or to have a step by step check system for when it does not, feeling disappointed that I've had to drop yet another project because of these software issues!
Ron - AppGini Pro V5.50 Rev 835

grimblefritz
Veteran Member
Posts: 288
Joined: 2015-12-23 16:52

Re: Unable to do a calculation

Post by grimblefritz » 2016-08-04 13:01

ronwill

I just downloaded the invoicing app and it works as it should. I'm using version 5.51 (latest version as of this writing) and have yet to encounter a problem, such as the one discussed in this thread. I have encountered a corrupt AppGini .axp file, one time, and simply restored a previous version to resolve that problem. Other than that, I've found AppGini to be solid. More often, especially early on in my use of AppGini, I encounter problems caused by my own lack of understanding of how AppGini works :)

Maybe you can explain, when you say 'calculate a total', exactly what you are attempting to do. There are several factors that come into play: using readonly or hidden fields, attempting to do things before_insert vs before_update, etc.

For example, in the sample invoicing app, the fields on the invoice page that are not inputs, are readonly. If you want to modify them, you cannot do it before_insert/update using the $data array. You have to do it after_insert/update using sql to update the database. There's a split second difference in when the insert vs update changes occur, but a vast difference in their environments.

To see why you can't modify subtotal, tax and total before_insert, add the following to the invoices_before_insert (and invoices_before_update) functions:

Code: Select all

exit('<pre>'.print_r($data,true));
You cannot modify fields in $data that do not exist. Readonly and hidden fields are not added to $data. Therefore, as the subtotal and tax and total fields are readonly, they aren't able to be changed in the before_insert/update functions.

dannybridi
Posts: 3
Joined: 2016-03-21 19:33

Re: Unable to do a calculation

Post by dannybridi » 2018-01-12 10:04

grimblefritz,

I'm having similar issues as ronwill:

I have a Participants table with a field ExtraAmount. Placing the following code (for testing) under Participants_before_update and/or after_update does not change anything to the field:

$data['ExtraAmount'] = $data['ExtraAmount']+1;

Then I realized from your reply above that ExtraAmount cannot be ReadOnly nor Hidden (which it was), so I removed these flags, but the code above still does not work. What am I doing wrong?

Thanks for your help

R Tammam
Veteran Member
Posts: 32
Joined: 2017-08-26 15:35

Re: Unable to do a calculation

Post by R Tammam » 2018-01-16 11:17

Hello dannybridi ,
i would like to know whether you have your code in after update or before update hooks function ?
but i think it's in after update , if though , try to put it in before update hook function

Post Reply