Page 2 of 2

Re: Unable to do a calculation

Posted: 2013-11-18 21:00
by bambinou
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

Re: Unable to do a calculation

Posted: 2013-11-19 09:51
by AhmedBR
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;
	}

Re: Unable to do a calculation

Posted: 2013-11-23 10:39
by bambinou
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

Re: Unable to do a calculation

Posted: 2014-10-14 04:51
by tuxor
You need to use php 5.4 or after. check that!

Re: Unable to do a calculation

Posted: 2014-10-29 15:32
by toconnell
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!

Re: Unable to do a calculation

Posted: 2016-08-03 20:50
by ronwill
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!

Re: Unable to do a calculation

Posted: 2016-08-04 13:01
by grimblefritz
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.

Re: Unable to do a calculation

Posted: 2018-01-12 10:04
by dannybridi
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

Re: Unable to do a calculation

Posted: 2018-01-16 11:17
by R Tammam
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

Re: Unable to do a calculation

Posted: 2018-02-01 02:07
by dannybridi
Thanks Tammam,
It seems to work in the before update hook function.