Hi All, i want to create a sales processor that can calculate the price based on weight for some items and quantity for others. My code below isn't working. Help please.
<?php
/******************************************************************************
*
*****************************************************************************/
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>Customer Purchases</title>
</head>
<?php
$time = time();
// Check if there is a cookie, if there isn't then redirect to login screen
if (!isset($_COOKIE['cookie_info'])) {
include ("index.htm");
exit;
}
// If there is a cookie, validate the cookie
else {
// Use Connect Script
include("connect.php");
// Include the validation of user file
include("validateuser.php");
// If user and password are correct
if (validateuser() == true) {
// Header
include("header.php");
login();
// Use cookie and Extract the cookie data (Username and Password)
$cookie_info = explode("-", $_COOKIE['cookie_info']);
$namecookie = $cookie_info[0];
$passcookie = $cookie_info[1];
// Get username from Database and it in a variable
$query = mysql_query("SELECT * FROM userdata WHERE userid = '$namecookie'");
$nameuser = mysql_result($query,0,13);
// Check the level of the user
$checklevel = mysql_query("SELECT * FROM userdata WHERE userid = '$namecookie'");
$checklevel1 = mysql_result($checklevel,0,14);
// If the user is staff
if (($checklevel1 == 2)||($checklevel1 == 1)) {
echo "<b><center><font face='Verdana'>Customer Purchases</font></b></center></p>";
?>
<body>
<form action="customerpurchases.php" method="POST">
<table border="0" id="table1" height="145">
<tr>
<td width="268" height="33"> </td>
<td height="33"> <font face="Verdana">ITEM ID:</font></td>
<td width="187" height="33">
<select size="1" name="itemid">
<?php
// Get all the items from the database and allow user to choose one
$items = mysql_query("SELECT * FROM items");
$items1 = mysql_num_rows($items);
for ($x = 0; $x < $items1; $x++) {
$itemid1= mysql_result($items,$x,0);
?>
<option><?php echo "$itemid1"; ?> </option>
<?php
}
?>
</select>
</td>
</tr>
<tr>
<td width="249"> </td>
<td> <font face="Verdana">Quantity:</font></td>
<td width="157">
<input type="text" name="quantity" value="1"/></td>
<td width="450">
</td>
</tr>
<tr>
<td width="249"> </td>
<td> <font face="Verdana">Weight (lb):</font></td>
<td width="157">
<input type="text" name="weight" value="1"/></td>
<td width="450">
</td>
</tr>
<tr>
<td width="249"> </td>
<td> <font face="Verdana">Discount($):</font></td>
<td width="157">
<input type="text" name="discount" value=""/></td>
<td width="450"><input type="hidden" name="tax" value="0"/>
</td>
</tr>
<tr>
<td width="249" height="47"> </td>
<td height="47"> </td>
<td height="47" width="157">
<input type="submit" value="Add Item">
<input type="reset" value="Reset"></form> </tr>
<td height="47" width="450">
<form action="customerpurchases_processtransaction.php" method="POST">
<input type="submit" value="Checkout"/></form></td>
</tr>
<tr>
<td width="249" height="28"> </td>
<td height="28"> </td>
<td height="28" width="157">
</form>
</tr>
<td height="28" width="450">
</td>
</p>
</tr>
</table>
</body>
</html>
<?php
$itemid = $_POST["itemid"];
$quantity = $_POST["quantity"];
$weight2 = $_POST["weight"];
$discount = $_POST["discount"];
$tax= $_POST["tax"];
// Check that item exists in database
$checkitem = mysql_query("SELECT * FROM items WHERE itemno = '$itemid'");
$checkitem1 = mysql_num_rows($checkitem);
// If there is no such item as the one typed in
if ($checkitem1 == 0) {
// If something was actually typing in the itemid
if ($itemid{1}) {
echo "<font face='Verdana' size='3'><center>No such item as $itemid<BR><BR>";
}
}
else {
$data = file('temp.txt'); // read the file
// If there is something in the file then use the number in the file as
// the table number and add the item there
if ($data[0]) {
// Check if the item is already in the table
$checkifitemexists = mysql_query("SELECT * FROM temp$data[0] WHERE itemid = '$itemid'");
$checkifitemexists1 = mysql_num_rows($checkifitemexists);
// If item is already in the table then add this quantity to the previous quantity
if ($checkifitemexists1 >= 1) {
$oldquantity = mysql_result($checkifitemexists,0,1);
$newquantity = $oldquantity + $quantity;
$oldquantity11 = mysql_result($checkifitemexists,0,2);
$newquantity22 = $oldquantity11 + $weight2;
$oldquantity111 = mysql_result($checkifitemexists,0,3);
$newquantity222 = $oldquantity111 + $discount;
$oldquantity1111 = mysql_result($checkifitemexists,0,4);
$newquantity2222 = $oldquantity1111 + $tax;
mysql_query("UPDATE temp$data[0] SET quantity = '$newquantity', weight2= '$newquantity22', discount2 = '$newquantity222', tax = '$newquantity2222' WHERE itemid = '$itemid'");
echo "<center><font face='Verdana' size='3'>Item updated: $itemid<br>Quantity added: $quantity<br>Weight added: $weight<br>Discount added: $discount<br>";
}
// Else insert the item into the table
else {
mysql_query("INSERT INTO temp$data[0] VALUES ('$itemid','$quantity','$weight2', '$discount', '$tax','0')");
echo "<center><font face='Verdana' size='3'>Item inserted: $itemid<br>Quantity: $quantity<br>Weight added: $weight<br>Discount added: $discount<br>";
}
}
// Make a temporary table
else {
// Make a temporary table and find if there is a temp table with the same
// name as we are about to name it
for ($x = 1; $x < 1000000; $x++) {
if (mysql_query("SELECT * FROM temp$x")) {
$temptabledone = 0;
}
else {
$temptabledone = 1;
break;
}
}
// Check if there is a temp table done
if ($temptabledone == 1) {
mysql_query("CREATE TABLE temp$x (itemid VARCHAR (50), quantity INT (10), weight2 FLOAT, discount2 FLOAT, tax FLOAT, cost FLOAT)");
// Add the temp table number to a new file
$filename ="temp.txt";
$handle= fopen($filename,'a');
$string = "$x";
fputs($handle, $string);
fclose($handle);
$temptabledone = 2;
}
// Else print a error message
else {
echo "An error occurred.";
}
// Add the item and quantity to the Temp table
if ($temptabledone == 2) {
// $weight2 = $weight * $quantity;
mysql_query("INSERT INTO temp$x VALUES ('$itemid','$quantity','$weight2','$discount2', '$tax', '0')");
echo "<center><font face='Verdana' size='3'>Item inserted: $itemid<br>Quantity: $quantity<br>Weight added: $weight<br>Discount added: $discount<br>";
}
// Print the number of items in the temp table
$countitem = mysql_query("SELECT * FROM temp$x");
$countitem1 = mysql_num_rows($countitem);
}
}
/* Print the items in the table */
$data = file('temp.txt'); // read the file
// If there is a number in the file then use that as the temp table number
if ($data[0]) {
// Print the items in the temp table
$countitem = mysql_query("SELECT * FROM temp$data[0]");
$countitem1 = mysql_num_rows($countitem);
echo "<center><font face='Verdana' size='3'>There are $countitem1 items in the cart<br><br>";
echo "
<div align='center'>
<table border='1' id='table2' width='100%' cellspacing='0'>
<tr>
<td width='106' align='center'><b><font face='Verdana'>Item ID</font></b></td>
<td width='348' align='center'><b><font face='Verdana'>Description</font></b></td>
<td width='92' align='center'><b><font face='Verdana'>Quantity</font></b></td>
<td width='92' align='center'><b><font face='Verdana'>Weight</font></b></td>
<td align='center'><b><font face='Verdana'>Item Price</font></b></td>
<td width='112' align='center'><b><font face='Verdana'>Stock after Sale</font></b></td>
<td width='112' align='center'><b><font face='Verdana'>Weight after Sale</font></b></td>
<td width='112' align='center'><b><font face='Verdana'>Discount</font></b></td>
<td width='103' align='center'><b><font face='Verdana'>Total Price</font></b></td>
<td width='103' align='center'><b><font face='Verdana'>Tax</font></b></td>
<td width='91' align='center'><b><font face='Verdana'>Remove?</font></b></td>
</tr>";
$fullcost = 0;
// Loop for all the times already in the temptable and print them out
for ($y = 0; $y < $countitem1; $y++) {
for ($z = 0; $z < 2; $z++) {
$query = mysql_query("SELECT * FROM temp$data[0]");
$itemandquantity = mysql_result($query,$y,$z);
// $itemandweight = mysql_result($query,$y,$z);
//$query = mysql_query("SELECT * FROM temp$data[0] WHERE itemid = '$itemid'");
if ($z == 0) {
echo "<tr><td width='106' align='center'><font face='Verdana' size='3'>$itemandquantity</font></td>";
$theitemid = $itemandquantity;
$itemdes = mysql_query("SELECT * FROM items WHERE itemno = '$itemandquantity'");
$itemdes1 = mysql_result($itemdes,0,1);
echo "<td width='348' align='center'><font face='Verdana'>$itemdes1</font></td>";
// $query = mysql_query("SELECT * FROM temp$data[0] WHERE itemid = '$itemid'");
}
else {
echo "<td width='92' align='center'><font face='Verdana'>$itemandquantity</font></td>";
//
$query = mysql_query("SELECT * FROM temp$data[0] WHERE itemid = '$itemid'");
$weightnn= mysql_result($query,0,2);
// $ddvvq = mysql_result($query,$y,5);
// echo "<td align='center'><font face='Verdana'>$newquantity22 </font></td>";
echo "<td align='center'><font face='Verdana'>$weightnn</font></td>";
$itemprice1 = mysql_result($itemdes,0,3);
echo "<td align='center'><font face='Verdana'>$$itemprice1</font></td>";
// Check the stock and print either low, medium or high
$checkstock = mysql_result($itemdes,0,2);
if ($checkstock == 0) {$ww=$checkstock-$itemandquantity;
echo "<td width='112' align='center'><font face='Verdana'>Out of Stock($ww)</font></td>";
}
if ($checkstock >= 1 && $checkstock <= 5) {$ww=$checkstock-$itemandquantity;
echo "<td width='112' align='center'><font face='Verdana'>Low($ww)</font></td>";
}
if ($checkstock >= 6 && $checkstock <= 20) {$ww=$checkstock-$itemandquantity;
echo "<td width='112' align='center'><font face='Verdana'>Medium($ww)</font></td>";
}
if ($checkstock > 20) {
$ww=$checkstock-$itemandquantity;
echo "<td width='112' align='center'><font face='Verdana'>High($ww)</font></td>";
}
$checkweight = mysql_result($itemdes,0,4);
if ($checkweight == 0) {$www=$checkweight-$weightnn;
echo "<td width='112' align='center'><font face='Verdana'>Out of Stock($www)</font></td>";
}
if ($checkweight >= 1 && $checkstock <= 5) {$www=$checkweight-$weightnn;
echo "<td width='112' align='center'><font face='Verdana'>Low($www)</font></td>";
}
if ($checkweight >= 6 && $checkweight <= 20) {$www=$checkweight-$weightnn;
echo "<td width='112' align='center'><font face='Verdana'>Medium($www)</font></td>";
}
if ($checkweight > 20) {$www=$checkweight-$weightnn;
echo "<td width='112' align='center'><font face='Verdana'>High($www)</font></td>";
}
$ddc = mysql_result($query,0,3);
// $dcost= mysql_result($query,$y,5);
$query2 = mysql_query("SELECT SUM(discount2) FROM temp$data[0] ");
$rowc = mysql_fetch_array($query2);
$ddvv = $rowc[0];
echo "<td width='103' align='center'><font face='Verdana'>$$ddc</font></td>";
//$itemid = $_POST["itemid"];
if (($_POST['itemid']=='Small Fish Soup')) {
$cal = $weightnn;
//$dc= mysql_result($query2,0,3);
// echo "<td width='103' align='center'><font face='Verdana'>$$totalprice</font></td>";
}
else
{
$cal = $itemandquantity;
}
$totalprice = $itemprice1*$cal;
$bnm = mysql_query("SELECT * FROM temp$data[0] WHERE itemid = '$itemid'");
// $checkifitemexists1w = mysql_num_rows($checkifitemexistsw);
$cost = mysql_result($bnm,0,5);
$cow = $cost + $totalprice ;
mysql_query("UPDATE temp$data[0] SET cost = '$cow' WHERE itemid = '$itemid'");
// $dcost= mysql_result($query,$y,5);
// $queryq = mysql_query("SELECT cost FROM temp$data[0] ");
//$rowcq = mysql_fetch_array($queryq);
//$rt = $rowcq['cost'];
echo "<td width='103' align='center'><font face='Verdana'>$$cow</font></td>";
// echo "<td width='103' align='center'><font face='Verdana'>$$ddvvq</font></td>";
// $dcost= mysql_result($query,$y,5);
//echo "<td width='103' align='center'><font face='Verdana'>$$dcost</font></td>";
$fullcost = $fullcost + $totalprice;
$fullcost2 = $fullcost-$ddvv;
$fulltax = 0.175*($fullcost2);
$finalcost = $fulltax + $fullcost2;
$tax1 = ($totalprice-$ddc)*0.175;
$dcost= mysql_result($query,0,5);
//echo "<td width='103' align='center'><font face='Verdana'>$$dcost</font></td>";
// echo "<td width='103' align='center'><font face='Verdana'>$$rowwd['cost']</font></td>";
mysql_query("UPDATE temp$data[0] SET tax = '$tax1' WHERE itemid = '$itemid'");
echo "<td width='103' align='center'><font face='Verdana'>$$tax1</font></td>";
echo "<td width='91' align='center'><font face='Verdana'>
<a href='customerpurchases_removeitem.php?itemid=$theitemid'>Remove</a>
</font></td></tr>";
}
}
//$queryb = mysql_query("SELECT * FROM temp$data[0] WHERE itemid = '$itemid'");
//$ddvvq = mysql_result($queryb,0,5);
// echo "<td width='103' align='center'><font face='Verdana'>$$ddvvq</font></td>";
}
// $queryb = mysql_query("SELECT * FROM temp$data[0] WHERE itemid = '$itemid'");
// $weightnn= mysql_result($query,$y,2);
//$ddvvq = mysql_result($queryb,0,5);
//echo "<td width='103' align='center'><font face='Verdana'>$$ddvvq</font></td>";
//echo "<td width='103' align='center'><font face='Verdana'>$$tax1</font></td>";
// echo "<td width='91' align='center'><font face='Verdana'>
// <a href='customerpurchases_removeitem.php?itemid=$theitemid'>Remove</a>
//</font></td></tr>";
echo "</table></div>";
// Print the total of all the items
echo "<div align='center'>
<table border='1' id='table3' width='100%' cellspacing='0'>
<tr>
<td width='559' td align='center'> </td>
<td align='center' width='219'><b><font face='Verdana'>Total Cost:</font></b></td>
<td width='103' align='center'><font face='Verdana'><b>$$fullcost</font></td>
<td width='91' align='center'> </td>
</tr>
<tr>
<td width='559' td align='center'> </td>
<td align='center' width='219'><b><font face='Verdana'>Total Discount:</font></b></td>
<td width='103' align='center'><font face='Verdana'><b>$$ddvv</font></td>
<td width='91' align='center'> </td>
</tr>
<tr>
<td width='559' td align='center'> </td>
<td align='center' width='219'><b><font face='Verdana'>Total Tax:</font></b></td>
<td width='103' align='center'><font face='Verdana'><b>$$fulltax</font></td>
<td width='91' align='center'> </td>
</tr>
<tr>
<td width='579' td align='center'> </td>
<td align='center' width='219'><b><font face='Verdana'>Grand Total:</font></b></td>
<td width='103' align='center'><font face='Verdana'><b>$$finalcost</font></td>
<td width='71' align='center'> </td>
</tr>
</table>
</div>";
}
}
// If user and password are not correct print error message
}
else {
echo "Incorrect username/password";
exit;
}
}
?>


