PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
I have a system developed where the script checks the Product CAT Id against a DB ... if the Cat is returned as 2, then it adds tax (17.5%)... but when I select a tax-able item and a non-tax-able item... it adds it up for both..
foreach ($_SESSION['purchase_prods'] AS $arr){
$tot+=$arr[1] * $arr[2];
$qry=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 AND cat.catid='2'") OR DIE (mysql_error());
$result=mysql_num_rows($qry);
switch ($result){
case '1':
$vat=(.175 * $tot);
$hax=$sub_total+$vat;
$vat=number_format($vat,2,'.','');
$sub_total=$hax;
break;
default:
$vat='0.00';
break;
}
}
$sub_total=number_format($sub_total, 2, '.', "");
You'll have to ask it for each item separately and add tax to only those that need it. If you have an array it should be fairly easy to sum up the array values at the end to get the total.
Personally, I prefer to do all calculations on the cost of items in the SQL .. including adding VAT .. ideally in a stored procedure, but just in the raw SQL is ok if you're not running MySQL 5 yet.
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());
if ($result['catid'] == 2){
$needsTAX=TRUE;
}
define('TAX_AMNT',.175);
while(list($needsTAX,$price)=mysql_fetch_row($sql)) {
$sub_total+=$price;
$vat=(.175 * $sub_total);
$sub_total+=$needsTAX ? $price * TAX_AMNT : '0.00';
}
$vat=number_format($vat,2,'.','');
}