PHP Cookie Shopping cart usage ideas

Source: Internet
Author: User
Tags unique id

Simple idea, really shopping cart far more complex than this, suitable for beginners, easy to understand
$result _arr = Here is the query result, or data, or string;

Analog data: Array ([0] = = Array ([id] = = 40920435 [title] = 42432 [Addtime] = 2018-02-08 09:22:37 [content] = 432432 [PHOTO] = 2018-02-08/5a7ba65d89746.jpg))

$result = Serialize ($result _arr); Array must be serialized

After serialization: A:1:{i:0;a:5:{s:2: "id"; s:8: "40920435"; s:5: "title"; s:5: "42432"; s:7: "Addtime"; s:19: "2018-02-08 09:22:37"; s:7 : "Content"; s:6: "432432"; s:5: "Photo"; s:28: "2018-02-08/5a7ba65d89746.jpg";}}

$id = Mark, can be the data unique ID, can also be a name and so on, convenient for later use, here I use the Product ID;
Setcookie ("buycart[". $id. "]", $result, Time () +20),//20 is seconds, customizable;

The above is the name and value of the set shopping cart cookie;

The following is the acquisition of data in the shopping cart;

$cookies _arr = $_cookie[' Buycart ');
foreach ($cookies _arr as $name = + $v) {

Print_r ($name);//buycart[". $id."], the output is the value of $id;
Print_r (Unserialize ($v));//deserialization, output is an array
}

PHP Cookie Shopping cart usage ideas

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.