["XS",26],
5=>["S",36],
10=>["M",53],
20=>["L",80],
30=>["XL",105],
40=>["XXL",135],
];
$type_prices = [
15,//Eco
26,//Premium
38,//Express
7,//Documents
];
//////////////CALCULATION////////////////
$output_total = 0;//Total price and table will not be shown on first visit
if(//check if variables are set, to avoid the calculation on first visit
isset($_POST['country_origin'])
&&isset($_POST['country_destination'])
&&isset($_POST['weight'])
&&isset($_POST['length'])
&&isset($_POST['width'])
&&isset($_POST['height']))
{
if(//only calculate if all necassary fields were filled with an option
empty($_POST['country_origin'])
||empty($_POST['country_destination'])
||empty($_POST['length'])
||empty($_POST['width'])
||empty($_POST['height'])
||empty($_POST['weight']))
{
$error_message = 'Please select an option in every field!';
}
else{//origin to calculate after all data is availible
$output_total = 1;
//save inserted data in more readeable variables
$country_origin = $_POST['country_origin'];
$country_destination = $_POST['country_destination'];
$weight = $_POST['weight'];
$length = $_POST['length'];
$size = ($_POST['length'] * $_POST['width'] * $_POST['height']/6000);
$type = $_POST['type'];
//define the price for a route depending on the origin and destination country
switch($country_origin){
case 1: switch($country_destination){
case 1:$country_price=1.5;break;
case 2:$country_price=2;break;
case 3:$country_price=2;break;
}break;
case 2: switch($country_destination){
case 1:$country_price=2.5;break;
case 2:$country_price=4;break;
case 3:$country_price=2;break;
}break;
case 3: switch($country_destination){
case 1:$country_price=4.5;break;
case 2:$country_price=5;break;
case 3:$country_price=3;break;
}break;
}
//define the price for the weight depending on the selected value
foreach ($categories as $i => $value) {
if($weight<=$i){
$weight_category = $i;
break;
}else{
$weight_category = max(array_keys($categories));
}
}
$weight_price = $categories[$weight_category][1];
$weight_name = $categories[$weight_category][0];
//define the price for the size depending on the selected value
foreach ($categories as $i => $value) {
if($size<=$i){
$size_category = $i;
break;
}else{
$size_category = max(array_keys($categories));
}
}
$size_price = $categories[$size_category][1];
$size_name = $categories[$size_category][0];
if($size_price>=$weight_price){$size_weight_price=$size_price;$size_weight_name=$size_name;}else{$size_weight_price=$weight_price;$size_weight_name=$weight_name;}
//Set the price of selected delivery-type
$type_price = $type_prices[$type];
}
}
?>
Origin
Destination
Weight
Size
Type
';
echo '
Option
'.$country_origin.'
'.$country_destination.'
'.$weight_name.'('.$weight.')
'.$size_name.'('. round($size,2).')
'.$type.'
';
echo '
Price
'.$country_price.'€
'. $size_weight_price.'€ ('.$size_weight_name.')
'.$type_price.'€
';
//calculation of the main price (add every single values)
$total = $country_price + $size_weight_price + $type_price;
if($size>max(array_keys($categories))){
echo 'The size of the package is to big! Please adjust the size to maximal XXL';
}
else{
echo 'Total: '. $total .'€';
}
}
?>