mardi 31 juillet 2018

(PHP) Can you use the outcome of an if statement in a calculation?

So I have two different prices per product in my database table. One is the "sale_price" and the other is the usual "price" of the product. I have made it so that if the "sale" column is set to "yes" in the database, then it will display the sale price of the product and vice-versa.

Here is my code that calculates the total price of the product by multiplying the quantity by the price of the product. You'll see that if $cart[sale'] is equal to 'yes' then it will create a variable called $tot: this will multiply $cart['sale_price'] of the product by $cart['quantity] else it will multiply the $cart['price'] by $cart['quantity]. This will display the total price PER PRODUCT...

<?php if($cartprod['salec'] == "yes"){

    $tot = number_format($cartprod['list_price'] * $cartprod['quantity'],2);

}else{

    $tot = number_format($cartprod['price'] *  $cartprod['quantity'],2);

}?> 

... I want to display the SUM of all of the totals in the cart but I can't seem to achieve it. Can anybody lead me in the right direction?

Aucun commentaire:

Enregistrer un commentaire