Hi Barry, here is a form you can build on:
[php]
<?php
// check if form has been submitted
if(isset($_POST['submit']))
{
// initialise an error array
$errors = array();
// check the item is not empty
if(!empty($_POST['item']))
{
$item = $_POST['item'];
}
else
{
$item = false;
$errors[] = 'Please enter an item';
}
// check the amount is a number
if(is_numeric($_POST['amount']))
{
$amount = $_POST['amount'];
}
else
{
$amount = false;
$errors[] = 'Please enter an amount';
}
// check everything is ok
if($item && $amount)
{
// set the item price (this is a dummy price, you will use the proper price stored elsewhere!)
$itemPrice = 14.99;
// how much is the total cost?
$totalPrice = $itemPrice * $amount;
// how much to spend before discount applied
$discountTarget = 35.00;
// set the discount at 10 percent of total price
$discount = .10;
// check if the total price exceeds the discount target price
if($totalPrice > $discountTarget)
{
// if so, set the discount
$discountSaved = number_format($totalPrice * $discount, 2);
$discountPrice = number_format($totalPrice - ($totalPrice * $discount), 2);
}
else
{
// if not, set the normal price
$discountPrice = $totalPrice;
}
// show how much to pay?
echo 'Original price: £' . $totalPrice;
echo '
';
echo 'Discount: £' . $discountSaved;
echo '
';
echo 'Discount price: £' . $discountPrice;
}
else // something wasn't right?
{
// show the errors (if any)
foreach($errors AS $error)
{
echo '
' . $error . '
';
}
}
}
else
{
// display the form in HTML ?>
<form action="<?php $_SERVER['PHP_SELF']; ?>" method="post">
Item: <input type="text" name="item" />
Amount: <input type="text" name="amount" />
<input type="submit" name="submit" value="Submit" />
</form>
<?php
} // close if/else statement
?>
[/php]
Based on the price i’ve put in the script, and wanting to purchase 4 items, this outputs:
[sup]Original price: £59.96
Discount: £6.00
Discount price: £53.96
[/sup]
Hope this helps