How to implement collection function in php

藏色散人
Release: 2023-03-06 07:04:01
Original
3016 people have browsed it

How to implement the collection function in PHP: first create the front-end code to implement the login interface; then implement the collection function through the if statement; and finally implement the collection processing function in the PHP background.

How to implement collection function in php

Recommended: "PHP Video Tutorial"

php realizes the collection function

This is the database table

How to implement collection function in php

Without further ado, 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>
Copy after login

html part

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

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;;
          }
        }
      }
    }
Copy after login

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

Related labels:
php
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template