php - Like-Unlike function in codeigniter -


i implement , unlike codeigniter. can in normal php using following codes don't why not working in codeigniter below database table , model view , controller. appritiated. thanks.

posts table

create table `posts` (   `id` int(11) not null primary key auto_increment,   `title` varchar(100) not null,   `content` text not null,   `timestamp` timestamp not null default current_timestamp on update current_timestamp ) engine=innodb default charset=latin1; 

like-unlike table

create table `like-unlike` (   `id` int(11) not null primary key auto_increment,   `user_id` int(11) not null,   `post_id` int(11) not null,   `purpose` int(2) not null,   `timestamp` timestamp not null default current_timestamp on update current_timestamp ) engine=innodb default charset=latin1; 

jquery file

function likeitem(post_id) {     if ($("#likeitem_" + post_id).text() == "like")     {         $("#likeitem_" + postid).html('unlike');         var purpose = "like";     } else     {         $("#likeitem_" + post_id).html('like');         var purpose = "unlike";     }     $.ajax({         type: "post",         url: "<?php echo base_url('posts/likes');?>",         data: "postid=" + postid + "&purpose=" + purpose,         success: function (data)         {             // seomthing here data             // console.log(data) --> see data return or not         }     }     ); 

this model "post_model.php"

public function itemlike() {     $id = $this->session->userdata('user_id');     $postid = $this->input->post('post_id');     $purpose = $this->input->post('purpose');     if ($purpose == "like") {         // echo 'test';         // exit();         $data = array(             "user_id" => $id,             "post_id" => $postid,         );         $this->db->insert('like', $data);     } else {         echo 'failed';     } } 

this view file

<li><a class="like-unlike" href="#" id="likeitem_{$item['post_id']}" onclick="likeitem({$item['post_id']})">like</a></li> 

this controller "post.php"

public function likeitem() {     $this->usermodel->itemlike(); } 

in database schema, have mentioned table like_unlike , saving data in table like-unlike. also, set column purpose mandatory , not passing values here:

$data = array( "user_id" => $id, "post_id" => $postid, ); 

you don't need pass data if use default value. need keep in mind column purpose holds integer value per database schema. apart need modify code mention @jyothi


Comments

Popular posts from this blog

networking - Vagrant-provisioned VirtualBox VM is not reachable from Ubuntu host -

c# - ASP.NET Core - There is already an object named 'AspNetRoles' in the database -

ruby on rails - ArgumentError: Missing host to link to! Please provide the :host parameter, set default_url_options[:host], or set :only_path to true -