해시 데이터 형식의 간단한 조작(장바구니 케이스 구현)

PHPz
풀어 주다: 2023-03-07 10:02:01
원래의
1838명이 탐색했습니다.

여기서 번거롭게 하지 않는 것이 어떻습니까설치redis 및 php redis 확장 프로그램 , 주로 그림

데이터 유형

을 호출하는 간단한 방법에 익숙합니다. >

동작은 다음과 같습니다

1:h

Set

해시 데이터 형식의 간단한 조작(장바구니 케이스 구현)


2: hGet


해시 데이터 형식의 간단한 조작(장바구니 케이스 구현)


4:hDel


해시 데이터 형식의 간단한 조작(장바구니 케이스 구현)


5:hGetAll


해시 데이터 형식의 간단한 조작(장바구니 케이스 구현)


4:h있음


해시 데이터 형식의 간단한 조작(장바구니 케이스 구현)


5:hIncrBy


해시 데이터 형식의 간단한 조작(장바구니 케이스 구현)


간편

장바구니 구현


해시 데이터 형식의 간단한 조작(장바구니 케이스 구현)



해시 데이터 형식의 간단한 조작(장바구니 케이스 구현)



해시 데이터 형식의 간단한 조작(장바구니 케이스 구현)



해시 데이터 형식의 간단한 조작(장바구니 케이스 구현)



해시 데이터 형식의 간단한 조작(장바구니 케이스 구현)





namespaceHomeController;

useThinkController;

useOrgNetHttp;

useThink

캐시DriverRedis;

classIndexControllerextendController

private$redis_obj=

null

;

공용 함수 구성()

{

$this->redis_obj=

new

Redis();

}

/**

*@

파일

: 장바구니에 할인된 제품이 있는지 확인

*@param$user_id

*@param$goods_id

*/

public functiongoods_is_exist($user_id,$goods_id)

{

$r1 =$this->redis_obj->h존재함($user_id,$goods_id);

반환

$r1;

}

/* *

*@file: 장바구니 항목 추가

*

*/

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) {

//추가된 상품이 없습니다.

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

$add_exist_result=$this->redis_obj->hIncrBy( $user_id,$goods_id,1);

if($add_exist_result) {

$this->ajaxReturn(array('code'=>0,'msg'=> '성공','1'=>$add_exist_result));

}else{

$this->ajaxReturn(array('code'=>1,'msg' =>'오류'));

}

}

}

}

//减少购물车적商품

공용 함수reduce_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);

//불가

if(!$exist) {

$this->ajaxReturn(array('code'=>1 ,'msg'=>'상품이 존재하지 않습니다'));

}elseif($exist) {

$val=$this->redis_obj->hGet($ user_id,$goods_id);

if($val==1) {//购物车商品只有一件的时候 减少到0就是删除

$del_result =$this->redis_obj->hDel($user_id,$goods_id);

if($del_result==1) {

$this->ajaxReturn(array(' code'=>0,'msg'=>'성공','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'=>'성공','num'=>$new_value));

}else{

$this->ajaxReturn(array('code'=>1,'msg'=>'error'));

}

}

}

}else{

$this->ajaxReturn(array('code'=>1,'msg'=>'param이 비어 있습니다.'));

}

}

//移除商품

public functionrm_goods()

{

$user_id= intval ($_POST['사용자_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= 폭발(',',$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'=>'제거 성공'));

}

}else{

$this->ajaxReturn(array('code'=>1 ,'msg'=>'매개변수가 비어 있습니다'));

}

}

//购物车列表

공개 함수카트_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이 비어 있습니다'));

}

}

//设置一个商제품 수량

공용 함수edit_goods_num()

{

}

}

위 내용은 해시 데이터 형식의 간단한 조작(장바구니 케이스 구현)의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

관련 라벨:
원천:php.cn
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
최신 이슈
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!