lawang code

lawang Code


Tanyakan kode disini

Codeigniter - bikin unlike sistem di CI

ditanyakan oleh andhikark 4 tahun yang lalu

Gan ane lagi stuck nih.. Ane mau bikin like di web ane pake CI. Ane punya 2 table namanya post dan vote, table post punya id_post, id_user, isi_berita, dan like. Table vote punya id_vote, id_post, id_user, sama like juga yang isinya cuma 1 sama 0.

Kalo user like maka di table post bakal LIKE+1, dan di table vote kolom LIKE diset ke 1. Untuk like udah sukses gan. Masalah ane cuma di unlikenya aja.. jadi ketika user yang udah like mau unlike cukup klik button like lagi dan table post jadi LIKE-1 dan kolom vote LIKE di set ke 0.

ini view ane gan:

<button id="1_upvote"><span id="1_upvote_result" >12</span>LIKE</button>

ini javascriptnya gan di view juga:

<script type="text/javascript">
$(document).ready(function(){
      $(".voteme").click(function() {
        var id_post = this.id;
        var upOrDown = id_post.split('_'); 
        $.ajax({
            type: "post",
            url: "<?php echo base_url()?>home/voteme",
            cache: false,       
            data:'id_post='+upOrDown[0] + '&upOrDown=' +upOrDown[1],
            success: function(response){                
                try{
                    if(response=='true'){   
                        var newValue = parseInt($("#"+id_post+'_result').text()) + 1;            
                        $("#"+id_post+'_result').html(newValue);
                    }
                }catch(e) {
                    alert('Exception while request..');
                }
            },
            error: function(){                      
                alert('Error while request..');
            }
         });
    });
});
</script>

Ini controllernya gan:

    function voteme()
{
        $id_post=  $this->input->post('id_post');
        $upOrDown=  $this->input->post('upOrDown');
        $status ="false";
        $updateRecords = 0;     
        if($upOrDown=='upvote'){
            $updateRecords = $this->home_model->updateUpVote($id_post);
        }
        if($updateRecords>0){
            $status = "true";
        }
        echo $status;
        if(! $this->input->is_ajax_request()){ // if not ajax request
        $data['id_post'] = $id_post;
        redirect ('home',$data);// redirect it
        }
        exit;
}

Ini modelnya gan:

 function updateUpVote($id_post){
        //cek user pernah like atau belum
        $id_user = $this->session->userdata('id');
        $hasil="SELECT id_vote FROM vote WHERE id_post=$id_post AND id_user=$id_user";
        $result=$this->db->query($hasil);
        $row=$result->row();        
        //kalo user udah pernah like
        if (!empty($row)){
            $id_user = $this->session->userdata('id');
            $hasil="SELECT LIKE FROM vote WHERE id_post=$id_post AND id_user=$id_user";
            $result=$this->db->query($hasil);
            $row=$result->row('LIKE');
            /Kalo LIKE = 0 then change into 1 AND update LIKE+1
            if ($row('LIKE')==0){
                //update vote
                $sql_post = "UPDATE vote set LIKE = 1 WHERE id_post =? AND id_user=?";
                $this->db->query($sql_post, array($id_post,$id_user));
                //then update post
                $sql_post = "UPDATE post set LIKE = LIKE+1 WHERE id_post =?";
                $this->db->query($sql_post, array($id_post));
                return $this->db->affected_rows();
            }
            //Kalo LIKE = 1 then change into 0 AND update LIKE-1
            elseif ($row('LIKE')==1){
                //update vote
                $sql_post = "UPDATE vote set LIKE = 0 WHERE id_post =? AND id_user=?";
                $this->db->query($sql_post, array($id_post,$id_user));
                //then update post
                $sql_post = "UPDATE post set LIKE = LIKE-1 WHERE id_post =?";
                $this->db->query($sql_post, array($id_post));
                return $this->db->affected_rows();
            }   
        }
        //Kalo user belum pernah like then create vote AND update LIKE+1
        elseif(empty($row)) {
            //create add vote dan diset LIKE jadi 1
            $this->db->query('INSERT INTO vote VALUES(id_vote,' . $id_post . ','. $id_user . ',1)');
            //then update postnya
            $sql_post = "UPDATE post set LIKE = LIKE+1 WHERE id_post =?";
            $this->db->query($sql_post, array($id_post));
            return $this->db->affected_rows();
        }   
    }

Ane bener bener stuck gan... cuma tinggal unlikenya aja tapi kok susah banget ya ane rasa sih salahnya ada di ajax/javascriptnya

Please help...

Menampilkan 1-1 dari 1 hasil
handita okviyanto menjawab 4 tahun yang lalu :

Coba dicek di firebugnya ada yang error gak gan?


Anda harus login terlebih dahulu untuk memberikan komentar

Recent Tag

X 6
X 48
X 14
X 10
X 1
X 5
X 4
X 18
X 10

Bantuan

Help

Feedback

Help