How to add values in php? - php

I thought I had this syntax right, but apparently I don't... here's what I have.
/* Price Calculation */
if ($stage="1") $price + 1000 = $price;
if ($stage="2") $price + 2500 = $price;
if ($stage="3") $price + 5000 = $price;
if ($chrome=on) $price + 100 = $price;
if ($bov=on) $price + 200 = $price;
if ($controller=on) $price + 500 = $price;
if ($filter=on) $price + 50 = $price;
print "<br /><u>Total: </u>";
echo $price;
The variables are already declared correctly, so I know it isn't that. The php has a problem with the ='s. What did I do wrong?

you should use == in condition
if ($stage="1")
should be
if ($stage=="1")
and adding should be
$price = $price + 1000;
Your code should be
<?php
/* Price Calculation */
if ($stage=="1") {
$price = $price + 1000;
}
if ($stage=="2") {
$price = $price + 2500;
}
if ($stage=="3") {
$price = $price + 5000;
}
if ($chrome==true) {
$price = $price + 100;
}
if ($bov==true) {
$price = $price + 200;
}
if ($controller==true) {
$price = $price + 500;
}
if ($filter==true) {
$price = $price + 50;
}
echo "<br /><u>Total: </u>";
echo $price;
?>
I don't know why you are using on. I think, it's a Boolean condition.

if ($stage=="1") $price = $price + 1000;
if ($stage=="2") $price = $price + 2500;
if ($stage=="3") $price = $price + 5000;
if ($chrome==on) $price = $price + 100;
if ($bov==on) $price = $price + 200;
if ($controller==on) $price = $price + 500;
if ($filter==on) $price = $price + 50;
print "<br /><u>Total: </u>";
echo $price;

Shouldn't it be $price = $price + 100; or $price += 100?
Also, you are mistaking your assignment for equality testing. = never meant equality in php---it means assignment (think of it $price = $price + 100 doesn't make sense it it is equality). To test for equality, use == (check your conditions).

You should change your code to:
if ($stage=="1")
$price = $price + 1000;
if ($stage=="2")
$price = $price + 2500;
if ($stage=="3")
$price = $price + 5000;
if ($chrome==on)
$price = $price + 100;
if ($bov==on)
$price = $price + 200;
if ($controller==on)
$price = $price + 500;
if ($filter==on)
$price = $price + 50;
print "<br /><u>Total: </u>";
echo $price;
Hope this helps :)

In addition to using == for comparison (since = is for assignment), consider adding using the += operator to add values to variables. Also, while it works, many folks consider it bad practice to skip your curly braces.
if ($stage=="1") {
$price += 1000;
}
...

if ($stage=="1") $price += 1000;
if ($stage=="2") $price += 2500;
if ($stage=="3") $price += 5000;
if ($chrome=="on") $price += 100; // if your want to use "on" instead of TRUE, use it as a string
if ($bov=="on") $price += 200;
if ($controller=="on") $price += 500;
if ($filter=="on") $price += 50;
echo "<br /><u>Total: </u>";
echo $price;

There are a couple of problems:
(1)
You are trying to assign the value of $price into $price + number, which is impossible. Try:
$price = $price + number;
(2)
Use the == operator for checking equality, like:
if ($stage == "1")
A single = is the assignment operator.

Related

Cart Price Discount according to quantity value

How can i apply 5% discount in base price after Quantity cross 15 in custom e Commerce.
Code:
$price = $_POST['price'];
$quantity = $_POST['quantity'];
$discount = $_POST['discount'];
$tax = $_POST['tax'];
$shipping = $_POST['shipping'];
$payments = $_POST['payments'];
$total = (($price * $quantity) + $shipping) - $discount;
$price = mysqli_real_escape_string($conn , $_POST['price']);
$quantity =mysqli_real_escape_string($conn , $_POST['quantity']);
if ($quantity > 15)
{
$discount = ($price/100)*5;
$price = $price - $discount;
}
else
{
$price = $price;
}

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
}

Why isn't working my notifications script with sales module?

The sales notifications is not working because this line amountArray[$idproduct] += $amount; is returning me offset. I don't know how to fix it.
My full function is this:
function saveAllSaleDetails($idsale, $sale) {
$this->conexion->startTransaction();
$amountArray = [];
try {
foreach ($sale as $detail):
$idproduct = $detail['id'];
$amount = $detail['amount'];
$price = $detail['price'];
$subtotal = $detail['subtotal'];
$iduser = 1;
$this->saveSaleDetail($idsale, $idproduct, $amount, $price, $subtotal, $iduser);
$amountArray[$idproduct] += $amount;
$stock = $this->product->getProductStock($idproduct);
$stock = $stock[0][0] - $amountArray[$idproduct];
if ($stock <= 20) {
$product = $this->product->getProductById($idproduct);
$message = $product[0][1]." stock is bellow 20.";
notification::add($message, $idproduct, 'warning', 'product.php');
}
endforeach;
$this->conexion->commit();
$this->conexion->cerrar();
return true;
} catch (Exception $e) {
$this->conexion->rollback();
$this->conexion->cerrar();
var_dump($e->getMessage());
return false;
}
}
The problem is because of this line,
$amountArray[$idproduct] += $amount;
The above statement can be expanded as
$amountArray[$idproduct] = $amountArray[$idproduct] + $amount;
Initially during the first iteration of foreach loop, $amountArray[1] and $amountArray[2] are not set, and hence you're getting these undefined offset error.
So instead of
$amountArray[$idproduct] += $amount;
Do this:
$amountArray[$idproduct] = isset($amountArray[$idproduct]) ? $amountArray[$idproduct] + $amount : $amount;

How to use proper switch or condition statement?

Can you help me solve this problem:
For that
$share Value <= 30000 the term is from 1 - 36 with 1.3% on the first
term and diminishing value of .65 on the next term while $share Value >= 30001 with diminishing value of .75
<?php
// Create a function that can accept parameters
function CalculateItem($share = 0,$terms = 0)
{
if($share <= 30000)
$multiplier = ($terms == "1")? .0130 : .0195;
elseif($share >= 30001)
$multiplier = ($terms == "1")? .0130 : .0205;
if(empty($multiplier))
return;
$data['share'] = $share;
$data['terms'] = $terms;
$data['interest'] = ($share * $multiplier);
$data['service_fee'] = ($share * .01);
$data['filling_fee'] = 30;
$data['cash_on_hand'] = $share - ($data['service_fee'] + $data['interest'] + $data['filling_fee']);
$data['loan_receivable'] = $data['cash_on_hand'] + $data['service_fee'] + $data['interest'] + $data['filling_fee'];
$data['debit'] = $data['loan_receivable'];
$data['credit'] = $data['interest'] + $data['service_fee'] + $data['filling_fee'] + $data['cash_on_hand'];
return $data;
}
// Get the state of data. Anything but false will trigger the breakout table
$data = (isset($_POST['calculate']))? CalculateItem($_POST['share'],$_POST['terms']) : false;
?>
While the rate is fixed you may do it as --
$rate=.0130;
if($share <= 30000)
$multiplier = $rate + ($terms-1)*(0.65);
elseif($share >= 30001)
$multiplier = $rate + ($terms-1)*(0.75);
By this way , you may calculate as per all terms

variables outside/inside of a function: PHP beginner

Trying to learn about using functions in PHP: if I want start a variable at a value of 0, and use assignment-operators to add to it, how would I do that within a function? Kinda hard to describe in words, so, here's an example:
<?php
function tally($product){
// I want these to be the starting values of these variables (except for $tax, which will remain constant)
$tax = 0.08;
$total_price = 0;
$total_tax = 0;
$total_shipping = 0;
$grand_total = 0;
// So, the program runs through the function:
if($product == 'Candle Holder'){
$price = 11.95;
$shipping = 0;
$total_price += $price;
$total_tax += $tax * $price;
$total_shipping += $shipping * $price;
$grand_total = ($total_price + $total_tax + $total_shipping);
}
else if($product == 'Coffee Table'){
$price = 99.50;
$shipping = 0.10;
$total_price += $price;
$total_tax += $tax * $price;
$total_shipping += $shipping * $price;
$grand_total = ($total_price + $total_tax + $total_shipping);
}
else if($product == 'Floor Lamp'){
$price = 44.99;
$shipping = 0.10;
$total_price += $price;
$total_tax += $tax * $price;
$total_shipping += $shipping * $price;
$grand_total = ($total_price + $total_tax + $total_shipping);
}else{
echo '<li>Missing a product!</li>';
}
// And then, it echoes out each product and price:
echo '<li>'.$product.': $'.$price;
// To test it, I echo out the $grand_total to see if it's working:
echo '<br>---'.$grand_total;
} //end of function tally()
// End of the function, but every time I call
tally('Candle Holder');
tally('Coffee Table');
tally('Floor Lamp');
?>
it doesn't add to the $grand_total of all three products.
I know it's because the function runs through the beginning (top) and resets $grand_total to 0. If I try to place the original-value variables outside of the function, the browser returns an error: undefined variable.
I know this is jumbled, so tell me if I need to supply more info.
Thanks!
EDIT
Found another way to simplify it. Totally had forgotten about the return function:
<B>Checkout</B><br>
Below is a summary of the products you wish to purchase, along with totals:
<?php
function tally($product, $price, $shipping){
$tax = 0.08;
$total_tax = $tax * $price;
$total_shipping = $shipping * $price;
$grand_total = ($total_price + $total_tax + $total_shipping);
echo '<li>'.$product.': $'.$grand_total;
return $grand_total;
} //end of function tally()
?>
<ul>
<?php
$after_tally = tally('Candle Holder', 11.95, 0);
$after_tally += tally('Coffee Table', 99.50, 0.10);
$after_tally += tally('Floor Lamp', 49.99, 0.10);
?>
</ul>
<hr>
<br>
<B>Total (including tax and shipping): $<? echo number_format($after_tally, 2); ?></B>
Does exactly what I wanted it to!
Thanks for the help! I know that arrays could help with this, but I'm just now getting to that in my lessons.
You need to be aware of scope. functions don't have access to standard variables unless you either pass them to the function or globalize them in the function. Ideally you pass what you need to the function.
In your case you're expecting a function - an isolated process - to work as a constantly running program... or something of the sort. Perhaps what you need to do is reconsider what you expect from tally($product)...
<?php
function tally($product)
{
$tax = 0.08;
$total_price = 0;
$total_tax = 0;
$total_shipping = 0;
$grand_total = 0;
if($product == 'Candle Holder'){
$price = 11.95;
$shipping = 0;
$total_price += $price;
$total_tax += $tax * $price;
$total_shipping += $shipping * $price;
$grand_total = ($total_price + $total_tax + $total_shipping);
}
else if($product == 'Coffee Table'){
$price = 99.50;
$shipping = 0.10;
$total_price += $price;
$total_tax += $tax * $price;
$total_shipping += $shipping * $price;
$grand_total = ($total_price + $total_tax + $total_shipping);
}
else if($product == 'Floor Lamp'){
$price = 44.99;
$shipping = 0.10;
$total_price += $price;
$total_tax += $tax * $price;
$total_shipping += $shipping * $price;
$grand_total = ($total_price + $total_tax + $total_shipping);
}
return $grand_total;
}
$grand_total = 0;
$grand_total += tally('Candle Holder');
$grand_total += tally('Floor Lamp');
?>
<ul>
<li>Candle Holder: $<?php echo tally('Candle Holder'); ?></li>
<li>Floor Lamp: $<?php echo tally('Floor Lamp'); ?></li>
<li>Total: $<?php echo $grand_total; ?></li>
</ul>
In this example you can see I use $grand_total inside and outside the function. They are unrelated. The function does not know about the outside $grand_total because it is not within its scope.
This function is used for only one thing - tally the total for that product. It's up to you to compound the results for each product.
You could write a function to tally all, or a class to handle it all, but that's another subject. This example just explains why it is not doing what you are asking for
The problem as others have stated is scope. In your situation with the code you have, maybe use a static var (not preferred), pass true as second arg to reset $grand_total to 0:
function tally($product, $reset=false)
{
//your vars
static $grand_total = 0;
if($reset) {
$grand_total = 0;
}
//your code
return $grand_total;
}
Better would be to just return the $grand_total and sum it in your code that calls the function.
However, I would consider using an object. At a minimum, add the products and prices into an array in a file that can be included and then loop over when needed:
$tax = 0.08;
$products = array(
'Candle Holder' => array(
'price' => 11.95,
'shipping' => 0,
),
'Coffee Table' => array(
'price' => 99.50,
'shipping' => .10,
),
);
$grand_total = 0;
foreach($products as $product => $values) {
$total = $values['price'] + ($tax * $values['price']) + ($values['price'] * $values['shipping']);
$grand_total += $total;
echo '<li>'.$product.': $'.$values['price'];
}
<?php
$input_product_array = array("Candle Holder","Coffee Table");
function tally($incomingarray){
$tax = 0.08;
$total_price = 0;
$total_tax = 0;
$total_shipping = 0;
$grand_total = 0;
$return_product_array = array(); // we're doing this so we can return multiple product row entries and a single grand total it'll make sense shortly
foreach ($incomingarray as $key=>$productname) {
if($productname == 'Candle Holder'){
$price = 11.95;
$shipping = 0;
$total_price += $price;
$total_tax += $tax * $price;
$total_shipping += $shipping * $price;
$grand_total = ($total_price + $total_tax + $total_shipping);
$return_product_array[] = '<li>'.$productname .': $'.$price.'</li>';
} else if($productname == 'Coffee Table'){
$price = 99.50;
$shipping = 0.10;
$total_price += $price;
$total_tax += $tax * $price;
$total_shipping += $shipping * $price;
$grand_total = ($total_price + $total_tax + $total_shipping);
$return_product_array[] = '<li>'.$productname .': $'.$price.'</li>';
} else if($productname == 'Floor Lamp'){
$price = 44.99;
$shipping = 0.10;
$total_price += $price;
$total_tax += $tax * $price;
$total_shipping += $shipping * $price;
$grand_total = ($total_price + $total_tax + $total_shipping);
$return_product_array[] = '<li>'.$productname .': $'.$price.'</li>';
}
}
//now we construct a final return array which contains all of our products array in one entry and then the grandtotal/totalprice/totaltax/total shipping in other columns
$returnarray = array($return_product_array,$grand_total,$total_shipping,$total_tax,$total_price);
return $returnarray;
}
$returnedinfo = tally($input_product_array);
//now we can spit out our products
foreach ($returnedinfo[0] as $key=>$productlist) { // always going to be an array returned from function and element 0 will always be items
echo $productlist;
}
echo "totals<br />";
echo "Pre-Tax Total = $".$returnedinfo[4];
echo "Total Tax = $".$returnedinfo[3];
echo "Total Shipping = $".$returnedinfo[2];
echo "Grand Total = $".$returnedinfo[1];
?>
Something like this
This happens due to a programming concept called 'scope'
While the function's code is run, the values are just what you want them to be.
In order to solve the problem, first of all declare the variables OUTSIDE the function.
<?php
$tax = 0.08;
$total_price = 0;
$total_tax = 0;
$total_shipping = 0;
$grand_total = 0;
tally('Candle Holder');
tally('Coffee Table');
tally('Floor Lamp');
?>
Then inside the function tally(), add this piece of code before your if/else statements
global $tax;
global $total_price;
global $total_tax;
global $total_shipping;
global $grand_total;
This pretty much tells the function that there is a variable called "tax" that is outside its current "scope", and that it should link it into its current memory.
Then when the function updates the value of tax, it is updating the main variable outside of its "scope", hence preserving the values
(I gave a example of tax, same for every other variable you declare global)
Ps: I understand i might of got your question wrong, if so do tell me and i will update the answer.

Categories