PHP Tutorial: Combining cookies and arrays to implement a shopping cart

Source: Internet
Author: User
Tags setcookie
found that many of PHP's functions result in the array given, the most commonly used is mysql_fetch_array (),
Includes the obtained string information to generate an array, and then decomposed by the array into a string using the function explode (); Implode ();

I've never written a shopping cart or something like that, consciously belonging to the type of stupidity, writing off and on for one weeks, pondering the handling of a shopping cart (let's be a newbie).
And because it has not entered the class door, so it is a process-oriented bar.

The essence of my method is: Put the cookie into the array, add, delete, modify the array, each set of records in the array is a product information (number, price, etc.)



The idea of solving a shopping cart is to use a cookie to record a two-dimensional array. One-dimensional represents each commodity, two-dimensional contains the product ID, the number of goods ... And so can increase their own, anyway, are two-dimensional, they are willing to take the number of product attributes added to.

The operation of the goods in the shopping cart generally has the following: Add items, modify the number of items, delete items, empty the shopping cart.
These actions are for cookies. Each time the array in the cookie is taken out, added, modified, deleted, and then recorded in the cookie. As for emptying the cart, it is easier to set up a cookie with the same name directly.

Here's what I wrote about the specific operation function

Add to 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 array key name in reverse-fetching maximum
$ar _keys = Array_keys ($cur _cart_array);
Rsort ($ar _keys);
$max _array_keyid = $ar _keys[0]+1;

Iterate through the current shopping cart array
Traverse the 0 value of each product information array, if the key value is 0 and the item number is the same item in the Cart
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 item, continue shopping by----OK!) Check cart press----Cancel! ') ";
echo "{History.go (-1)}";
echo "else{window.location.href=" http://www.php1.cn/"> 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 '));

Remove 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 shopping Cart item quantity
function Update_cart ($up _id, $up _num, $goods _ids) {

First empty the cookie so that it is reset, pass over three array parameter 1 array identification 2 number of items array 3 Product number array
If you do not empty the cookie, you cannot process a zero quantity item
Setcookie ("Shop_cart_info", "" ");
foreach ($up _id as $song) {

The current cell of the array is returned first, and the pointer moves 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 values 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 of things, it is necessary according to their own needs, whether there are points, whether there are cash prizes such as warehousing.

The above is the combination of cookies and arrays to achieve the contents of the shopping cart, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!

  • 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.