I have a problem, where if I check 'remove' for an item in the cart, then hit 'update', it removes all the items in the cart. Any ideas?
Code:
<?
function AddBatch($_POST,$Session_Prefix)
{
// Check For Batch Count
if (is_numeric($_POST['Batch_Count']))
{
$Batch_Count = $_POST['Batch_Count'];
} else
{
// Error, No ID, Send Away
$Batch_Count = 0;
}
// Using a for loop, check through each field in form looking for check box
for ($i = 0; $i < $Batch_Count; $i++)
{
$Cur_CheckBox = "checkbox".$i;
// If box is checked, add item to cart
if ($_POST[$Cur_CheckBox]==1)
{
// Parse Optional Inputs Into Title & Serial
$title_In = cleanvalue($_POST['Title'.$i]);
$serialnum_In = cleanvalue($_POST['SerialNum'.$i]);
if (is_numeric($_POST['Price'.$i]))
{
$price_In = $_POST['Price'.$i];
} else
{
// Error, No ID, Send Away
$price_In = '';
}
if (is_numeric($_POST['Shipping1'.$i]))
{
$shipping1_In = $_POST['Shipping1'.$i];
} else
{
// Error, No ID, Send Away
$shipping1_In = '';
}
if (is_numeric($_POST['Shipping2'.$i]))
{
$shipping2_In = $_POST['Shipping2'.$i];
} else
{
// Error, No ID, Send Away
$shipping2_In = '';
}
if (is_numeric($_POST['Quantity'.$i]))
{
$Quantity_In = $_POST['Quantity'.$i];
} else
{
// Error, No ID, Send Away
$Quantity_In = '1';
}
// Add Item To Cart
AddItemToCart($title_In,$serialnum_In,$price_In,$shipping1_In,$shipping2_In,$Session_Prefix,$Quantity_In);
} // end if
} // end for
header("location:ShoppingCart.php");
} // end function
// Add Item To Cart ************************************************************************************************************
function AddItemToCart($Title,$SerialNum,$Price,$Shipping1,$Shipping2,$Session_Prefix,$Quantity='1')
{
// Check To See If Cart Exists, And Set Count Accordingly
if ($_SESSION[$Session_Prefix.'Item_Count']!="")
{
$Item_Row = $_SESSION[$Session_Prefix.'Item_Count'] + 1;
} else
{
$Item_Row = 1;
}
// check to see if the new item's serial num is already in the shopping cart
$searchflag = "";
for ($i = 1; $i < $Item_Row; $i++)
{
$searchserialnum = "serialnum".$i;
if ($_SESSION[$Session_Prefix.$searchserialnum]==$SerialNum)
{
$searchflag = $i;
}
}
//If no match was found add the new info as a new entry (Item_Row)
if ($searchflag=="")
{
// Prepare Cart Field Titles For New Entry
$Title_serialnum = "serialnum".$Item_Row;
$Title_shippinga = "shippinga".$Item_Row;
$Title_shippingb = "shippingb".$Item_Row;
$Title_title = "title".$Item_Row;
$Title_price = "price".$Item_Row;
$Title_quantity = "quantity".$Item_Row;
$_SESSION[$Session_Prefix.$Title_serialnum] = $SerialNum;
$_SESSION[$Session_Prefix.$Title_title] = $Title;
$_SESSION[$Session_Prefix.$Title_shippinga] = $Shipping1;
$_SESSION[$Session_Prefix.$Title_shippingb] = $Shipping2;
$_SESSION[$Session_Prefix.$Title_price] = $Price;
$_SESSION[$Session_Prefix.$Title_quantity] = $Quantity;
$_SESSION[$Session_Prefix."Item_Count"] = $Item_Row;
} else
{
//Otherwise just update the quanity of this item/serialnum
$quantity = "quantity".$searchflag;
$newquantity = $_SESSION[$Session_Prefix.$quantity] + $Quantity;
$_SESSION[$Session_Prefix.$quantity] = $newquantity;
} // end searchflag if
header("location:ShoppingCart.php");
}
// ****************************************************************************************************************************************
function DisplayEditableCart($Session_Prefix,$Currency_Symbol,$Currency_Code)
{
$RowCount=$RowCount+1;
$TempCol=$RowCount%2+1;
$Output = '';
$Output = $Output.'<div align="center" class="Bold">
<table width="600" border="1" align="center" cellpadding="0" cellspacing="0">';
if (isset($_SESSION[$Session_Prefix.'Item_Count']) && $_SESSION[$Session_Prefix.'Item_Count']!=0)
{
$CheckOrd=1;
$Output = $Output.'<tr>
<td width="16%" class="RepeatRowColor'.$TempCol.'"><div align="center" class="FieldTitleColor'.$TempCol.'">Qty</div></td>
<td width="10%" class="RepeatRowColor'.$TempCol.'"><div align="center" class="FieldTitleColor'.$TempCol.'">Remove</div></td>
<td colspan="2" class="RepeatRowColor'.$TempCol.'"><div align="center" class="FieldTitleColor'.$TempCol.'">Item</div></td>
<td width="15%" class="RepeatRowColor'.$TempCol.'"><div align="right" class="FieldTitleColor'.$TempCol.'">Price</div></td>
</tr>
<form id="form2" name="form2" method="post" action="ShoppingCart.php">';
//Setup Cart Vars
$displaysubtotal=0;
$displayshippingtotal=0;
$displaytotal=0;
$displaynewshipping=0;
$displaynewprice=0;
$final = $_SESSION[$Session_Prefix.'Item_Count']+1;
$cnt=1;
for ($i = 1; $i < $final; $i++)
{
$RowCount=$RowCount+1;
$TempCol=$RowCount%2+1;
// Setup Vars for this Row
$displayshippinga = "shippinga".$i;
$displayshippingb = "shippingb".$i;
$displaytitle = "title".$i;
$displayprice = "price".$i;
$displayquantity = "quantity".$i;
$displayremove = "remove".$i;
// Calculate The Total Shipping For This Item
if ($_SESSION[$Session_Prefix.$displayquantity]>1)
{
$displaynewshipping = ($_SESSION[$Session_Prefix.$displayshippinga] + ($_SESSION[$Session_Prefix.$displayshippingb] * ($_SESSION[$Session_Prefix.$displayquantity] - 1)));
} else
{
$displaynewshipping = $_SESSION[$Session_Prefix.$displayshippinga];
}
// Calculate The price For This Item
$displaynewprice = $_SESSION[$Session_Prefix.$displayprice] * $_SESSION[$Session_Prefix.$displayquantity];
$Output = $Output.'<tr>
<td class="RepeatRowColor'.$TempCol.'"><div align="center" class="DataTextColor'.$TempCol.'"><input name="'.$displayquantity.'" type="text" value="'.$_SESSION[$Session_Prefix.$displayquantity].'" size="3" maxlength="4"></div></td>
<td class="RepeatRowColor'.$TempCol.'"><div align="center" class="DataTextColor'.$TempCol.'"><input name="'.$displayremove.'" type="checkbox" value="1"></div></td>
<td colspan="2" class="RepeatRowColor'.$TempCol.'"><div align="left" class="DataTextColor'.$TempCol.'">'.$_SESSION[$Session_Prefix.$displaytitle].'<br />
'.$_SESSION[$Session_Prefix.$displayserialnum].'</div></td>
<td class="RepeatRowColor'.$TempCol.'"><div align="right" class="DataTextColor'.$TempCol.'">'.$Currency_Symbol.' '.number_format($displaynewprice, 2, '.', '').'</div></td>
</tr>';
// Update The Total Vars
$displaysubtotal = $displaysubtotal + $displaynewprice;
$displayshippingtotal = $displayshippingtotal + $displaynewshipping;
} // end For
$RowCount=$RowCount+1;
$TempCol=$RowCount%2+1;
// Calculate Totals
$displaytotal = ($displayshippingtotal + $displaysubtotal);
$Output = $Output.'<tr>
<td colspan="4" class="RepeatRowColor'.$TempCol.'"><div align="right" class="FieldTitleColor'.$TempCol.'">*Total ('.$Currency_Code.'):</div></td>
<td class="RepeatRowColor'.$TempCol.'"><div align="right" class="DataTextColor'.$TempCol.'">'.$Currency_Symbol.' '.number_format($displaysubtotal, 2, '.', '').'</div></td>
</tr>';
$RowCount=$RowCount+1;
$TempCol=$RowCount%2+1;
$Output = $Output.'<tr>
<td colspan="5" class="RepeatRowColor'.$TempCol.'"><div align="center" class="FieldTitleColor'.$TempCol.'">*Shipping
costs will be calculated upon checkout.</div></td>
</tr>';
$RowCount=$RowCount+1;
$TempCol=$RowCount%2+1;
$Output = $Output.'<tr><td colspan="5" class="RepeatRowColor'.$TempCol.'"><div align="center" class="DataTextColor'.$TempCol.'">
<table width="100%" border="1" cellspacing="0" cellpadding="0">
<tr>
<td width="50%"><div align="center">
<input name="cmd" type="hidden" id="cmd" value="4">
<table width="0%" border="2" bordercolor="#FFFFFF">
<tr>
<td><div align="center" class="">
<input name="Submit" type="submit" value="Update"/>
</div></td>
</tr>
</table>
</div></td>
<td><div align="center">
<table width="0%" border="2" bordercolor="#FFFFFF">
<tr>
<td><div align="center" class="">
<a href="ChoosePayment.php" class="InSiteLink"><img src="Checkout.jpg"</a>
</div></td>
</tr>
</table>
</div></td>
<td><div align="center">
<table width="0%" border="2" bordercolor="#FFFFFF">
<tr>
<td><div align="center" class="">
<a href="javascript:history.go(-1)"><img src="Continue Shopping.jpg"</a>
</tr>
</table>
</div></td>
</tr>
</form></table>';
} else
{
$Output = $Output.'<tr>
<td colspan="5" class="RepeatRowColor'.$TempCol.'"><div align="center" class="DataTextColor'.$TempCol.'">...Cart is Empty... <br />
</div></td>
</tr>
</table>';
} // end if
$Output = $Output.'
</div>';
return $Output;
}
//********************************************************************************************************************
function UpdateCart($_POST,$Session_Prefix)
{
// Loop Through Form Data
for ($updatecnt = 1; $updatecnt < ($_SESSION[$Session_Prefix.'Item_Count'] + 1); $updatecnt++)
{
// Create Field Names
$updatequantity = "quantity".$updatecnt;
$updateremove = "remove".$updatecnt;
// Check Numeric Inputs
if (is_numeric($_POST[$updatequantity]))
{
// Quanity is numeric, update cart
$_SESSION[$Session_Prefix.$updatequantity] = $_POST[$updatequantity];
}
if (is_numeric($_POST[$updateremove]))
{
// Remove is numeric, update cart
if ($_POST[$updateremove]=="1")
{
$_SESSION[$Session_Prefix.$updatequantity] = 0;
}
}
//Make Empty OR non-numeric quantities into zeros
if ($_SESSION[$Session_Prefix.$updatequantity]=="" || !is_numeric($_SESSION[$Session_Prefix.$updatequantity]))
{
$_SESSION[$Session_Prefix.$updatequantity] = "0";
}
// Delete Serial Num for 0 quantity / removed items
if ($_SESSION[$Session_Prefix.$updatequantity]=="0")
{
$updateserialnum = "serialnum".$updatecnt;
$_SESSION[$Session_Prefix.$updateserialnum] = "";
}
}
// Repair The Cart
// Run the Repair Process Until No Gaps Are Found!
$gaps = "1";
while ($gaps != 0)
{
$gaps = "0";
// Main Loop For Dealing With Removing Deleted Items
for ($updatecnt = 1; $updatecnt < ($_SESSION[$Session_Prefix.'Item_Count'] + 1); $updatecnt++)
{
$updateserialnum = "serialnum".$updatecnt;
// Check to see if the serial num exists
if ($_SESSION[$Session_Prefix.$updateserialnum]=="")
{
// No Serialnum - gap found
$gaps = "1";
// Set Field Titles For This And THis+1 Row:
$nextrow=$updatecnt+1;
$previousrow=$updatecnt-1;
$updateserialnum = "serialnum".$updatecnt;
$updateserialnumnext = "serialnum".$nextrow;
$updateshippinga = "shippinga".$updatecnt;
$updateshippinganext = "shippinga".$nextrow;
$updateshippingb = "shippingb".$updatecnt;
$updateshippingbnext = "shippingb".$nextrow;
$updatetitle = "title".$updatecnt;
$updatetitlenext = "title".$nextrow;
$updateprice = "price".$updatecnt;
$updatepricenext = "price".$nextrow;
$updatequantity = "quantity".$updatecnt;
$updatequantitynext = "quantity".$nextrow;
// Check To See If EOF
if ($nextrow < ($_SESSION[$Session_Prefix.'Item_Count'] + 1))
{
// Not EOF
// Copy NextRow Info Down To This Row (and delete NextRow's SerialNum)
$_SESSION[$Session_Prefix.$updateserialnum] = $_SESSION[$Session_Prefix.$updateserialnumnext];
$_SESSION[$Session_Prefix.$updateshippinga] = $_SESSION[$Session_Prefix.$updateshippinganext];
$_SESSION[$Session_Prefix.$updateshippingb] = $_SESSION[$Session_Prefix.$updateshippingbnext];
$_SESSION[$Session_Prefix.$updatetitle] = $_SESSION[$Session_Prefix.$updatetitlenext];
$_SESSION[$Session_Prefix.$updateprice] = $_SESSION[$Session_Prefix.$updatepricenext];
$_SESSION[$Session_Prefix.$updatequantity] = $_SESSION[$Session_Prefix.$updatequantitynext];
$_SESSION[$Session_Prefix.$updateserialnumnext] = "";
} else
{
// EOF
//
$_SESSION[$Session_Prefix.$updateserialnum] = "";
$_SESSION[$Session_Prefix.$updateshippinga] = "";
$_SESSION[$Session_Prefix.$updateshippingb] = "";
$_SESSION[$Session_Prefix.$updatetitle] = "";
$_SESSION[$Session_Prefix.$updateprice] = "";
$_SESSION[$Session_Prefix.$updatequantity] = "";
$_SESSION[$Session_Prefix."Item_Count"] = $previousrow;
} // end EOF check if
}
} // end main loop for dealing with removing items
} //endwhile
header("location:ShoppingCart.php?u");
}
// ****************************************************************************************************************************************
function DisplayCartWShipping($Session_Prefix,$Currency_Symbol,$Currency_Code)
{
$RowCount=$RowCount+1;
$TempCol=$RowCount%2+1;
$Output = '';
$Output = $Output.'<table width="600" border="1" align="center" cellpadding="0" cellspacing="0">';
if (isset($_SESSION[$Session_Prefix.'Item_Count']) && $_SESSION[$Session_Prefix.'Item_Count']!=0)
{
$CheckOrd=1;
$Output = $Output.'<tr>
<td width="11%" class="RepeatRowColor'.$TempCol.'"><div align="center" class="FieldTitleColor'.$TempCol.'">Qty</div></td>
<td width="52%" class="RepeatRowColor'.$TempCol.'"><div align="center" class="FieldTitleColor'.$TempCol.'">Item</div></td>
<td width="21%" class="RepeatRowColor'.$TempCol.'"><div align="center" class="FieldTitleColor'.$TempCol.'">Shipping</div></td>
<td width="16%" class="RepeatRowColor'.$TempCol.'"><div align="right" class="FieldTitleColor'.$TempCol.'">Price</div></td>
</tr>';
//Setup Cart Vars
$displaysubtotal=0;
$displayshippingtotal=0;
$displaytotal=0;
$displaynewshipping=0;
$displaynewprice=0;
$final = $_SESSION[$Session_Prefix.'Item_Count']+1;
$cnt=1;
for ($i = 1; $i < $final; $i++)
{
$RowCount=$RowCount+1;
$TempCol=$RowCount%2+1;
// Setup Vars for this Row
$displayshippinga = "shippinga".$i;
$displayshippingb = "shippingb".$i;
$displaytitle = "title".$i;
$displayprice = "price".$i;
$displayquantity = "quantity".$i;
$displayremove = "remove".$i;
// Calculate The Total Shipping For This Item
if ($_SESSION[$Session_Prefix.$displayquantity]>1)
{
$displaynewshipping = ($_SESSION[$Session_Prefix.$displayshippinga] + ($_SESSION[$Session_Prefix.$displayshippingb] * ($_SESSION[$Session_Prefix.$displayquantity] - 1)));
} else
{
$displaynewshipping = $_SESSION[$Session_Prefix.$displayshippinga];
}
// Calculate The price For This Item
$displaynewprice = $_SESSION[$Session_Prefix.$displayprice] * $_SESSION[$Session_Prefix.$displayquantity];
$Output = $Output.'<tr>
<td class="RepeatRowColor'.$TempCol.'"><div align="center" class="DataTextColor'.$TempCol.'">'.$_SESSION[$Session_Prefix.$displayquantity].'</div></td>
<td class="RepeatRowColor'.$TempCol.'"><div align="left" class="DataTextColor'.$TempCol.'">'.$_SESSION[$Session_Prefix.$displaytitle].'<br></div></td>
<td class="RepeatRowColor'.$TempCol.'"><div align="right" class="DataTextColor'.$TempCol.'">'.$Currency_Symbol.' '.number_format($displaynewshipping, 2, '.', '').'</div></td>
<td class="RepeatRowColor'.$TempCol.'"><div align="right" class="DataTextColor'.$TempCol.'">'.$Currency_Symbol.' '.number_format($displaynewprice, 2, '.', '').'</div></td>
</tr>';
// Update The Total Vars
$displaysubtotal = $displaysubtotal + $displaynewprice;
$displayshippingtotal = $displayshippingtotal + $displaynewshipping;
} // end For
$RowCount=$RowCount+1;
$TempCol=$RowCount%2+1;
// Calculate Totals
$displaytotal = ($displayshippingtotal + $displaysubtotal);
$Output = $Output.'<tr>
<td colspan="2" class="RepeatRowColor'.$TempCol.'"><div align="right" class="FieldTitleColor'.$TempCol.'">Sub Totals ('.$Currency_Code.'):</div></td>
<td class="RepeatRowColor'.$TempCol.'"><div align="right" class="DataTextColor'.$TempCol.'">'.$Currency_Symbol.' '.number_format($displayshippingtotal, 2, '.', '').'</div></td>
<td class="RepeatRowColor'.$TempCol.'"><div align="right" class="DataTextColor'.$TempCol.'">'.$Currency_Symbol.' '.number_format($displaysubtotal, 2, '.', '').'</div></td>
</tr>';
$RowCount=$RowCount+1;
$TempCol=$RowCount%2+1;
$Output = $Output.'<tr>
<td colspan="4" class="RepeatRowColor'.$TempCol.'"><div align="right" class="FieldTitleColor'.$TempCol.'">Total With Shipping ('.$Currency_Code.'): <span class="DataTextColor'.$TempCol.'">'.$Currency_Symbol.' '.number_format($displaytotal, 2, '.', '').'</span></div></td>
</tr>';
} else
{
$Output = $Output.'<tr>
<td colspan="4" class="RepeatRowColor'.$TempCol.'"><div align="center" class="DataTextColor'.$TempCol.'">...Cart is Empty... <br>
</div></td>
</tr>';
} // end if
$Output = $Output.'</table>';
return $Output;
}
// 8888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888
function GetCartTotals($Session_Prefix)
{
$Output = '';
if (isset($_SESSION[$Session_Prefix.'Item_Count']) && $_SESSION[$Session_Prefix.'Item_Count']!=0)
{
$CheckOrd=1;
//Setup Cart Vars
$displaysubtotal=0;
$displayshippingtotal=0;
$displaytotal=0;
$displaynewshipping=0;
$displaynewprice=0;
$final = $_SESSION[$Session_Prefix.'Item_Count']+1;
for ($i = 1; $i < $final; $i++)
{
// Setup Vars for this Row
$displayserialnum = "serialnum".$i;
$displayshippinga = "shippinga".$i;
$displayshippingb = "shippingb".$i;
$displaytitle = "title".$i;
$displayprice = "price".$i;
$displayquantity = "quantity".$i;
$displayremove = "remove".$i;
// Calculate The Total Shipping For This Item
if ($_SESSION[$Session_Prefix.$displayquantity]>1)
{
$displaynewshipping = ($_SESSION[$Session_Prefix.$displayshippinga] + ($_SESSION[$Session_Prefix.$displayshippingb] * ($_SESSION[$Session_Prefix.$displayquantity] - 1)));
} else
{
$displaynewshipping = $_SESSION[$Session_Prefix.$displayshippinga];
}
// Calculate The price For This Item
$displaynewprice = $_SESSION[$Session_Prefix.$displayprice] * $_SESSION[$Session_Prefix.$displayquantity];
// Update The Total Vars
$displaysubtotal = $displaysubtotal + $displaynewprice;
$displayshippingtotal = $displayshippingtotal + $displaynewshipping;
} // end For
// Calculate Totals
$displaytotal = ($displayshippingtotal + $displaysubtotal);
$Output['Shipping_Total']=$displayshippingtotal;
$Output['Sub_Total']=$displaysubtotal;
$Output['Order_Total']=$displaytotal;
}
return $Output;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function cleanvalue($strvalue) { // remove dangerous data from the value passed
$newvalue = strip_tags($strvalue);
$newvalue = str_replace("|", " ", $newvalue);
$newvalue = str_replace("'", " ", $newvalue);
$newvalue = stripslashes($newvalue);
return $newvalue;
}