english

learn php

learn css

learn html

learn javascript

learn php

5. multiple conditions & arrays

solution 5.2
2 minutes / 301 words

five / solution 5.2

<?php

$br = "\n";

$departments = ["clothing" => ["tax" => 15, "shipping" => 12000], "accessories" => ["tax" => 15, "shipping" => 18000], "shipping" => ["tax" => 15]];

$products = [["name" => "t-shirt", "price" => 24900, "weight" => 320, "department" => "clothing"], ["name" => "hoodie", "price" => 46240, "weight" => 510, "department" => "clothing"], ["name" => "jeans", "price" => 80750, "weight" => 805, "department" => "clothing"], ["name" => "belt", "price" => 26050, "weight" => 400, "department" => "accessories"]];

$shipping = 0;
$shipping += ($products[0]["weight"] * $departments[$products[0]["department"]]["shipping"]) / 1000;
$shipping += ($products[1]["weight"] * $departments[$products[1]["department"]]["shipping"]) / 1000;
$shipping += ($products[2]["weight"] * $departments[$products[2]["department"]]["shipping"]) / 1000;
$shipping += ($products[3]["weight"] * $departments[$products[3]["department"]]["shipping"]) / 1000;

$subtotal = 0;
$subtotal += $products[0]["price"];
$subtotal += $products[1]["price"];
$subtotal += $products[2]["price"];
$subtotal += $products[3]["price"];
$subtotal += $shipping;

$tax = 0;
$tax += ($products[0]["price"] * $departments[$products[0]["department"]]["tax"]) / 100;
$tax += ($products[1]["price"] * $departments[$products[1]["department"]]["tax"]) / 100;
$tax += ($products[2]["price"] * $departments[$products[2]["department"]]["tax"]) / 100;
$tax += ($products[3]["price"] * $departments[$products[3]["department"]]["tax"]) / 100;
$tax += ($shipping * $departments["shipping"]["tax"]) / 100;

$total = $subtotal + $tax;

$priceOutput = [];
$priceOutput[0] = explode(".", $products[0]["price"])[0];
$priceOutput[1] = explode(".", $products[1]["price"])[0];
$priceOutput[2] = explode(".", $products[2]["price"])[0];
$priceOutput[3] = explode(".", $products[3]["price"])[0];
$shipping = explode(".", $shipping)[0];
$subtotal = explode(".", $subtotal)[0];
$tax = explode(".", $tax)[0];
$total = explode(".", $total)[0];

// note, this rounds everything down.
// there are various ways to round but none have been covered yet.
$priceOutput[0] = substr($priceOutput[0], 0, -1);
$priceOutput[1] = substr($priceOutput[1], 0, -1);
$priceOutput[2] = substr($priceOutput[2], 0, -1);
$priceOutput[3] = substr($priceOutput[3], 0, -1);
$shipping = substr($shipping, 0, -1);
$subtotal = substr($subtotal, 0, -1);
$tax = substr($tax, 0, -1);
$total = substr($total, 0, -1);

$priceOutput[0] = substr_replace($priceOutput[0], ".", -2, 0);
$priceOutput[1] = substr_replace($priceOutput[1], ".", -2, 0);
$priceOutput[2] = substr_replace($priceOutput[2], ".", -2, 0);
$priceOutput[3] = substr_replace($priceOutput[3], ".", -2, 0);
$shipping = substr_replace($shipping, ".", -2, 0);
$subtotal = substr_replace($subtotal, ".", -2, 0);
$tax = substr_replace($tax, ".", -2, 0);
$total = substr_replace($total, ".", -2, 0);

$receipt = "";
$receipt .= $products[0]["name"] . " $" . $priceOutput[0] . $br;
$receipt .= $products[1]["name"] . " $" . $priceOutput[1] . $br;
$receipt .= $products[2]["name"] . " $" . $priceOutput[2] . $br;
$receipt .= $products[3]["name"] . " $" . $priceOutput[3] . $br;
$receipt .= "shipping $" . $shipping . $br . $br;
$receipt .= "subtotal $" . $subtotal . $br . "tax $" . $tax . $br . "total $" . $total;

print_r($receipt);

learn php

learn css

learn html

learn javascript

powered by akelei. an avi sato project.