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

How to implement collection function in php

藏色散人
藏色散人Original
2020-09-28 10:12:542854browse

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

html part

      

收藏

php background processing

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

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!

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