Hi
I am working on a shopping cart system and am having trouble with
a function that moves an item from an order list to a wish list
There are actually two functions, one to move to the wish list
and one to move to the order list.
the lists are stored in a record keyed to the user's id
The functions appear to work correctly but when I go back and forth,
after a few times, all of a sudden, I lose one of the lists and
get an empty record in the cart.
Here is the function add to wish list. The move to cart is the same
except the names of the lists are changed.
Does anyone see anything here that might be causing the problem?
Thanks
David
function add_to_wishlist () {
$cust_id = $_COOKIE['cust_id'] ;
$new_wish_list = "" ;
$new_order_list = "" ;
$order_items = "" ;
// get orderlist and wishlist
db_connect () ;
$cart_query = "select * from temp_sales where trim(customer_id) =
'$cust_id'; " ;
$cart_result = mysql_query($cart_query) or die(mysql_error()) ;
$cart_row = mysql_fetch_array ($cart_result, MYSQL_ASSOC) ;
$num_carts = mysql_num_rows ($cart_result) ;
if ($num_carts == 0) {
echo "<br>Cart Not Found<br>";
}
mysql_close () ;
// we're adding to wish list so there has to be something in the
orderlist to start with
// don't know if there is anything already in the wish list so check
if it's empty or not and set the var accordingly
if (!empty($cart_row['wish_list']) ) {
$new_wish_list = $cart_row['wish_list'] ;
}
$order_items = explode ("|" , $cart_row['order_list']) ;
if (count (order_items) == 1 ) {
$new_wish_list = $cart_row['order_list'] ;
echo "<br>1 item in order list<br>";
} else {
foreach ($order_items as $item) {
echo "<br>item: $item<br>" ;
$details = explode ("^" , $item) ;
list
($item_number,$item_name,$quantity,$discount,$price,$cost,$weight ) =
$details ;
$item_list =
"$item_number^$item_name^$quantity^$discount^$price^$cost^$weight" ;
if (trim($_REQUEST['item_number']) == trim($item_number) &&
trim($_REQUEST['item_name']) == trim($item_name) ) {
// this is the item we're moving to the wish
list so add it to the wish list
if (empty($new_wish_list)) {
$new_wish_list = $item_list ;
} else {
$new_wish_list = $new_wish_list. "|" .$item_list ;
}
} else { // item remains in orderlist so add it to the
orderlist
if (empty ($new_order_list)) {
$new_order_list = $item_list ;
} else {
$new_order_list = $new_order_list. "|" .$item_list ;
}
} // end of if adding to wishlist
} // END Foreach order item
} // end if order list = 1
// save lists and reload cart
db_connect () ;
$update_cart = "UPDATE temp_sales SET order_list =
'$new_order_list', wish_list = '$new_wish_list' where customer_id =
'$cust_id' ; " ;
$update_cart_result = mysql_query ($update_cart) or
die(mysql_error()) ;
mysql_close () ;
unset ($new_wish_list) ;
unset ($new_order_list) ;
unset ($item_list) ;
unset ($order_items) ;
shopping_cart () ;
} // end of move to wish list function
function move_to_cart () {
$cust_id = $_COOKIE['cust_id'] ;
$new_wish_list = "" ;
$new_order_list = "" ;
$wish_items = "" ;
// get orderlist and wishlist
db_connect () ;
$cart_query = "select * from temp_sales where trim(customer_id) =
'$cust_id'; " ;
$cart_result = mysql_query($cart_query) or die(mysql_error()) ;
$cart_row = mysql_fetch_array ($cart_result, MYSQL_ASSOC) or
die(mysql_error()) ;
$num_carts = mysql_num_rows ($cart_result) ;
if ($num_carts == 0) {
echo "<br>Cart Not Found<br>";
}
mysql_close () ;
// we're adding to order list so there has to be something in the
wishlist to start with
// don't know if there is anything already in the order list so check
if it's empty or not and set the var accordingly
if (!empty($cart_row['order_list']) ) {
$new_order_list = $cart_row['order_list'] ;
}
$wish_items = explode ("|" , $cart_row['wish_list']) ;
if (count (wish_items) == 1 ) {
$new_order_list = $cart_row['wish_list'] ;
echo "<br>1 item in wish list<br>";
} else {
foreach ($wish_items as $item) {
echo "<br>item: $item<br>" ;
$details = explode ("^" , $item) ;
list
($item_number,$item_name,$quantity,$discount,$price,$cost,$weight ) =
$details ;
$item_list =
"$item_number^$item_name^$quantity^$discount^$price^$cost^$weight" ;
if (trim($_REQUEST['item_number']) == trim($item_number) &&
trim($_REQUEST['item_name']) == trim($item_name) ) {
// this is the item we're moving to the wish
list so add it to the order list
if (empty ($new_order_list)) {
$new_order_list = $item_list ;
} else {
$new_order_list = $new_order_list. "|" .$item_list ;
}
} else { // item remains in wishlist so add it to the
wishlist
if (empty ($new_wish_list)) {
$new_wish_list = $item_list ;
} else {
$new_wish_list = $new_wish_list. "|" .$item_list ;
}
} // end of if adding to wishlist
} // END Foreach order item
} // end if 1 item in list
// save lists and reload cart
db_connect () ;
$update_cart = "UPDATE temp_sales SET order_list =
'$new_order_list', wish_list = '$new_wish_list' WHERE customer_id =
'$cust_id' ; " ;
$update_cart_result = mysql_query ($update_cart) or
die(mysql_error()) ;
mysql_close () ;
unset ($new_wish_list) ;
unset ($new_order_list) ;
unset ($item_list) ;
unset ($order_items) ;
shopping_cart () ;
} // end of move to CART function