Page 1 of 1

Using the Elseif statement

Posted: Thu Nov 14, 2013 10:07 pm
by paycheck
I am trying to change some shopping cart script for a site I developed over 5 years ago. I have a simple set up which sets a few session variables of $boothNumber and $boothCost

Cost is calculated by the $boothNumber so simply put the number of booths * the Cost of the booth which was a set variable of 130

Now my client wants to have the number of booths change the price based on how many booths they order. Here is the schedule of costs they want to implement.
1 booth 155
2 booths 285
3 booths 415
4 booths 545
5 booths 675
6 booths 805

Here is what I am trying to do with the elsif statement but am not succeeding

Code: Select all

session_start();

$_SESSION['eventDate'] = strip_tags($_POST['eventDate']);
$_SESSION['boothNumber'] = strip_tags($_POST['boothNumber']);
$_SESSION['tableNumber'] = strip_tags($_POST['tableNumber']);

//New Code for Looping through costs of booths
    if($boothNumber=="1")
{
    $boothCost=155;
}
else if($boothNumber=="2")
{
   $boothCost=285;
}
else if($boothNumber=="3")
{
     $boothCost=415;
} 
else if($boothNumber=="4")
{
     $boothCost=545;
}  
else if($boothNumber=="5")
{
     $boothCost=675;
} 
else if($boothNumber=="6")
{
     $boothCost=805;
} 
enfif;

//$boothCost = 130;
$boothSubCost = ($_SESSION['boothNumber'] * $boothCost);
$_SESSION['boothSubCost'] = $boothSubCost;

$tableCost = 10;
$tableSubCost = ($_SESSION['tableNumber'] * $tableCost);
$_SESSION['tableSubCost'] = $tableSubCost;

$totalCost = $boothSubCost + $tableSubCost;
$_SESSION['totalCost'] = $totalCost;

setlocale(LC_MONETARY, 'en_US');


?>

Re: Using the Elseif statement

Posted: Thu Nov 14, 2013 10:11 pm
by Celauran

Code: Select all

$boothCost = (130 * $boothNumber) + 25;

Re: Using the Elseif statement

Posted: Thu Nov 14, 2013 10:49 pm
by paycheck
A start but it is not calculating correctly

Code: Select all

The result is 100.00 when using this set up

$boothCost = (130 * $boothNumber) + 25;
$boothSubCost = ($_SESSION['boothNumber'] * $boothCost);
$_SESSION['boothSubCost'] = $boothSubCost;

$tableCost = 10;
$tableSubCost = ($_SESSION['tableNumber'] * $tableCost);
$_SESSION['tableSubCost'] = $tableSubCost;

$totalCost = $boothSubCost + $tableSubCost;
$_SESSION['totalCost'] = $totalCost;

Re: Using the Elseif statement

Posted: Thu Nov 14, 2013 10:56 pm
by paycheck
paycheck wrote:A start but it is not calculating correctly

Code: Select all

The result is 100.00 when using this set up

$boothCost = (130 * $boothNumber) + 25;
$boothSubCost = ($_SESSION['boothNumber'] * $boothCost);
$_SESSION['boothSubCost'] = $boothSubCost;

$tableCost = 10;
$tableSubCost = ($_SESSION['tableNumber'] * $tableCost);
$_SESSION['tableSubCost'] = $tableSubCost;

$totalCost = $boothSubCost + $tableSubCost;
$_SESSION['totalCost'] = $totalCost;
Got it, with your help.

Code: Select all

session_start();

$_SESSION['eventDate'] = strip_tags($_POST['eventDate']);
$_SESSION['boothNumber'] = strip_tags($_POST['boothNumber']);
$_SESSION['tableNumber'] = strip_tags($_POST['tableNumber']);

$boothCost = 130;
$boothSubCost = ($_SESSION['boothNumber'] * $boothCost +25);
$_SESSION['boothSubCost'] = $boothSubCost;

$tableCost = 10;
$tableSubCost = ($_SESSION['tableNumber'] * $tableCost);
$_SESSION['tableSubCost'] = $tableSubCost;

$totalCost = $boothSubCost + $tableSubCost;
$_SESSION['totalCost'] = $totalCost;

setlocale(LC_MONETARY, 'en_US');

?>
instead of adding the calculation to the booth cost, I added it to the booth sub cost and now it works perfectly

Thank you