Using the Elseif statement

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
paycheck
Forum Newbie
Posts: 3
Joined: Thu Nov 14, 2013 9:39 pm

Using the Elseif statement

Post 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');


?>
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Using the Elseif statement

Post by Celauran »

Code: Select all

$boothCost = (130 * $boothNumber) + 25;
paycheck
Forum Newbie
Posts: 3
Joined: Thu Nov 14, 2013 9:39 pm

Re: Using the Elseif statement

Post 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;
paycheck
Forum Newbie
Posts: 3
Joined: Thu Nov 14, 2013 9:39 pm

Re: Using the Elseif statement

Post 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
Post Reply