Hash data type simple operation (implement shopping cart case)

Source: Internet
Author: User
Tags php redis install redis
It's not a liability. How to Http://www.php.cn/php/php-tp-inst install Redis and PHP Redis extensions, primarily familiar with calling Redis hash data types



The simple way to do this

1:hset



2:hget




4:hdel




5:hgetall




4:hexists




5:hincrby




Simple Shopping Cart implementation

















Namespacehome\controller;

Usethink\controller;

Useorg\net\http;

Usethink\cache\driver\redis;

Classindexcontrollerextendscontroller

{

Private$redis_obj=null;

Public Functionconstruct ()

{

$this->redis_obj=newredis ();

}

/**

* @file: Determine whether the shopping cart exists this reduced product

* @param $user _id

* @param $goods _id

*/

Public Functiongoods_is_exist ($user _id, $goods _id)

{

$r 1= $this->redis_obj->hexists ($user _id, $goods _id);

RETURN$R1;

}

/**

* @file: Add Shopping cart Items

*

*/

Public Functionadd_goods ()

{

$user _id= intval ($_post[' user_id ')? Intval ($_post[' user_id '): 0;

$goods _id= intval ($_post[' goods_id ')? Intval ($_post[' goods_id '): 0;

$exist = $this->goods_is_exist ($user _id, $goods _id);

if (!empty ($user _id) && $goods _id) {

There is no increase in the product

if (! $exist) {

$add _return= $this->redis_obj->hset ($user _id, $goods _id,1);

if ($add _return) {

$this->ajaxreturn (Array (' Code ' =>0, ' msg ' = ' success '));

}else{

$this->ajaxreturn (Array (' Code ' =>1, ' msg ' = ' error '));

}

}elseif ($exist) {

1 Increase in existing goods

$add _exist_result= $this->redis_obj->hincrby ($user _id, $goods _id,1);

if ($add _exist_result) {

$this->ajaxreturn (' Code ' =>0, ' msg ' = ' success ', ' 1 ' = = $add _exist_result));

}else{

$this->ajaxreturn (Array (' Code ' =>1, ' msg ' = ' error '));

}

}

}

}

Reduce the shopping cart items

Public Functionreduce_goods ()

{

$user _id= intval ($_post[' user_id ')? Intval ($_post[' user_id '): 0;

$goods _id= intval ($_post[' goods_id ')? Intval ($_post[' goods_id '): 0;

if (!empty ($user _id) && $goods _id) {

$exist = $this->goods_is_exist ($user _id, $goods _id);

does not exist

if (! $exist) {

$this->ajaxreturn (Array (' Code ' =>1, ' msg ' = ' goods ' is not exist '));

}elseif ($exist) {

$val = $this->redis_obj->hget ($user _id, $goods _id);

if ($val ==1) {//Cart item only one item reduced to 0 is deleted

$del _result= $this->redis_obj->hdel ($user _id, $goods _id);

if ($del _result==1) {

$this->ajaxreturn (Array (' Code ' =>0, ' msg ' = ' success ', ' num ' =>0));

}

}elseif ($val >1) {

$new _value= $this->redis_obj->hincrby ($user _id, $goods _id,-1);

if ($new _value>0) {

$this->ajaxreturn (Array (' Code ' =>0, ' msg ' = ' success ', ' num ' = $new _value));

}else{

$this->ajaxreturn (Array (' Code ' =>1, ' msg ' = ' error '));

}

}

}

}else{

$this->ajaxreturn (Array (' Code ' =>1, ' msg ' = ' param is empty '));

}

}

Remove a product

Public Functionrm_goods ()

{

$user _id= intval ($_post[' user_id ')? Intval ($_post[' user_id '): 0;

$goods _id= intval ($_post[' goods_id ')? Intval ($_post[' goods_id '): 0;

if (!empty ($user _id) &&!empty ($goods _id)) {

$arr = Explode (', ', $goods _id);

Array_unshift ($arr, $user _id);

$RM _result= Call_user_func_array (Array ($this->redis_obj, "Hdel"), $arr);

if ($rm _result>=0) {

$this->ajaxreturn (Array (' Code ' =>0, ' msg ' = ' remove success '));

}

}else{

$this->ajaxreturn (Array (' Code ' =>1, ' msg ' = ' param is empty '));

}

}

Shopping Cart List

Public Functioncart_list ()

{

$user _id= intval ($_post[' user_id ')? Intval ($_post[' user_id '): 0;

if (!empty ($user _id)) {

$goods _list= $this->redis_obj->hgetall ($user _id);

$this->ajaxreturn (Array (' Code ' =>0, ' list ' = + $goods _list));

}else{

$this->ajaxreturn (Array (' Code ' =>1, ' msg ' = ' param is empty '));

}

}

Set the quantity of an item

Public Functionedit_goods_num ()

{

}

}

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.