PHP and cookies Implement shopping cart

Source: Internet
Author: User
Tags setcookie
Not accustomed to using cookies, before making a shopping cart is to allow users to log in before buying, so I can store the goods in the cart into a data table, wait until the order to transfer them to the table of another order. But the customer's demand is unlimited, want to also can not register can buy, can only use cookies, because has been used, do not know much, and has been not too fond of this thing (probably because it will not use it, hey.) , the bite of the bullet to find information to do it, the online search a lot of shopping cart, find a paragraph, excerpt down:


Using PHP arrays and cookies to implement the shopping cart function, the method is: the cookie into the array, the array to add, delete, modify operations, array of each group of records are a commodity information (number, price, etc.)

The idea of solving a shopping cart is to record a two-dimensional array with cookies. One dimension represents every commodity, two dimensions contain the ID of the commodity, the quantity of the goods ... And so can increase their own, anyway are two-dimensional, they are willing to take the number of commodity attributes added to.

The operation of the goods in the shopping cart generally has the following: Add goods, modify the number of goods, delete goods, empty the shopping cart.
All of these actions are for cookies. Each time you take the array out of the cookie, add, modify, and delete it, and then record it in the cookie. As for emptying the cart, it is much simpler to simply set a cookie with the same name to be empty.
The specific code is as follows:
Add Shopping Cart
function Addcart ($goods _id, $goods _num) {
$cur _cart_array = unserialize (stripslashes ($_cookie[' shop_cart_info '));
if ($cur _cart_array== "") {
$cart _info[0][] = $goods _id;
$cart _info[0][] = $goods _num;
Setcookie ("Shop_cart_info", Serialize ($cart _info));
}elseif ($cur _cart_array<> "") {
Returns the maximum number of array key names in reverse
$ar _keys = Array_keys ($cur _cart_array);
Rsort ($ar _keys);
$max _array_keyid = $ar _keys[0]+1;
Traverse the current shopping cart array
Traverse the 0 value of each product information array, and if the key value is 0 and the item number is the same, the cart has the same item.
foreach ($cur _cart_array as $goods _current_cart) {
foreach ($goods _current_cart as $key => $goods _current_id) {
if ($key = = 0 and $goods _current_id = = $goods _id) {
echo "<script language=javascript>";
echo "If" (Confirm (' Shopping cart already has this product, continue to shop by----OK!) Check cart----Cancel! ')
echo "{History.go (-1)}";
echo "else{window.location.href= ' xqs_cart.php '}";
echo "</script>";
Exit ();
}
}
}
$cur _cart_array[$max _array_keyid][] = $goods _id;
$cur _cart_array[$max _array_keyid][] = $goods _num;
Setcookie ("Shop_cart_info", Serialize ($cur _cart_array));
}
}
Remove from Shopping cart
function Delcart ($goods _array_id) {
$cur _goods_array = unserialize (stripslashes ($_cookie[' shop_cart_info '));
Deletes the position of the item in the array
unset ($cur _goods_array[$goods _array_id]);
Setcookie ("Shop_cart_info", Serialize ($cur _goods_array));
}
Modify the number of shopping cart items
function Update_cart ($up _id, $up _num, $goods _ids) {
Empty cookies first to reset, pass over three array parameter 1 array ID 2 Commodity quantity array 3 commodity number array
If you do not empty cookies, you cannot process goods with a quantity of zero
Setcookie ("Shop_cart_info", "");
foreach ($up _id as $song) {
Returns the current cell of the array, and then moves the pointer down one position
$goods _nums = current ($up _num);
$goods _id = current ($goods _ids);
Next ($up _num);
Next ($goods _ids);
When the number of items is empty, unregister the array value here and use the Continue 2 statement to avoid the following operation and continue with the Foreach Loop
while ($goods _nums = = 0) {
Unset ($song);
Continue 2;
}
$cur _goods_array[$song][0] = $goods _id;
$cur _goods_array[$song][1] = $goods _nums;
}
Setcookie ("Shop_cart_info", Serialize ($cur _goods_array));
}
After the formation of the order, it needs to be based on their own needs, whether there are points, whether there are cash rewards, such as warehousing.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.