PHP Calculator

Soldato
Joined
30 Apr 2007
Posts
3,095
Location
Kent
Hi Guys,

I run a website for a guy that I know that owns his own bait rolling company. The more bait people order, the less it costs them - to avoid confusion, I've created a little 'calculator'. However, he's now changed his prices, and I can't get it to work.

The code I currently use is:

Code:
<?

$amount = $_POST['amount'];

$kilob="6";

$kiloa="5";

$ten="10";

$msg="standard message<br />";

if (!$amount || $amount<0) {

echo "Please enter a value that is greater than zero!";

}

else

if ($amount < $ten) {

$sum = $amount * $kilob;

echo "$msg";

echo "<center><b>For $amount Kg your estimated price is: £$sum</b></center>";

}

else

if ($amount >= $ten) {

$sum = $amount * $kiloa;

echo "$msg";

echo "<center><b>For $amount Kg your estimated price is: £$sum</b></center>";
}

else
echo "Error - Please try again";

?>

The prices he now has are:

Up to 50Kg = £5.50 per kilo
50kg - 100Kg = £5.00 per kilo
100Kg plus = £4.50 per kilo

If someone can help me out that be great - my code is probably really poor too, so any advice on that would be great!

Thanks in Advance
 
Would you not just change

$kilob="5.5";

$kiloa="5";

$ten="4.5";


? I'm no PHP expert.. but that's what I would try.
 
Hmm, I've got it working with this code:

Code:
<?
$amount = $_POST['amount'];

$limit1="50";
$limit2="100";

$price1="5.50";
$price2="5";
$price3="4.50";

$msg="message<Br /><Br />";

if (!$amount || $amount<0) {
echo "Please enter a value that is greater than zero!";
}

else
if ($amount < $limit1) {
$sum = $amount * $price1;
echo "$msg";
echo "<center><b>For $amount Kg your estimated price is: £$sum</b></center>";
}

else
if ($amount <= $limit2) {
$sum = $amount * $price2;
echo "$msg";
echo "<center><b>For $amount Kg your estimated price is: £$sum</b></center>";
}

else
if ($amount > $limit2) {
$sum = $amount * $price3;
echo "$msg";
echo "<center><b>For $amount Kg your estimated price is: £$sum</b></center>";
}

else
echo "Error - Please Try Again";
?>

How do I get it to show for example "5.50" instead of "5.5" ?

Thanks
 
Code:
<?php

if (!isset($_POST['amount'])) die("no amount set!");

$amount = intval($_POST['amount']);
$kilob = 6;
$kiloa = 5;
$ten = 10;

if ($amount < 0) die("Please enter a value that is greater than zero!");

if ($amount < $ten) {
  $sum = $amount * $kilob;
} else if ($amount >= $ten) {
  $sum = $amount * $kiloa;
} else {
  die("Error calculating, please try again");
}

echo "<center><b>For " . htmlescapeentities($amount) . "Kg your estimated price is: £" . number_format($sum, 2) . "</b></center>";

?>
 
Or even, just pinching Dj's code
Code:
<?php

if (!isset($_POST['amount'])) die("no amount set!");

$amount = intval($_POST['amount']);
$limit1=50;
$limit2=100;

$price1=5.5;
$price2=5;
$price3=4.5;

if ($amount < 0) die("Please enter a value that is greater than zero!");

if ($amount < $limit1) {
  $sum = $amount * $price1;
} else if ($amount <= $limit2) {
  $sum = $amount * $price2;
} else if ($amount > $limit2) {
  $sum = $amount * $price3;
} else {
  die("Error calculating, please try again");
}

echo "<center><b>For " . htmlescapeentities($amount) . "Kg your estimated price is: £" . number_format($sum, 2) . "</b></center>";

?>
 
I would also use the switch statement instead of all the IF statements - but thats just me, but its easy to read and if your mate every changes prices or changes how many price breaks he has it will be a lot easier to modify.

Just my opinion and I love switch cases (so much so I use the SWITCH perl module for all my perl code as well!!)
 
Back
Top Bottom