標籤:iwebshop收藏功能思路
今天做了一個店鋪收藏功能!把喜歡的店鋪可以收藏起來!
前端主要是在js上做了文章
這是頁面a標籤部分:
<a href="javascript:;" onclick="add_shop_collect({echo:$item[‘id‘]});" class="stores_shou">收藏店鋪</a>
這是頁面js部分:
<script>//傳遞後台參數 function add_shop_collect(shop_id){ $.ajax( { url:‘後台方法路徑‘, type:‘POST‘, dataType:‘text‘, data:{"shop_id":shop_id}, success:function(message){ if(message == 0){ }else if(message == 1){ alert("收藏成功"); }else if (message == 2){ alert("收藏失敗"); }else if (message ==3){ alert("您已經收藏了"); } } } )}</script>
後台控制器部分:
function add_collect(){ //擷取目前使用者id $user_id = $this->user[‘user_id‘]; //擷取前端參數 $shop_id = IFilter::act(IReq::get(‘shop_id‘), ‘int‘); $shop_id_str = ‘‘; if($user_id){ //執行查詢 結合架構邏輯來書寫次部分 $memberDB = new IModel(‘‘); $memberData = $memberDB->getObj(‘user_id=‘.$user_id,‘collection_shop_id‘); //結合架構邏輯來書寫次部分 $shop_id_str = empty($memberData[‘collection_shop_id‘])?"":$memberData[‘collection_shop_id‘]; if(!in_array($shop_id,explode(",",$shop_id_str))){ $shop_id_str = trim(($shop_id_str.‘,‘.$shop_id),","); $memberDB->setData(array( ‘collection_shop_id‘ => "$shop_id_str", )); }else{ echo 3; exit(); } if($memberDB->update(‘user_id = ‘.$user_id)){ echo 1; }else{ echo 2; } }else{ echo 0; } exit();}
本文出自 “kangjunfei” 部落格,謝絕轉載!
iwebshop關於收藏店鋪