今天做了一个店铺收藏功能!把喜欢的店铺可以收藏起来!
前端主要是在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” 博客,谢绝转载!
原文地址:http://kangjunfei.blog.51cto.com/11556647/1928530