SoFunction
Updated on 2025-04-04

php sample code to implement collection function

Organize the document, search out a sample code for PHP to implement the collection function, and organize it a little and streamline it to share it.

HTML:

<a class = "x"  u status = "{$collect_pic}" href = "javascript:void(0);">
<if condition = "$collect_num gt 0">
  <span>Cancel Favorites</span>
<else/><span>collect</span></if></a>

Js:

//Click to favorites to realize the collection  $('.x').on('click',function(){
    var Oa=$(this);
    var id=('id');//Get the image id attribute    var uid = ('uid');//Get user id    var status = ('status'); //Get favorite status 1 collection, 0 Cancel favorites    $.post('url',{id:id,uid:uid,status:status},function(msg){
      if(==1){
        ("span").text('Cancel Favorites');
        ('status','1');
      }else{
        ("span").text('collect');
        ('status','0');
      }
    },'json')
  })

PHP:

//Discern whether the user has collected the image      $data['uid'] = $_SESSION['uid'];
      $data['pid'] = $pid;
      $collect_pic = M('collect')->where($data)->getfield('status');
      $this->collect_num = $collect_num;
      $this->collect_pic = $collect_pic;

//Selected successfully    public function cang(){

      $data['pid'] = isset($_POST['id'])?intval(trim($_POST['id'])):0;
      $data['uid'] = $_SESSION['uid'];
      //Default o collection 1 Cancel favorite      $status = isset($_POST['status'])?intval(trim($_POST['status'])):0;
      $db = M('collect');
      if($status == 0){
        //Data assembly, ignore        if(M('collect')->data($collect)->add()){
          $msg['info'] = "ok";
          $msg['status'] = 1;
          $this->ajaxreturn($msg);
          exit;        
        }else{
          $msg['info'] = 'fail';
          $msg['status'] = 1;
          $this->ajaxreturn($msg);
        }
      }else{
        //Cancel Favorites        if($db->where($data)->delete()){
          $msg['info'] = "ok";
          $msg['status'] = 0;
          $this->ajaxreturn($msg);
          exit;
        }else{
          $msg['info'] = 'fail';
          $msg['status'] = 0;
          $this->ajaxreturn($msg);
        }
      }

Idea: It’s basically not difficult, just pay attention to the details.

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.