123guy Posted February 21, 2013 Share Posted February 21, 2013 hello. I am trying to calculate 7% sales tax inside of this script, however, every time I do it, it never works. It is either exponentially adding the tax, multiplying the price by 10, then adding tax,etc....and I am not sure what I was doing wrong. any ways, I would like it to calculate tax for any item that has the category of "artist" or "retail". can anyone help me out here? <?php $sql1= mysql_query("SELECT * FROM cart where cart_id='".$cid."' AND product123 !='' ORDER BY id DESC "); while($row1 = mysql_fetch_array($sql1)){ $price = $row1['price']; $product = $row1['product123']; $id = $row1['id']; $qty = $row1['quantity']; $category = $row1['category']; $price1 = $price * $qty; $total = $price1 + $total; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/274782-calculating-tax/ Share on other sites More sharing options...
Jessica Posted February 21, 2013 Share Posted February 21, 2013 There's nothing in your code that tries to add tax. you need to make an attempt at it. Show us the code that you had problems with. FYI: 7% is 0.07 of the total. Quote Link to comment https://forums.phpfreaks.com/topic/274782-calculating-tax/#findComment-1413925 Share on other sites More sharing options...
123guy Posted February 21, 2013 Author Share Posted February 21, 2013 <?php $sql1= mysql_query("SELECT * FROM cart where cart_id='".$cid."' AND product123 !='' ORDER BY id DESC "); while($row1 = mysql_fetch_array($sql1)){ $price = $row1['price']; $product = $row1['product123']; $id = $row1['id']; $qty = $row1['quantity']; $category = $row1['category']; if($category="artist"){ $price2= $price*'.07'; $price1 = $price * $qty; $total = $price1 + $total + $price2; } $price1 = $price * $qty; $total = $price1 + $total; } ?> this is one way I tried just addding onto artist category, but it still did it for all other categories as well, and it multiplied by two, then added the tax to the end. Quote Link to comment https://forums.phpfreaks.com/topic/274782-calculating-tax/#findComment-1413927 Share on other sites More sharing options...
Jessica Posted February 21, 2013 Share Posted February 21, 2013 (edited) = is assignment. == is comparison. You also need to move the $price1 part BEFORE your if, and the total part AFTER. Go read each line and say what it does in English. It's obvious why you're getting duplicate amounts. You're telling it to add and multiply twice. Also, '.07' is a string. Try using a NUMBER. Edited February 21, 2013 by Jessica Quote Link to comment https://forums.phpfreaks.com/topic/274782-calculating-tax/#findComment-1413930 Share on other sites More sharing options...
exeTrix Posted February 21, 2013 Share Posted February 21, 2013 Not sure what you were doing there mate, think you need o lay off the crack but this should work. Not tested <?php $sql1= mysql_query("SELECT * FROM cart where cart_id='".$cid."' AND product123 !='' ORDER BY id DESC "); while($row1 = mysql_fetch_array($sql1)){ $price = $row1['price']; $product = $row1['product123']; $id = $row1['id']; $qty = $row1['quantity']; $category = $row1['category']; if($category="artist"){ //add the tax, you guys are lucky only getting charged 7%!!!!! $price = $price + ( $price * 0.07 ); } $price1 = $price * $qty; $total = $price1 + $total; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/274782-calculating-tax/#findComment-1413965 Share on other sites More sharing options...
Barand Posted February 21, 2013 Share Posted February 21, 2013 The tax = price * 0.07; if you want to add the tax to the price $price *= 1.07; Quote Link to comment https://forums.phpfreaks.com/topic/274782-calculating-tax/#findComment-1413969 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.