php discounts for different ages and members [duplicate] - php

This question already has answers here:
The 3 different equals
(5 answers)
Closed 4 years ago.
I am attempting to create a program which can calculate the amount of discount for different ages and members. People aged under 12 can a 50% discount and a additional 10% if their a member. People aged under 18 or over 65 can have a 25% discount and an additional 10% on top if their a member.
My program only seems to work if the age is below 12 does anyone have any suggestions on how to fix (go easy on me i'm new to programming).
$ticketPrice = 25;
$age = 25;
$membership = 'Yes';
$finalPrice;
$discount;
$memberDis;
if($age < 12) {
$finalPrice = 25 / 2;
} else if($age < 18) {
$discount = 25 * 0.25;
$finalPrice = 25 - $discount;
} else if($age < 65) {
$discount = 25 * 0.25;
$finalPrice = 25 - $discount;
} else if($membership = 'Yes') {
$discount = $finalPrice * .10;
$memberDis = $dicount * 100;
}
echo "<br />";
echo "<h1>Ticket Example</h1>";
echo 'Inital Ticket Price: '."&pound".$ticketPrice;
echo "<br />";
echo "Age: ".$age;
echo "<br />";
echo "Member: ".$membership;
echo "<br />";
echo "Final Ticket Price: "."&pound".$finalPrice;

You should use the variable $ticketPrice instead of hardcoding its value in the if ..else.
Inside your conditional statements, just determine the $discount first.
Then outside the conditions, calculate the final price
Comparison operator is == not =.
Membership condition check should be moved out and separate from age checks.
Try
// Initialize discount to 0
$discount = 0;
$finalPrice = $ticketPrice;
if($age < 12) {
// if age is less than 12 then 50% discount
$discount = 50;
} elseif($age < 18 || $age > 65) {
// 25% discount for age < 18 or > 65
$discount = 25;
}
if ($membership == 'Yes') {
// additional 10% discount on membership
$discount += 10;
}
// now calculate the final price after removing discount
$finalPrice -= ($finalPrice*$discount/100);

You can see a flowchart representation of your code, in order to make easy to understand what are you doing and why it doesn't work.
As you can see the membership discount is applied if the age is greater or equals to 65, though you should see the difference between =, == and === operators.
according to your code structure, you should have:
<?php
$ticketPrice = 25;
$age = 25;
$membership = 'Yes';
$finalPrice;
$discount;
$memberDis;
if($age < 12) {
$finalPrice = 25 / 2;
} else if($age < 18) {
$discount = 25 * 0.25;
$finalPrice = 25 - $discount;
} else if($age < 65) {
$discount = 25 * 0.25;
$finalPrice = 25 - $discount;
}
if($membership === 'Yes') {
$discount = $finalPrice * .10;
$finalPrice -= $discount;
}
echo "<br />";
echo "<h1>Ticket Example</h1>";
echo 'Inital Ticket Price: '."&pound".$ticketPrice;
echo "<br />";
echo "Age: ".$age;
echo "<br />";
echo "Member: ".$membership;
echo "<br />";
echo "Final Ticket Price: "."&pound".$finalPrice;

Related

Finding certain percentages from function

I'm making a grade calculator where the user inputs their earned points, points possible, then its weight within the class. Afterwards it then calculates and prints the percentage earned for each portion along with its weight. Finally, it prints your total calculated score along with the letter grade. I'm able to print the very last line completely, along with each categories' weight, but I can't print out the percentage earned in each category
<?php
$par_Earned = $_POST['earnedParticipation'];
$par_Possible = $_POST['maxParticipation'];
$par_Weight = $_POST['weightParticipation'];
$q_Earned = $_POST['earnedQuiz'];
$q_Possible = $_POST['maxQuiz'];
$q_Weight = $_POST['weightQuiz'];
$l_Earned = $_POST['earnedLab'];
$l_Possible = $_POST['maxLab'];
$l_Weight = $_POST['weightLab'];
$p_Earned = $_POST['earnedPracticum'];
$p_Possible = $_POST['maxPracticum'];
$p_Weight = $_POST['weightPracticum'];
function percentage($a,$b){
$percent = ($a/$b)*100;
return $percent;
}
function weightValue($c,$d,$e){
$weight = (percentage($c,$d) * $e)/100 ;
return $weight;
}
$parWeight = weightValue($par_Earned,$par_Possible,$par_Weight);
$quizWeight = weightValue($q_Earned,$q_Possible,$q_Weight);
$labWeight = weightValue($l_Earned,$l_Possible,$l_Weight);
$pracWeight = weightValue($p_Earned,$p_Possible,$p_Weight);
$total = $parWeight+$quizWeight+$labWeight+$pracWeight;
function lettergrade($total) {
if ($total >= 95) {
$lettergrade = "A+";
}
else if ($total < 95 && $total >= 90 ) {
$lettergrade = "A";
}
else if ($total < 90 && $total >= 85) {
$lettergrade = "B+" ;
}
else if ($total < 85 && $total >= 80) {
$lettergrade = "B" ;
}
else if ($total < 80 && $total >= 75) {
$lettergrade = "C+" ;
}
else if ($total < 75 && $total >= 70) {
$lettergrade = "C" ;
}
else if ($total < 70 && $total >= 60) {
$lettergrade ="D" ;
}
else if ($total < 60 && $total >= 0) {
$lettergrade = "F" ;
}
else {
$lettergrade = "Grade is not valid";
}
return $lettergrade;
}
echo "<p>You earned a X with a weighted value of " .$par_Weight. "% </p>";
echo "<p>You earned a X with a weighted value of " .$q_Weight. "% </p>";
echo "<p>You earned a X with a weighted value of " .$l_Weight. "% </p>";
echo "<p>You earned a X with a weighted value of " .$p_Weight. "% </p>";
echo "<p><b>Your final grade is " .$total. "%, which is a ".lettergrade($total)."</b></p>";
?>

the rewards / loyalty program is dishing out free money to everybody

My application rewards loyalty by printing a coupon code on the receipt / thank you page every 15th order. The coupon value is equal to the total price of one of the previous 15 orders, pulled at random.
This was working for a while, but now for some reason, every new user is getting a coupon code on their very first order:
$count = dbFuncs::countRewardsProgress($_SESSION['user_id'])['count'];
$displayRewardsCode = dbFuncs::displayRewardsCode()['rewardsCode'];
if ( $count > 0 ) {
$remainder = $count % 15;
}
if($remainder == 0) {
echo "Congratulations, You've earned a $$previousOrderValueRoulette coupon code! We thank you for your continued business.";
echo "<br />";
echo "<br />";
echo 'Coupon code: ';
echo $displayRewardsCode;
dbFuncs::assignRewardsCode($previousOrderValueRoulette, $_SESSION['user_id'], $displayRewardsCode);
} else {
echo 'rewards progress: ';
echo $count;
echo '/15 orders completed';
}
How can I ensure that customers only receive a coupon every 15th order?
Because if $count is 0, $reminder is not initialized, so it is equal to 0 in your next condition.
To solve, change the second condition to also check that $count is greater than 0
if ( $count > 0 ) {
$remainder = $count % 15;
}
if($count > 0 && $remainder == 0) {
echo "Congratulations, You've earned a $$previousOrderValueRoulette coupon code! We thank you for your continued business.";
echo "<br />";
echo "<br />";
echo 'Coupon code: ';
echo $displayRewardsCode;
dbFuncs::assignRewardsCode($previousOrderValueRoulette, $_SESSION['user_id'], $displayRewardsCode);
} else {
echo 'rewards progress: ';
echo $count;
echo '/15 orders completed';
}

Some rows aren't counted in a foreach loop

I get price from database with 9xx items.
I add this on show items page. Using Foreach $rows=$row. vprice is my sellingprice and dprice is my dealerprice
$commisionrate = 30;
$commisionfee = 100;
$fee = $row['dprice'] + $commisionfee;//+100
$x = $row['dprice'];
$y = $x * $commisionrate / 100;
$z = $x + $y;
$rate = $z;//(100*30%)+100
if (($rate > $row['vprice']) && ($fee < $row['vprice'])){
echo $fee;
}elseif (($fee > $row['vprice']) && ($rate < $row['vprice'])){
echo $rate;
}elseif ($row['dprice']=$row['vprice']){
echo $row['dprice'];
}
when I recheck all, I found that few items of $row['dprice'] is not counted and still show by old price. Example that is false: I found vprice is 188 with 80 dprice after calculate should be 104 but not changing with still stay on 80.
$commisionrate = 30;
$commisionfee = 100;
$fee = $row['dprice'] + $commisionfee;//+100
$x = $row['dprice'];
$y = $x * $commisionrate / 100;
$rate = $x + $y;
// You don't need to put nested brackets, it very simple condition
if ($rate > $row['vprice'] && $fee < $row['vprice']){
echo "fee: " . $fee; //add some hint words, so you know which condition fires
// You don't need to put nested brackets, it very simple condition
} elseif ($fee > $row['vprice'] && $rate < $row['vprice']) {
echo "rate: " . echo $rate;
// USE double `==`, because when single `=` used, the condition always returns true and you're confused by your result
} elseif ($row['dprice'] == $row['vprice']) {
echo "row[\'dprice\']: " . $row['dprice'];
// add last else
} else {
// this is helpful for debugging of your possible output, regardless you are awaiting some output here
}

PHP nested IF statement returns parse error

I have nested IF statement to evaluate the quantity variable and process accordingly. I checked the nested IF statement on php.net and followed the same rule but still It does not work.
It parse the following error:
Parse error: syntax error, unexpected 'elseif' (T_ELSEIF) in
if(empty($quantity)){
echo "<p>You did not enter <mark>Quantity</mark>.</p>";
echo "<p>Please return to the form and re-enter the information <br><a href='conditionalsForm.html'>Go Back!</a>
</p>";
}
else {
#ABS function for quantity and tax
$quantity = abs($quantity);
$tax = abs($tax) + 1;
$totalCost = $quantity * $cost;
if($quantity >= 5000){
$totalCost = $totalCost - $discount;
}
#Calculate total cost to include tax.
$totalCost = ($totalCost * $tax);
$totalCost = round($totalCost);
$monthlyPayment = ($totalCost / 12);
#Nested IF to check the total cost for discount and output message
elseif($quantity < 5000){
echo "<p><mark>Discount will not apply as the sale is under $5,000.</p></mark>";
}
endif;
}
endif;
Your syntax is wrong view the updates I made below.
if(empty($quantity)){
echo "<p>You did not enter <mark>Quantity</mark>.</p>";
echo "<p>Please return to the form and re-enter the information <br><a href='conditionalsForm.html'>Go Back!</a>
</p>";
} else {
#ABS function for quantity and tax
$quantity = abs($quantity);
$tax = abs($tax) + 1;
$totalCost = $quantity * $cost;
if($quantity >= 5000){
$totalCost = $totalCost - $discount;
} else {
echo "<p><mark>Discount will not apply as the sale is under $5,000.</p></mark>";
}
#Calculate total cost to include tax.
$totalCost = ($totalCost * $tax);
$totalCost = round($totalCost);
$monthlyPayment = ($totalCost / 12);
}

Can't figure out why this isn't working? [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 11 years ago.
<?php
$tireqty = $_POST['tireqty'];
$oilqty = $_POST['oilqty'];
$sparkqty = $_POST['sparkqty'];
$totalamount = 0.00;
define('TIREPRICE', 100);
define('OILPRICE', 10);
define('SPARKPRICE', 4);
$totalqty = 0;
$totalqty = $tireqty + $oilqty + $sparkqty;
i f ($totalqty == 0) {
echo "You did not enter anything in the boxes on the previous page.";
}
else {
echo "<p>Order processed at ".date('H:i, jS F Y')."</p><br /><br />";
echo '<p>Your order is as follows:</p>';
echo $tireqty.' tires<br />';
echo $oilqty.' bottles of oil<br />';
echo $sparkqty.' spark plugs<br />';
echo "Items ordered: ".$totalqty."<br />";
if ($tireqty < 10) {
$discount = 0;
}
elseif ($tireqty >= 10) && ($tireqty <= 49) {
$discount = 0.05;
}
elseif ($tireqty >= 50) && ($tireqty <= 99) {
$discount = 0.10;
}
elseif ($tireqty >= 100) {
$discount = 0.15;
}
$totalamount = ($tireqty * TIREPRICE + $oilqty * OILPRICE + $sparkqty * SPARKPRICE) * (1+$discount);
echo "Subtotal (Discount applied here): $".number_format($totalamount, 2)."<br />";
$taxrate = 0.10;
$totalamount = $totalamount * (1+ $taxrate);
echo "Total including Tax: $".number_format($totalamount,2)."<br />";
}
?>
Any help would be appreciated.
I am a beginner so I might wrong but here elseif ($tireqty >= 10) && ($tireqty <= 49) I would use and extra bracket:
elseif (($tireqty >= 10) && ($tireqty <= 49)) {
...
}
Hope this is it:)
This is a terrible question but here goes... without knowing what's "not working" I can only guess:
i f ($totalqty == 0) {
That's a syntax error. You probably meant:
if ($totalqty == 0) {
Likewise, here:
if ($tireqty < 10) {
$discount = 0;
}
elseif ($tireqty >= 10) && ($tireqty <= 49) {
$discount = 0.05;
}
elseif ($tireqty >= 50) && ($tireqty <= 99) {
$discount = 0.10;
}
elseif ($tireqty >= 100) {
$discount = 0.15;
}
The entire conditions need to be enclosed in parens:
elseif (($tireqty >= 10) && ($tireqty <= 49)) {
$discount = 0.05;
}
elseif (($tireqty >= 50) && ($tireqty <= 99)) {
$discount = 0.10;
}
elseif ($tireqty >= 100) {
$discount = 0.15;
}
It's possible there's a lot more things wrong here. Please edit your question and describe specifically what is not working, and what you've done to fix it, and what you need help with.

Categories