Web Design/Generating a receipt with PHP
Once your basic response and email are all happening, and you're feeling a bit more confident with your first PHP pages, it's a good time to start learning a bit more about PHP variables and control structures! But before moving on from here, please make sure that your response.php currently:
- Responds personally to the user
- Includes the details of the product selected by the user (its name and its price)
- includes the billing information for the user.
Adding up the total
If a visitor to your site indicates that they want to buy 5 T-shirts at $15 each, we need to let them know what their total comes to! To do this, we'll need to create a variable.
You might have noticed that variables in PHP always have a dollar sign ($) in front of them. Here are a few examples:
$num1 = 12; // This is a number $string1 = "Here's a string in double quotes"; $string2 = 'Here\'s a string in single quotes';
Can you guess why the apostrophe (') in $string2 has a back-slash (\) in front of it?
We need to create a new variable called $total in our response.php. Initially we'll set it to 0 like this:
$total = 0; // We'll use this variable to calculate the total for the order
but your challenge is to print out the total for the order when your form is submitted!
Including postage in the total
Now that we've calculated the total for our order, we need to include a postage fee! The following exercise will help you along the way!
Free delivery for bulk buys
Let's say you decide it would be good for business if you offer free delivery if customers buy more than 6 items.
Your pseudo-code should look something like this:
if the quantity is less than 7 then do the following: add the $20 postage fee to the total
TO DO: Exercise with solution
Discount if you spend over $200
Finally, if your customers spend over $200 (modify to suit your needs), they get a 10% discount.
Again your pseudo-code should look like...
$discountmin = 200; if the quantity is more than discountmin then: multiply amount by 0.9 //0.9 basically just takes away the 10%