Jump to content

Equation breakdown


bukwus

Recommended Posts

Hi

 

I have built a series of forms with PHP and have been using SESSIONS to carry values from one to another. In one, the value of a variable is determined by adding the values of several other variables together:

$totalFixed = $fixedCost1 + $fixedCost2 + $fixedCost3 + $fixedCost4 + $fixedCost5;

This equation is working fine on that page. I then place the value of $totalFixed in a SESSION variable:

$_SESSION['totalFix']=$totalFixed;

I then assign this SESSION variable's value to a new variable on another page in the series:

$dataValue[4] = $_SESSION['totalFix'];

The number displays fine on this page as well. However, when the value is above 1,000 and I try to subtract another number from it, it ignores everything to the right of the comma. For example, if $dataValue[4] is 10,132 and $dataValue[5] is 25 and I subtract $dataValue[4] from $dataValue[5], the result is -15.

Does anyone know the reason for this?

Many thanks,

Andy

Link to comment
Share on other sites

Commas are added for human readability. They do not exist in computing. You need to remove these commas before attempting to perform arithmetic operations on the values.

 

str_replace might help with this.

 

Also, you need to be careful passing form data through sessions. What happens when you get half way through the form, open a new tab, and access the same form, or perhaps use a multi-page form with similar-named values?

In order to keep the value unique to the request, you need to create a unique token that gets passed through a hidden field in the form. You then use the token to store the data in a unique location within your session. $_SESSION['formData'][$token]...

Link to comment
Share on other sites

where is it getting the value with the commas in the first place? if you are storing numeric values you might as well store them as a number (without commas).

 

if you want to have commas in output, i'd still store without and just use number_format() when you are going to echo something

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.