# PHP and math calculations

Go to solution Solved by ignace,

## Recommended Posts

Hi there, I am trying to learn how to make a calculation script. I first started out trying to ask people online on javascript forums but for some reason no-one is very keen on helping me. I am not a coder or in work I am 16 and trying to create this for my ICT coursework, I am now afriad ive bitten off more than I can chew..

Basically, I have done the research as far as my forumulae is comcerend but I am having difficulties putting it all together onto a website.

Please dont shoot me if this isnt correct but this is my forumlae example

Math.round(400*(49+Math.max(count-35;0)*0.85)/count)/400

this is the problem im trying to solve:

£49 for the first 35 items

£0.85 each item thereafter

user inputs total amount of items eg 400 items

400 items - 35 = 365

365 * 0.85 = 310.25

310.25 + 49 = 359.25

Total Price £359.25

Then the total price £359.25 / 400 to give the price per item £0.89 (to two decimal places)

I really nope someone can help me out here or its back to the drawing bord

##### Share on other sites

You made a syntax error at `Math.max(count-35`;`0)` (bolded the mistake for you).

Besides, checking the maximum out of any value and 0 is basically pointless unless you know for sure that you're going to use negative values...

Oh, use `Number.toFixed(d)` where d is the amount of decimal spots you want to have (the function may round up or down if you're near to an integer value).

Edit: looks like I forgot that the ic code prevents BBCode from being parsed. Hah.

Also, all of this is pretty basic work which could easily be found in Google.

Edited by Irate
##### Share on other sites

thankyou. should I google how to do this using php or stick to javascript. I have to change the numbers into varibles with an input form

##### Share on other sites

You're validating forms?

Use the server-side solution (you could also use server-side JS for that, though).

php.net has all of the Math functions documented. Do not look any further than that if that's all you need.

##### Share on other sites

I think for I might be aiming a bit high as far as my courwork is concerned, but thanks I really do appreciate the adivce I will look at php.net at least ive got 4 months to finish my website

##### Share on other sites

```function calculatePrice(totalItems) {
if (totalItems < 1) {
return 0;
}
if (totalItems <= 35) {
return 49;
}
// we have more then 35 items
// the first 35 go for 49 pound
// all others (totalItems - 35) go for 0.85
return 49 + (totalItems - 35) * 0.85;
}
function calculatePricePerUnit(totalItems) {
return (calculatePrice(totalItems) / totalItems).toFixed(2);
}```
```calculatePrice(35); // 49
calculatePrice(36); // 49.85
calculatePrice(400); // 359.25
calculatePricePerUnit(400); // 0.90```
EDIT: apparently I misinterpreted Edited by ignace
##### Share on other sites

ok.. I can understand this better! Thanks!

##### Share on other sites

```function calculatePrice(totalItems) {
if (totalItems < 1) {
return 0;
}

if (totalItems <= 35) {
return 49;
}

// we have more then 35 items
// the first 35 go for 49 pound
// all others (totalItems - 35) go for 0.85
return 49 + (totalItems - 35) * 0.85;
}```
```calculatePrice(35); // 49
calculatePrice(36); // 49.85
calculatePrice(400); // 359.25```
EDIT: apparently I misinterpreted

Sorry can I ask, how do I assign varibles so I can create a form for someone to input their own number of items...

Edited by MBCUK
##### Share on other sites

One way you could do it is, you create a new function that fills out the other fields in your form:

```function updatePricesOnClick(form) {
form.pricePerUnit.value = calculatePricePerUnit(form.totalItems.value);
form.price.value = calculatePrice(form.totalItems.value);
}```
Your form would then look like:

```<form action="#">
<input type="text" name="totalItems" id="totalItems">
<input type="text" name="pricePerUnit" id="pricePerUnit">
<input type="text" name="price" id="price">
<input type="submit" onclick="updatePricesOnClick(this.parentElement); return false;">
</form>```
When you fill out the first field and press submit the other fields will be filled in. When you write code in the future make sure you separate the code that does calculations (or other app-related heavy-lifting like querying a database) and the code that displays things on the screen. Developers call this "separation of concern".

EDIT: de-VB'ed it.

Edited by ignace
##### Share on other sites

```<script>
function calculatePrice(totalItems) {
if (totalItems < 1) {
return 0;
}

if (totalItems <= 35) {
return 49;
}

// we have more then 35 items
// the first 35 go for 49 pound
// all others (totalItems - 35) go for 0.85
return 49 + (totalItems - 35) * 0.85;
}
function updatePricesOnClick(form) {
form.pricePerUnit.value = calculatePricePerUnit(form.totalItems.value);
form.price.value = calculatePrice(form.totalItems.value);
}
</script>

<form action="#">
<input type="text" name="totalItems" id="totalItems">
<input type="text" name="pricePerUnit" id="pricePerUnit">
<input type="text" name="price" id="price">
<input type="submit" onclick="updatePricesOnClick(this.parentElement); return false;">
</form>
```

Is this correct?

##### Share on other sites

THANKYOU SO MUCH!!!!

##### Share on other sites

```if (totalItems <= 35) {
return 49;
}```
is wrong and should probably be:

```if (totalItems <= 35) {
return (totalItems / 35) * 49;
}```
but i don't know the exact requirements, so don't simply copy/paste this and think about how it is written and correct where necessary. It is not because someone on the internet gave you a working example that is the be all and end all of it. People make mistake, those on the internet do it even more. Edited by ignace
##### 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.

×   Pasted as rich text.   Restore formatting

Only 75 emoji are allowed.