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

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';
}

Related

php discounts for different ages and members [duplicate]

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;

get rid of "dividing by zero" warning in sessions

This is a game of guessing number, I want the page to keep track of the wins, losses and totals and the %of winning by using sessions. I want to reset everything when I click "reset", but when I click reset everything equals to zero and there will be a warning saying "dividing by zero". How do I rearrange this php for it to only do the %calculation when the game is played but not when it's being reset?
<?php
// initialize wins and losses
if ($_SESSION['wins'] == "") {
$_SESSION['wins'] = 0;
}
if ($_SESSION['losses'] == "") {
$_SESSION['losses'] = 0;
}
echo "<h1>PHP SESSIONS</h1>";
if ($_GET['reset'] == "yes") {
$_SESSION['wins'] = 0;
$_SESSION['losses'] = 0;
$_SESSION['total'] = 0;
echo "<p>The game has been reset.</p>";
} else {
$randNum = rand(1,5);
echo "<p>The random number is <b>" . $randNum . "</b></p>";
if ($randNum == 3) {
echo "<p>The number equalled 3. YOU WIN!</p>";
$_SESSION['wins'] = $_SESSION['wins'] + 1;
} else {
echo "<p>The number did NOT equal 3. YOU LOSE!</p>";
$_SESSION['losses'] = $_SESSION['losses'] + 1;
}
$_SESSION['total'] = $_SESSION['total'] + 1;
}
$_SESSION ['percentage'] = (($_SESSION['wins']) / ($_SESSION['total'])) * 100 ;
echo "<p>WINS: " . $_SESSION['wins'] . " | LOSSES: " . $_SESSION['losses'] . "| TOTAL: " . $_SESSION['total'] . "</p>";
echo "<p>Percentage of Winning: ". $_SESSION ['percentage'] . " % </p>"
?>
ROLL AGAIN :)
RESET :)
Move up the line in which you are dividing:
$_SESSION ['percentage'] = (($_SESSION['wins']) / ($_SESSION['total'])) * 100 ;
Put it inside the else bracket. Once I reformatted your code, it was easy to see the problem. I'd advise always using proper indenting. :)
When $_GET['reset'] == 'yes' is satisfied, you are setting $_SESSION['total'] = 0.
Then, you're dividing $_SESSION['wins'] by a 0 valued $_SESSION['total'] in the following line (which is executed regardless of the current state of $_SESSION['total']:
$_SESSION ['percentage'] = (($_SESSION['wins']) / ($_SESSION['total'])) * 100 ;
It's easier to see what's going on when the code is properly formatted. Have a look:
if ($_GET['reset'] == "yes") { // reset called
$_SESSION['wins'] = 0;
$_SESSION['losses'] = 0;
$_SESSION['total'] = 0; // set to zero(0)
echo "<p>The game has been reset.</p>";
} else {
$randNum = rand(1,5);
echo "<p>The random number is <b>" . $randNum . "</b></p>";
if ($randNum == 3) {
echo "<p>The number equalled 3. YOU WIN!</p>";
$_SESSION['wins'] = $_SESSION['wins'] + 1;
} else {
echo "<p>The number did NOT equal 3. YOU LOSE!</p>";
$_SESSION['losses'] = $_SESSION['losses'] + 1;
}
$_SESSION['total'] = $_SESSION['total'] + 1;
}
$_SESSION ['percentage'] = (($_SESSION['wins']) / ($_SESSION['total'])) * 100; // chance that $_SESSION['total'] is zero(0)
So again, when you call the reset action, $_SESSION['total'] is given a value of int(0). Later in the script you then divide by that int(0), giving you the error in question. Make sense?
So you first need to ensure $_SESSION['total'] is greater than 0 before dividing by it.

What does this mean and how to resolve it ? - 3.55687428096E+14

Ok, so I need this number 3.55687428096E+14 to actually look like a proper whole number. I am not sure what to do in order to make it look like a whole number so that I can go about with my coding. Basically, without resolving this number 3.55687428096E+14, I cant proceed properly. Below is my code, please help!
<?php
$num = 17;
$fact = $num;
echo "The factorial of $num is $num X ";
while ($num > 1){
--$num;
if ($num != 1){
echo "$num X ";
}
else{
echo "$num <b>equals</b> ";
}
$fact = $fact * $num;
}
echo $fact ."<br/>";
?>
By the way, the number 3.55687428096E+14 is the result I am getting after running the program. It comes ok till factorial 16 but once it turns to 17, things become 3.55687428096E+14!
You can user number_format() function if your number is goes very large.
number_format():
The number_format() function formats a number with grouped thousands.
Your code
<?php
$num = 17;
$fact = $num;
echo "The factorial of $num is $num X ";
while ($num > 1){
--$num;
if ($num != 1){
echo "$num X ";
}
else{
echo "$num <b>equals</b> ";
}
$fact = $fact * $num;
}
echo number_format($fact) ."<br/>";
?>
Change the last line with :
echo number_format($fact,0) ."<br/>";
#Mark Baker: - my code below which was executed successfully thanks to your info and support:
<?php
echo "<h2>Find the sum of the digits in the number 100!</h2>"."<br/>";
$num = 100;
$fact = $num;
echo "The factorial of $num is $num X ";
while ($num > 1){
--$num;
if ($num != 1){
echo " $num X ";
}
else{
echo " $num";
}
$fact = bcmul($fact,$num); //right over here!
}
echo " which equals" ."<br/>". "<h3>$fact</h3>";
//proceeding now to calculate the sum of the digits of the factorial!
$_array = str_split($fact);
$sum = array_sum($_array);
echo "The sum of the digits of the factorial is " ."<br/>";
echo "<h3>$sum</h3>";
?>

Several group totals of an array

I want to display an array (which is already has been sorted) and want to show all rows of the array including some group totals (in this case the totals of one order and the totals per month. Here is the code which I have so far. When I "strip" the code and only do the totals on either order or month is works like a charm, together I don't see the solution...
Here's the code I have so far:
//Put test data in an array [0] = Order, [1] = Month, [2] = Pieces
$data = array(
array("1614-0082","JAN",10),
array("1614-0082","JAN",12),
array("1614-0082","JAN",20),
array("1614-0086","JAN",81),
array("1614-0064","FEB",10),
array("1614-0064","FEB",11),
array("1614-0101","MRT",19),
array("1614-0004","OCT",13),
array("1614-0004","OCT",12),
array("1614-0023","OCT",13),
array("1614-0025","DEC",15),
array("1614-0028","DEC",15),
);
$TotalPcsO = 0; //Total per order
$TotalPcsM = 0; //Total per month
$TotalPcsG = 0; //Grand total
$j = 0;
$i = 0;
$PrevOrder = $data[0][0];
$PrevMonth = $data[0][1];
for($k = 0; $k <= sizeof($data); $k++) {
while ($PrevMonth === $data[$i][1]) {
while ($PrevOrder === $data[$j][0]) {
echo $data[$j][0].' '.$data[$j][1].' '.$data[$j][2];
echo "<br>";
$TotalPcsO += $data[$j][2];
$PrevOrder = $data[$j][0];
$j++;
}
$i = $j;
//Order Totals
echo 'Total of order '.$PrevOrder.': '.$TotalPcsO;
echo "<br>";
echo "<br>";
$TotalPcsM += $TotalPcsO;
$TotalPcsO = 0;
$PrevOrder = $data[$i][0];
$i++;
}
$k = $i;
echo 'Total of month '.$PrevMonth.': '.$TotalPcsM;
echo "<br>";
echo "<br>";
$TotalPcsG += $TotalPcsM;
$TotalPcsM = 0;
$PrevMonth = $data[$k][1];
}
//Grand Totals
echo 'Grand total '.$TotalPcsG;
Try this... it's not the way I would have done it, but based on your code, at least it should work...
$TotalPcsO = 0; //Total per order
$TotalPcsM = 0; //Total per month
$TotalPcsG = 0; //Grand total
$PrevOrder = $data[0][0];
$PrevMonth = $data[0][1];
foreach($data as $row) {
$TotalPcsG+=$row[2];
if($row[0]==$PrevOrder) $TotalPcsO += $row[2];
else {
echo "Total of order $PrevOrder: $TotalPcsO<br /><br />";
$PrevOrder=$row[0];
$TotalPcsO=$row[2];
}
if($row[1]==$PrevMonth) $TotalPcsM += $row[2];
else {
echo "Total of month $PrevOrder: $TotalPcsM<br /><br />";
$PrevMonth=$row[1];
$TotalPcsM=$row[2];
}
echo $row[0].' '.$row[1].' '.$row[2].'<br />';
}
echo "Total of order $PrevOrder: $TotalPcsO<br /><br />";
echo "Total of month $PrevOrder: $TotalPcsM<br /><br />";
echo "Grand total: $TotalPcsG";

Echo text if value of variable equals zero

I have this code:
<?php
if ( $amount < 5 ) {
echo 'Credit Balance low! You have';
echo $amount;
echo ' remaining credits.';
} else {
echo 'No recent alerts...';
}
?>
Where it says echo $amount; I want to echo $00.00 if the value of $amount == 0. How would I include this in my code?
echo $amount === 0 ? '$00.00' : $amount;
?
You're probably looking for the money_format() function.
<?php echo money_format('%=0-2', $amount); ?>
=0 fills with the 0 character, and -2 left-justifies to a minimum field width of 2.
<?php
if ( $amount < 5 )
{
echo 'Credit Balance low! You have';
echo $amount == 0 ? '$00.00' : $amount;
echo ' remaining credits.';
}
else
{
echo 'No recent alerts...';
}
?>
if ($amount === 0) {
echo '$0.00';
} elseif ($amount < 5) {
echo "Credit Balance low! You have $amount remaining credits";
} else {
echo 'No recent alerts...';
}
You should do:
<?php
if ($amount === 0) {
echo '$00.00';
}
elseif ($amount < 5 && $amout > 0) {
echo 'Credit Balance low! You have' . $amount . ' remaining credits.';
}
else {
echo 'No recent alerts...';
}
?>

Categories