Home  >  Article  >  Backend Development  >  How to implement php collection function

How to implement php collection function

藏色散人
藏色散人Original
2020-08-31 10:36:473724browse

How to implement the php collection function: first create the database table; then create the front-end code to implement the login interface; then implement the collection style through html; finally use php for background processing.

How to implement php collection function

# Recommended: "

PHP Video Tutorial

This is the database table


How to implement php collection functionNot much to say, let’s talk about the code
Front desk

<script>
    $(function(){
      $(&#39;#sc&#39;).click(function(){

          var gid=$(this).attr(&#39;data-id&#39;);          var data={
            gid:gid
          };
          $.ajax({
            url:"{:U(&#39;Goods/collect_add&#39;)}", 
            type:"post",
            data:data,
            success:function(data){
              // window.clearInterval(timer);
                            if(data==1){
                                window.location.href="{:U(&#39;Public/login&#39;)}"; //登陆界面
                            }else {                                if(data==2){
                                    $(&#39;#sc&#39;).css({                                        &#39;background-color&#39;:&#39;white&#39;,                                        &#39;color&#39;:&#39;#00ccff&#39;,
                                    });
                                    $(&#39;#sc_words&#39;).html(                                        &#39;收藏&#39;
                                    );
                                }else if(data==3){
                                    $(&#39;#sc&#39;).css({                                        &#39;background-color&#39;:&#39;#00ccff&#39;,                                        &#39;color&#39;:&#39;white&#39;,
                                    });
                                    $(&#39;#sc_words&#39;).html(                                        &#39;已收藏&#39;
                                    );
                                }else{
                                    alert(data);
                                }
                            }
            },
            error:function(){
              alert(&#39;请求失败&#39;);
            }
          });
      });
    })</script>

html part

      <p id="sc" data-id="{$detail.id}" class="in_right">
        <p id="sc_words">收藏</p>
      </p>

PHP background processing

    // 商品收藏   1 代表未登录 2代表取消收藏  3 代表 收藏成功 
    public function collect_add(){
      if(empty(session(&#39;uid&#39;))){        echo &#39;1&#39;;
      }else {        $collect=M(&#39;collect&#39;);        $gid=I(&#39;post.gid&#39;);        //先确定是否已收藏
        $map[&#39;gid&#39;]=$gid;        $map[&#39;uid&#39;]=session(&#39;uid&#39;);        $data=$collect->where($map)->find();        if($data){          if($data[&#39;status&#39;]==1){            $collect->where(&#39;id=&#39;.$data[&#39;id&#39;])->setField(&#39;status&#39;,0);            echo &#39;2&#39;;
          }else{            $collect->where(&#39;id=&#39;.$data[&#39;id&#39;])->setField(&#39;status&#39;,1);            echo &#39;3&#39;;
          }
        }else{          if($collect->create()){            $collect->gid=$gid;            $collect->create_time=get_date();            $collect->uid=session(&#39;uid&#39;);            $collect->status=1;            $collect->add();            echo &#39;3&#39;;
          }else{            echo &#39;服务器出错,请重试!&#39;;
          }
        }
      }
    }

The above is the detailed content of How to implement php collection function. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn