I'm trying to apply a discount to my basket for each pair of 2 items that cost £1.50 each but I can't seem to get the hang of it. I draw the results from two SQL queries/tables of 'discount' and 'basket' Here's the following I've tried:
$discount_count[] = 0;
$i = 0;
while ($row2 = mysql_fetch_array($data_set2)) {
if ($row['product'] == $row2['product']) {
foreach ($row2 as $current_row) {
$actual_price = $row['product_cost'] * $row2['quantity'];
$discount_total = round(($actual_price - ($actual_price * ($row2['discount_percent'] / 100))), 2);
$total_price = $discount_total / $row2['quantity'] * $row['quantity'];
$product_total = $row['product_cost'] * $row['quantity'];
if ($i % 2 && $total_price * $row2['quantity'] > $row['product_cost'] * $row2['quantity']){
echo "<br /><br />actual price per 2: £" . $actual_price;
echo "<br />discount total: £" . $discount_total;
echo "<br />actual minus discount: £";
echo $actual_price - $discount_total;
echo "<br />total price: £" . $total_price;
echo "<br />product cost: £" . $product_total;
$discount_count[] = $actual_price - $discount_total;
}
$i++;
}
}
}
Related
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: '."£".$ticketPrice;
echo "<br />";
echo "Age: ".$age;
echo "<br />";
echo "Member: ".$membership;
echo "<br />";
echo "Final Ticket Price: "."£".$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: '."£".$ticketPrice;
echo "<br />";
echo "Age: ".$age;
echo "<br />";
echo "Member: ".$membership;
echo "<br />";
echo "Final Ticket Price: "."£".$finalPrice;
For example :
$monthly_bill = 700
$paid_amount = 2100
Now I want to use a php loop which will print me 700, 700 and 400 to each line. How can I do this ?
I am doing following code but not working :
<?php
$paid_amount = 1800;
$monthly_bill = 700;
$counting_month = round($paid_amount / $monthly_bill);
for ($x=1; $x<=$counting_month; $x++) {
echo $insert = $paid_amount - $monthly_bill;
$paid_amount = $insert;
echo '<br/>';
}
Don't use for loop. Those run a fixed number of times, and your loop would keep running until well after the money runs out.
Use a do instead, and have its termination clause be when the money runs out:
$remaining = 1800;
$monthly = 700;
do {
echo $monthly;
$remaining -= $monthly;
if ($remaining < $monthly) {
$monthly = $remaining;
}
} while ($remaining > 0);
<?php
$amount = 1800;
$monthlybill = 700;
for ($i=0; $i<$amount/$monthlybill; $i++) {
echo "paid for " . ($i+1) . " month =" . $monthlybill . "<br>\n";
$amount -= $monthlybill;
}
if ($amount>0) {
echo "paid for " . ($i+1) . " month =" . $amount;
}
I deleted a question I posted earlier because I oversimplified the example, and got more flak for problems there, so have decided to post the whole problem here instead.
The Task
Much like this question here...Copying Excel's Circular Reference formula in PHP I am trying to change an Excel Circular reference into PHP.
Most of my frustration is not knowing the correct terms to google this for myself.
The Situation
I have converted all logic in the following spreadsheet into PHP below...
<?php
$vat = '1.2';
$mileage = 10;
$price = 6250.00;
$contract_term = 24;
$deposit_months = 6;
$otr_price = 9409.00;
$discount = 0;
$lom_a = 36.08;
$lom_b = 5.14;
$vehicletype = "Car";
$first_reg_fee = 55.00;
$emission_price = 30.00;
$fees = 21.03;
$margin = 50.00;
if (!empty($discount)) {
$discounted_otr_percent = (int)$discount/100;
$discounted_otr_amount = $discounted_otr_percent * $otr_price;
$discounted_otr = $otr_price-$discounted_otr_amount;
} else {
$discounted_otr = $otr_price;
}
$totaltobefunded = ($discounted_otr - $first_reg_fee - 0 /* Emissions */) / $vat;
print '<hr><p>Total to be Funded: <strong>' . $totaltobefunded . '</strong></p>';
$vatbal = $discounted_otr - $first_reg_fee - 0 /* Emissions */ - $totaltobefunded;
print '<p>VAT: <strong>' . $vatbal . '</strong></p>';
if($vehicletype == "Car") {
$balloon = $price / $vat * 0.9;
} else {
$balloon = $price * 0.9;
}
print '<p>CAP 90% price: <strong>' . $balloon . '</strong></p>';
$totalamountborrowed = $totaltobefunded - $balloon;
print '<p>Total Amount Borrowed: <strong>' . $totalamountborrowed . '</strong></p>';
$fundingrental = $totalamountborrowed * $lom_a / 1000;
print '<p>Funding Rental: <strong>' . $fundingrental . '</strong></p>';
$balloonrental = $balloon * $lom_b / 1000;
print '<p>Funding Rental: <strong>' . $balloonrental . '</strong></p>';
$monthlyfinancerental = $fundingrental + $balloonrental;
print '<p>Monthly Finance Rental: <strong>' . $monthlyfinancerental . '</strong></p>';
$monthlyrentalwithfees = $monthlyfinancerental + $fees;
print '<p>Monthly Finance Rental With Fees: <strong>' . $monthlyrentalwithfees . '</strong></p>';
$totalmargin = $margin * $contract_term;
print '<p>Total Margin: <strong>' . $totalmargin . '</strong></p>';
$totalrentalandfees = $monthlyrentalwithfees * ($contract_term + $deposit_months -1);
print '<p>Total Rental and Fees: <strong>' . $totalrentalandfees . '</strong></p>';
$totalcontractcost = $totalmargin + $totalrentalandfees;
print '<p>Total Contract Cost: <strong>' . $totalcontractcost . '</strong></p>';
$actualmonthlyrental = $totalcontractcost / ($contract_term + $deposit_months -1);
print '<p>Actual Monthly Rental: <strong>' . $actualmonthlyrental . '</strong></p>';
$deposit = $actualmonthlyrental * $deposit_months;
print '<p>Deposit: <strong>' . $deposit . '</strong></p>';
In the PHP code above, I have hard coded all of the variables for the time being just to get this working. In the real life example, these will change for each vehicle.
A Circular Reference exists between N19 (Total to be funded) , N34 (Actual Monthly Rental) and N36 (Deposit).
So at some point before, during, or after the FOR statement, there needs to be...
$totaltobefunded = $totaltobefunded - $deposit;
What I have tried
I have tried using the PHP for from the linked question on my example, but it just keeps taking away the deposit from the Total to be funded, and I am left with minus numbers.
In the example in the link, it has the following results...
1410
1211.19
1239.22221
1235.26966839
1235.826976757
1235.7483962773
1235.7594761249
1235.7579138664
1235.7581341448
1235.7581030856
1235.7581074649
1235.7581068474
This refines the number to the best possible outcome, and this is what I am trying to achieve. Just like my Excel document.
At the moment, my PHP prints out....
Total to be Funded: 7795
VAT: 1559
CAP 90% price: 4687.5
Total Amount Borrowed: 3107.5
Funding Rental: 112.1186
Funding Rental: 24.09375
Monthly Finance Rental: 136.21235
Monthly Finance Rental With Fees: 157.24235
Total Margin: 1200
Total Rental and Fees: 4560.02815
Total Contract Cost: 5760.02815
Actual Monthly Rental: 198.62166034483
Deposit: 1191.729962069
What I need help with
Using the PHP code above (which I have over simplified for this question btw), where should I put my "for" statement in order to correctly loop through the 3 numbers in the circular reference to get the result Im after?
As you can see in the Excel Screenshot, the target numbers are...
Total to be Funded: 6815.32
VAT: 2538.68
CAP 90% price: 4687.5
Total Amount Borrowed: 2127.82
Funding Rental: 76.7717456
Funding Rental: 24.09375
Monthly Finance Rental: 100.8654956
Monthly Finance Rental With Fees: 121.8954956
Total Margin: 1200
Total Rental and Fees: 3534.9693724
Total Contract Cost: 4734.9693724
Actual Monthly Rental: 163.27480594483
Deposit: 979.64883566897
I hope this makes sense to someone and they can assist. I'll answer any questions as best as I can.
The solution in PHP is as follows....
<?php
$vat = '1.2';
$mileage = 10;
$price = 6250.00;
$contract_term = 24;
$deposit_months = 6;
$otr_price = 9409.00;
$discount = 0;
$lom_a = 36.08;
$lom_b = 5.14;
$vehicletype = "Car";
$first_reg_fee = 55.00;
$emission_price = 30.00;
$fees = 21.03;
$margin = 50.00;
$deposit = 0;
if (!empty($discount)) {
$discounted_otr_percent = (int)$discount/100;
$discounted_otr_amount = $discounted_otr_percent * $otr_price;
$discounted_otr = $otr_price-$discounted_otr_amount;
} else {
$discounted_otr = $otr_price;
}
$max_iter = 100;
$max_change = 0.001;
$prev_target_val = PHP_INT_MAX;
$totaltobefunded = 0;
$crt_iter = 0;
while(($crt_iter < $max_iter) && (abs($prev_target_val - $totaltobefunded) > $max_change)) {
$crt_iter++;
$prev_target_val = $totaltobefunded;
$totaltobefunded = ($discounted_otr - $first_reg_fee - 0 /* Emissions */) / $vat - $deposit;
$vatbal = $discounted_otr - $first_reg_fee - 0 /* Emissions */ - $totaltobefunded;
if($vehicletype == "Car") {
$balloon = $price / $vat * 0.9;
} else {
$balloon = $price * 0.9;
}
$totalamountborrowed = $totaltobefunded - $balloon;
$fundingrental = $totalamountborrowed * $lom_a / 1000;
$balloonrental = $balloon * $lom_b / 1000;
$monthlyfinancerental = $fundingrental + $balloonrental;
$monthlyrentalwithfees = $monthlyfinancerental + $fees;
$totalmargin = $margin * $contract_term;
$totalrentalandfees = $monthlyrentalwithfees * ($contract_term + $deposit_months -1);
$totalcontractcost = $totalmargin + $totalrentalandfees;
$actualmonthlyrental = $totalcontractcost / ($contract_term + $deposit_months -1);
$deposit = $actualmonthlyrental * $deposit_months;
}
print '<p>Final Number: <strong>' . $actualmonthlyrental . '</strong></p>';
?>
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";
I making one counter, I am using PHP and almost everything was going fine but i found one small bug in it.
My first two step on counter is going fine that is $bid and $stax.
My last result $pay should be : 8138 + 814 + 448 = 9400 but it is giving me $9,399
My Output is:
Value $8,138 bid $814 tax $448 You Pay $9,399
Value $8,952 bid $895 tax $492 You Pay $10,339
Value $9,847 bid $985 tax $542 You Pay $11,373
Here is my php
<?php
$i = 0;
$v = 8138; // value : 8138
do {
$i++;
$bid = $v / 10; // output : $814
$ftax = $v + $bid;
$stax = $ftax / 20; // output : tax $448
$pay = $v + $bid + $stax; // 8138 + 814 + 448 = 9400
echo "Value $" . number_format($v) . " bid $" . number_format($bid) . " tax $" . number_format($stax) . " You Pay $" . number_format($pay) . "<br />";
$v = $v * 1.1;
} while ($i <= 2);
?>
Thanks in advance :)
Change your echo line to not drop decimals:
echo "Value $" . $v . " bid $" . $bid . " tax $" . $stax . " You Pay $" . $pay . "<br />";
Value $8138 bid $813.8 tax $447.59 You Pay $9399.39Value $8951.8 bid $895.18 tax $492.349 You Pay $10339.329Value $9846.98 bid $984.698 tax $541.5839 You Pay $11373.2619
So 8138 + 813.8 + 447.59 = 9399.39, which rounds down to 9399.
On the other hand, if you round immediately, 813.8 rounds up to 814 and 447.59 rounds up to 448, but you've just added 0.61 to your calculation before even starting, which obviously results in a higher number (9400).
This is why math and science teachers tell you not to round until the very end, since each time you do it, your answer gets less accurate.
if you dont want to use decimals, round off all division calculations by using round(). Also, multiplications with decimals. See it in action here
$i = 0;
$v = 8138; // value : 8138
do {
$i++;
$bid = round($v / 10); // output : $814
$ftax = $v + $bid;
$stax = round($ftax / 20); // output : tax $448
$pay = $v + $bid + $stax; // 8138 + 814 + 448 = 9400
echo "Value $" . number_format($v) . " bid $" . number_format($bid) . " tax $" . number_format($stax) . " You Pay $" . number_format($pay) . "<br />";
$v = round($v * 1.1);
Lack of casting/decimals be ye problem matey. har har har