High_-_Tek Posted March 18, 2006 Share Posted March 18, 2006 Hey All,I have a page for my company that needs fixing, I am so tired right now I cant think straight. But anyway, if a product(s) selected are under a certain catagory, then they need to add TAXBut the way it is working now, it still adds tax for non-tax items.Code:[code]foreach ($_SESSION['purchase_prods'] AS $arr){ $price=$arr[1] * $arr[2]; $sql=mysql_query("SELECT cat.catid AS catid FROM Products AS p, categories AS cat WHERE p.Product_ID='$arr[0]' AND p.Product_Cat_ID=cat.catid"); $result=mysql_fetch_array(mysql_query("SELECT cat.catid AS catid FROM Products AS p, categories AS cat WHERE p.Product_ID='$arr[0]' AND p.Product_Cat_ID=cat.catid")) OR DIE (mysql_error()); define('TAX_AMNT',.175); while(list($needsTAX,$price)=mysql_fetch_row($sql)) { $sub_total+=$price; if ($result['catid'] == 2) { $sub_total+=$price * TAX_AMNT; $vat=(TAX_AMNT * $sub_total); } //var_dump($needsTAX); } $vat=number_format($vat,2,'.',''); }[/code]Any and all help is very much appriciated :) Quote Link to comment https://forums.phpfreaks.com/topic/5202-tax-adding-when-not-supposed-to/ Share on other sites More sharing options...
Barand Posted March 18, 2006 Share Posted March 18, 2006 Your code is pretty confusing.Why the same query twice? [!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]$sql=mysql_query("SELECT cat.catid AS catid FROM Products AS p, categories AS cat WHERE p.Product_ID='$arr[0]' AND p.Product_Cat_ID=cat.catid"); $result=mysql_fetch_array(mysql_query("SELECT cat.catid AS catid FROM Products AS p, categories AS cat WHERE p.Product_ID='$arr[0]' AND p.Product_Cat_ID=cat.catid")) OR DIE (mysql_error());[/quote]Then there's [code]while(list($needsTAX,$price)=mysql_fetch_row($sql)) {[/code]You only select "catid" which will go into $needsTAX and $price will be NULL.Having got a value in $needsTAX it isn't used. Quote Link to comment https://forums.phpfreaks.com/topic/5202-tax-adding-when-not-supposed-to/#findComment-18481 Share on other sites More sharing options...
High_-_Tek Posted March 18, 2006 Author Share Posted March 18, 2006 Ignore all commented-out lines :DAnd I was advised by another member to add the while loop in there Quote Link to comment https://forums.phpfreaks.com/topic/5202-tax-adding-when-not-supposed-to/#findComment-18484 Share on other sites More sharing options...
Barand Posted March 18, 2006 Share Posted March 18, 2006 A couple of assumptionsThe product code in $arr[0] is uniqueThe criteria for adding tax is the the catid = 2[code]define('TAX_AMNT', 0.175);$subTotal = vatTotal = 0;echo '<pre>';foreach ($_SESSION['purchase_prods'] AS $arr) { $price=$arr[1] * $arr[2]; $sql=mysql_query("SELECT cat.catid AS catid FROM Products AS p INNER JOIN categories AS cat ON p.Product_Cat_ID = cat.catid WHERE p.Product_ID='$arr[0]' "); $res = mysql_query($sql) or die (mysql_error()); $needsTAX = mysql_result ($res, 0, 'catid') == 2; if ($needsTAX) { $price *= 1 + TAX_AMNT; $vat = TAX_AMNT * $price; } printf ('%-12s %8.2f %8.2f<br>', $arr[0], $price, $vat ); $subTotal += $price; $vatTotal += $vat;}printf ('%-12s %8.2f %8.2f', 'Total, $subTotal, $vatTotal );echo '</pre>';[/code] Quote Link to comment https://forums.phpfreaks.com/topic/5202-tax-adding-when-not-supposed-to/#findComment-18541 Share on other sites More sharing options...
High_-_Tek Posted March 18, 2006 Author Share Posted March 18, 2006 Hey Barand thanks for that :)But when i run I get this:Parse error: parse error, unexpected '=' in /home/www/petalfoods.com/Test/order5.php on line 155Which is probably attribuatted to this line:[code]$subTotal = vatTotal = 0;(156) is the line[/code]Thanks again Quote Link to comment https://forums.phpfreaks.com/topic/5202-tax-adding-when-not-supposed-to/#findComment-18658 Share on other sites More sharing options...
Barand Posted March 18, 2006 Share Posted March 18, 2006 Oops! Forgot the '$'. Try$subTotal = [!--coloro:#FF6666--][span style=\"color:#FF6666\"][!--/coloro--]$[!--colorc--][/span][!--/colorc--]vatTotal = 0; Quote Link to comment https://forums.phpfreaks.com/topic/5202-tax-adding-when-not-supposed-to/#findComment-18671 Share on other sites More sharing options...
High_-_Tek Posted March 18, 2006 Author Share Posted March 18, 2006 Ok, That worked great :)But now I get this:You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'Resource id #11' at line 1Thanks again! Quote Link to comment https://forums.phpfreaks.com/topic/5202-tax-adding-when-not-supposed-to/#findComment-18672 Share on other sites More sharing options...
Barand Posted March 19, 2006 Share Posted March 19, 2006 What is the query giving the error?Spotted it.Should be[code]$sql="SELECT cat.catid AS catid FROM Products AS p INNER JOIN categories AS cat ON p.Product_Cat_ID = cat.catid WHERE p.Product_ID='$arr[0]' ";[/code] Quote Link to comment https://forums.phpfreaks.com/topic/5202-tax-adding-when-not-supposed-to/#findComment-18692 Share on other sites More sharing options...
High_-_Tek Posted March 19, 2006 Author Share Posted March 19, 2006 Ok when I run the script I get this:36 10.00 2.7142 6.46 1.13Warning: printf(): Too few arguments in /home/www/petalfoods.com/Test/order5.php on line 180 Quote Link to comment https://forums.phpfreaks.com/topic/5202-tax-adding-when-not-supposed-to/#findComment-18696 Share on other sites More sharing options...
Barand Posted March 19, 2006 Share Posted March 19, 2006 Another typo! Missing quote.printf ('%-12s %8.2f %8.2f', 'Total[!--sizeo:4--][span style=\"font-size:14pt;line-height:100%\"][!--/sizeo--][!--coloro:#FF6666--][span style=\"color:#FF6666\"][!--/coloro--]'[!--colorc--][/span][!--/colorc--] [!--sizec--][/span][!--/sizec--], $subTotal, $vatTotal ); Quote Link to comment https://forums.phpfreaks.com/topic/5202-tax-adding-when-not-supposed-to/#findComment-18754 Share on other sites More sharing options...
High_-_Tek Posted March 19, 2006 Author Share Posted March 19, 2006 Thank You so much barand :)We are soo close :). The tax added into the subtotal is spot on, but the tax LISTED off off by a few dollars.Is there a way to splice the tax off when its added to the sub_total? Because only the LISTED tax is off and not the tax added to the totalBTW~ Here is what is outputted5 2.00 3.3210 4.00 3.3212 2.00 3.3242 1.18 0.2143 11.75 2.06Total 20.93 12.24The tax is supposed to be 1.93Thank You Again :) Quote Link to comment https://forums.phpfreaks.com/topic/5202-tax-adding-when-not-supposed-to/#findComment-18814 Share on other sites More sharing options...
Barand Posted March 20, 2006 Share Posted March 20, 2006 Needs the "else" bit below and alter calc lines so vat is calculated first, but I don't understand how total is OK.[code]if ($needsTAX) { $vat = TAX_AMNT * $price; $price += $vat; }else $vat = 0;[/code] Quote Link to comment https://forums.phpfreaks.com/topic/5202-tax-adding-when-not-supposed-to/#findComment-18990 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.