I've got a shopping cart app which calculates the total price for a number of items. I want it to add in the sales tax (in this case 15%) but when I write the code and test it, it doesn't. Could you guys check it out, as I'm stumped and need this by tomorrow. Here's the code. Thanks
<?php
// display full cart
// album detail...
$a_query = "SELECT c.cart_id,c.quantity,pg.product_group_title,f.format_description,";
$a_query .= "pc.product_code_id,pc.product_code_price ";
$a_query .= "* CASE WHEN ISNULL(s.special_percentage) OR s.start_date>CURDATE() OR s.end_date<CURDATE() ";
$a_query .= "THEN 1 ELSE (100-s.special_percentage)*.01 END AS price FROM product_codes pc ";
$a_query .= "LEFT JOIN product_groups pg USING (product_group_id) ";
$a_query .= "LEFT JOIN formats f ON f.format_id=pc.format_id ";
$a_query .= "LEFT JOIN carts c ON pc.product_code_id=c.product_code_or_event_id ";
$a_query .= "LEFT JOIN specials s ON pg.product_group_id=s.product_group_or_event_id ";
$a_query .= "WHERE c.session_id='$s_id' AND c.item_type_id=1";
$a_result = mysql_query($a_query)
or die("<h3>*Server Error*</h3><p>Please try again later.</p>"
;
$albums = 0;
$album_total = 0;
$line = 0;
if(mysql_num_rows($a_result) == 0)
echo "<tr><td colspan=\"4\" align=\"center\">[NO ALBUMS SELECTED]</td></tr>\n";
else
{
while($a_row = mysql_fetch_assoc($a_result))
{
$c_id = $a_row["cart_id"];
$quantity = $a_row["quantity"];
$price = $a_row["price"];
$tax = 0.15;
$tax++;//Tax is now worth 1.15
$code = $a_row["product_code_id"];
$title = stripslashes( $a_row["product_group_title"] );
$format = stripslashes( $a_row["format_description"] );
$item_amount = $quantity * $price * $tax;
$albums += $quantity;
$album_total += $item_amount;
$bgcolor = $line % 2 == 1?"":" bgcolor=\"#EEEEEE\"";
$button_class = "changeCart" . ($bgcolor == ""?"1":"2"
;
$line++;
echo "<tr><td class=\"cart\" align=\"right\" nowrap=\"nowrap\"$bgcolor>\n<form action=\"cart.php\" ";
echo "method=\"POST\" onsubmit=\"return checkConfirm(this);\">\n";
echo "<input type=\"text\" name=\"quantity\" size=\"2\" value=\"$quantity\" />\n";
echo "<input type=\"hidden\" name=\"item\" value=\"$c_id\" />\n";
echo "<input type=\"hidden\" name=\"code\" value=\"$code\" />\n";
if( isset($HTTP_POST_VARS["last_page"]) )
$last_page = $HTTP_POST_VARS["last_page"];
else
$last_page = $HTTP_REFERER;
echo "<input type=\"hidden\" name=\"last_page\" value=\"$last_page\" />\n";
echo "<input type=\"submit\" class=\"$button_class\" name=\"update\" value=\"Change\" ";
echo "onclick=\"setButtonValue(this);\" ";
echo "title=\"Click here to change item quantity.\" /><br />\n";
echo "<input type=\"submit\" class=\"$button_class\" name=\"delete\" value=\"Delete\" ";
echo "onclick=\"button='delete';formSubmit=true;\" title=\"Click here to delete this item.\" /></form></td>";
echo "<td class=\"cart\"$bgcolor>$format -- $title</td>\n";
echo "<td class=\"cart\"$bgcolor align=\"center\">\$";
printf("%.2f", $price);
echo "</td>\n<td class=\"cart\"$bgcolor align=\"right\">\$";
printf("%.2f", $item_amount);
echo "</td></tr>\n";
}
echo "<tr><td colspan=\"3\" align=\"right\"><b>$albums ALBUMS:</b></td><td align=\"right\"><b>\$";
printf("%.2f", $album_total);
echo "</b></td></tr>\n";
}
<?php
// display full cart
// album detail...
$a_query = "SELECT c.cart_id,c.quantity,pg.product_group_title,f.format_description,";
$a_query .= "pc.product_code_id,pc.product_code_price ";
$a_query .= "* CASE WHEN ISNULL(s.special_percentage) OR s.start_date>CURDATE() OR s.end_date<CURDATE() ";
$a_query .= "THEN 1 ELSE (100-s.special_percentage)*.01 END AS price FROM product_codes pc ";
$a_query .= "LEFT JOIN product_groups pg USING (product_group_id) ";
$a_query .= "LEFT JOIN formats f ON f.format_id=pc.format_id ";
$a_query .= "LEFT JOIN carts c ON pc.product_code_id=c.product_code_or_event_id ";
$a_query .= "LEFT JOIN specials s ON pg.product_group_id=s.product_group_or_event_id ";
$a_query .= "WHERE c.session_id='$s_id' AND c.item_type_id=1";
$a_result = mysql_query($a_query)
or die("<h3>*Server Error*</h3><p>Please try again later.</p>"
$albums = 0;
$album_total = 0;
$line = 0;
if(mysql_num_rows($a_result) == 0)
echo "<tr><td colspan=\"4\" align=\"center\">[NO ALBUMS SELECTED]</td></tr>\n";
else
{
while($a_row = mysql_fetch_assoc($a_result))
{
$c_id = $a_row["cart_id"];
$quantity = $a_row["quantity"];
$price = $a_row["price"];
$tax = 0.15;
$tax++;//Tax is now worth 1.15
$code = $a_row["product_code_id"];
$title = stripslashes( $a_row["product_group_title"] );
$format = stripslashes( $a_row["format_description"] );
$item_amount = $quantity * $price * $tax;
$albums += $quantity;
$album_total += $item_amount;
$bgcolor = $line % 2 == 1?"":" bgcolor=\"#EEEEEE\"";
$button_class = "changeCart" . ($bgcolor == ""?"1":"2"
$line++;
echo "<tr><td class=\"cart\" align=\"right\" nowrap=\"nowrap\"$bgcolor>\n<form action=\"cart.php\" ";
echo "method=\"POST\" onsubmit=\"return checkConfirm(this);\">\n";
echo "<input type=\"text\" name=\"quantity\" size=\"2\" value=\"$quantity\" />\n";
echo "<input type=\"hidden\" name=\"item\" value=\"$c_id\" />\n";
echo "<input type=\"hidden\" name=\"code\" value=\"$code\" />\n";
if( isset($HTTP_POST_VARS["last_page"]) )
$last_page = $HTTP_POST_VARS["last_page"];
else
$last_page = $HTTP_REFERER;
echo "<input type=\"hidden\" name=\"last_page\" value=\"$last_page\" />\n";
echo "<input type=\"submit\" class=\"$button_class\" name=\"update\" value=\"Change\" ";
echo "onclick=\"setButtonValue(this);\" ";
echo "title=\"Click here to change item quantity.\" /><br />\n";
echo "<input type=\"submit\" class=\"$button_class\" name=\"delete\" value=\"Delete\" ";
echo "onclick=\"button='delete';formSubmit=true;\" title=\"Click here to delete this item.\" /></form></td>";
echo "<td class=\"cart\"$bgcolor>$format -- $title</td>\n";
echo "<td class=\"cart\"$bgcolor align=\"center\">\$";
printf("%.2f", $price);
echo "</td>\n<td class=\"cart\"$bgcolor align=\"right\">\$";
printf("%.2f", $item_amount);
echo "</td></tr>\n";
}
echo "<tr><td colspan=\"3\" align=\"right\"><b>$albums ALBUMS:</b></td><td align=\"right\"><b>\$";
printf("%.2f", $album_total);
echo "</b></td></tr>\n";
}