thegentleman
IS-IT--Management
Hi,
I'm very new to PHP and have created a simple function that calculates an estimate based on the weight and quantity of a series of packages. The form that it is submitted from will have 1 to 10 packages. Each package can have on of three options which are then used to assign a value to that package. A running total is then created and sent back to an asp page also e-mailed to both the user and the adminstrator of the website.
Discounts are also applied of the quantity of each type of package exceed 1. The function works fine but in a bid to enhance my PHP skills I want to simplify it.
Any ideas?
Oh - during testing it was reviled that the e-mail that is sent out is fine except in AOL - where the numbers and layout are all wrong. Any way to avoid this?
Thanks to all,
~tg
<?php
$total = 0;
$low = 0;
$mid = 0;
$high = 0;
$saving = 0;
function calculate(&$var)
{
global $total;
global $low;
global $mid;
global $high;
switch ($var) {
case 1:
$total = $total + 8.95;
$low = $low + 1;
break;
case 2:
$total = $total + 10.95;
$mid = $mid + 1;
break;
case 3:
$total = $total + 12.95;
$high = $high + 1;
break;
}
}
switch ($pnumber) {
case 1:
calculate($weight1);
break;
case 2:
calculate($weight1);
calculate($weight2);
break;
case 3:
calculate($weight1);
calculate($weight2);
calculate($weight3);
break;
case 4:
calculate($weight1);
calculate($weight2);
calculate($weight3);
calculate($weight4);
break;
case 5:
calculate($weight1);
calculate($weight2);
calculate($weight3);
calculate($weight4);
calculate($weight5);
break;
case 6:
calculate($weight1);
calculate($weight2);
calculate($weight3);
calculate($weight4);
calculate($weight5);
calculate($weight6);
break;
case 7:
calculate($weight1);
calculate($weight2);
calculate($weight3);
calculate($weight4);
calculate($weight5);
calculate($weight6);
calculate($weight7);
break;
case 8:
calculate($weight1);
calculate($weight2);
calculate($weight3);
calculate($weight4);
calculate($weight5);
calculate($weight6);
calculate($weight7);
calculate($weight8);
break;
case 9:
calculate($weight1);
calculate($weight2);
calculate($weight3);
calculate($weight4);
calculate($weight5);
calculate($weight6);
calculate($weight7);
calculate($weight8);
calculate($weight9);
break;
case 10:
calculate($weight1);
calculate($weight2);
calculate($weight3);
calculate($weight4);
calculate($weight5);
calculate($weight6);
calculate($weight7);
calculate($weight8);
calculate($weight9);
calculate($weight10);
break;
}
$inlow = $low - 1;
IF ($inlow > 0) {
$saving = $saving + $inlow;
}
$inmid = $mid - 1;
IF ($inmid > 0) {
$inmid = $inmid * 2;
$saving = $saving + $inmid;
}
$inhigh = $high - 1;
IF ($inhigh > 0) {
$inhigh = $inhigh * 3;
$saving = $saving + $inhigh;
}
$reference = rand(1000000,9999999);
$message = "
Dear $name,
Please find below the details of your estimate:
";
IF ($low > 0) {
$lowtotal = $low * 8.95;
$message = "$message
$low Parcels less than 10kg = £$lowtotal";
}
IF ($mid > 0) {
$midtotal = $mid * 10.95;
$message = "$message
$mid Parcels between 10kg and 20kg = £$midtotal";
}
IF ($high > 0) {
$hightotal = $high * 12.95;
$message = "$message
$high Parcels between 20kg and 30kg = £$hightotal";
}
switch ($destination) {
case 12:
$total = $total + 4;
$message = "$message
Before 12 noon next day = £4.00";
break;
case 10:
$total = $total + 6;
$message = "$message
Before 10am next day = £6.00";
break;
}
$message = "$message
-----------------------------------------------
Subtotal = £$total";
$total = $total - $saving;
$message = "$message
Discount = £$saving";
$message = "$message
-----------------------------------------------
Total = £$total
-----------------------------------------------";
$message = "$message
";
mail("xxx@xxx.com", "On-line estimate REF: $reference", $message, "From: $email"
mail($email, "On-line estimate REF: $reference", $message, "From: xxx@xxx.com"
header( "Location: estimate.asp?step=3&estimate=$total&email=$email&name=$name" );
?>
I'm very new to PHP and have created a simple function that calculates an estimate based on the weight and quantity of a series of packages. The form that it is submitted from will have 1 to 10 packages. Each package can have on of three options which are then used to assign a value to that package. A running total is then created and sent back to an asp page also e-mailed to both the user and the adminstrator of the website.
Discounts are also applied of the quantity of each type of package exceed 1. The function works fine but in a bid to enhance my PHP skills I want to simplify it.
Any ideas?
Oh - during testing it was reviled that the e-mail that is sent out is fine except in AOL - where the numbers and layout are all wrong. Any way to avoid this?
Thanks to all,
~tg
<?php
$total = 0;
$low = 0;
$mid = 0;
$high = 0;
$saving = 0;
function calculate(&$var)
{
global $total;
global $low;
global $mid;
global $high;
switch ($var) {
case 1:
$total = $total + 8.95;
$low = $low + 1;
break;
case 2:
$total = $total + 10.95;
$mid = $mid + 1;
break;
case 3:
$total = $total + 12.95;
$high = $high + 1;
break;
}
}
switch ($pnumber) {
case 1:
calculate($weight1);
break;
case 2:
calculate($weight1);
calculate($weight2);
break;
case 3:
calculate($weight1);
calculate($weight2);
calculate($weight3);
break;
case 4:
calculate($weight1);
calculate($weight2);
calculate($weight3);
calculate($weight4);
break;
case 5:
calculate($weight1);
calculate($weight2);
calculate($weight3);
calculate($weight4);
calculate($weight5);
break;
case 6:
calculate($weight1);
calculate($weight2);
calculate($weight3);
calculate($weight4);
calculate($weight5);
calculate($weight6);
break;
case 7:
calculate($weight1);
calculate($weight2);
calculate($weight3);
calculate($weight4);
calculate($weight5);
calculate($weight6);
calculate($weight7);
break;
case 8:
calculate($weight1);
calculate($weight2);
calculate($weight3);
calculate($weight4);
calculate($weight5);
calculate($weight6);
calculate($weight7);
calculate($weight8);
break;
case 9:
calculate($weight1);
calculate($weight2);
calculate($weight3);
calculate($weight4);
calculate($weight5);
calculate($weight6);
calculate($weight7);
calculate($weight8);
calculate($weight9);
break;
case 10:
calculate($weight1);
calculate($weight2);
calculate($weight3);
calculate($weight4);
calculate($weight5);
calculate($weight6);
calculate($weight7);
calculate($weight8);
calculate($weight9);
calculate($weight10);
break;
}
$inlow = $low - 1;
IF ($inlow > 0) {
$saving = $saving + $inlow;
}
$inmid = $mid - 1;
IF ($inmid > 0) {
$inmid = $inmid * 2;
$saving = $saving + $inmid;
}
$inhigh = $high - 1;
IF ($inhigh > 0) {
$inhigh = $inhigh * 3;
$saving = $saving + $inhigh;
}
$reference = rand(1000000,9999999);
$message = "
Dear $name,
Please find below the details of your estimate:
";
IF ($low > 0) {
$lowtotal = $low * 8.95;
$message = "$message
$low Parcels less than 10kg = £$lowtotal";
}
IF ($mid > 0) {
$midtotal = $mid * 10.95;
$message = "$message
$mid Parcels between 10kg and 20kg = £$midtotal";
}
IF ($high > 0) {
$hightotal = $high * 12.95;
$message = "$message
$high Parcels between 20kg and 30kg = £$hightotal";
}
switch ($destination) {
case 12:
$total = $total + 4;
$message = "$message
Before 12 noon next day = £4.00";
break;
case 10:
$total = $total + 6;
$message = "$message
Before 10am next day = £6.00";
break;
}
$message = "$message
-----------------------------------------------
Subtotal = £$total";
$total = $total - $saving;
$message = "$message
Discount = £$saving";
$message = "$message
-----------------------------------------------
Total = £$total
-----------------------------------------------";
$message = "$message
";
mail("xxx@xxx.com", "On-line estimate REF: $reference", $message, "From: $email"
mail($email, "On-line estimate REF: $reference", $message, "From: xxx@xxx.com"
header( "Location: estimate.asp?step=3&estimate=$total&email=$email&name=$name" );
?>