Guys please help!
Hi,
Basically i need some help with my cookies.
The problems are pretty basic:
I am writing a shopping cart application where the user clicks on add item and surprise surprise, the data is set in a cookie and displayed in a table resembling a shopping cart.
so the following happens:
the page containing the product information
......
......
echo ("<tr><td><a href='setbasket.php?select_item_no=$myrow[0]'><img src='addtobasket.gif' border=0></a><td><tr>");
......
......
so when the user clicks on it, it goes to the script setbasket.php with the variable $select_item_no assigned a value.
setbasket.php
if (!$total_items) {
$total_items=1;
setcookie("item_number[$total_items]", $select_item_no);
setcookie("quantity[$total_items]", "1");
setcookie("total_items", $total_items);
} else {
$i=0;
while ($i < $total_items) {
if ($select_item_no==$item_number[$i]) {
$cookie_trigger=1;
} else {
$cookie_trigger=0;
}
$i=$i+1;
}
}
if ($cookie_trigger==1){
echo ("duplicate");
// echo("<META HTTP-EQUIV=\"Refresh\" CONTENT=\"0; URL=basket.php\">");
}
if ($cookie_trigger==0){
$total_items++;
setcookie("item_number[$total_items]", $select_item_no);
setcookie("quantity[$total_items]", "1");
setcookie("total_items", $total_items);
echo ("no duplicate");
// echo("<META HTTP-EQUIV=\"Refresh\" CONTENT=\"0; URL=basket.php\">");
}
So i hear you ask, whats the problem?
First Problem
Basically i have noticed that if the cookie is empty ( the first if statement) when i assign a value and display to all cookies there is no data in any of them apart from the variable total_items. So basically the user will need to submit the item they wish to add to the basket twice if the basket is empty on the first submit.
Second Problem
Basically i dont want the cookie to have duplicated information stored in it, so i wrote the while statement above that checks the information in the cookie against the select_item_no that was submited by the user.
This works when the user clicks on a product A repeatedly, but when user clicks on product B with a different select_item_no, and then goes back to product A and click on that item, the information is duplicated.
can anyone help, this is driving me mad.
I applogies in advance in poor programming code and technique, still new to it.
I hope i am making sense since i have no idea whats going on.
Many Thanks
Sunit
😕 😕 🙁