Like and unlike concept in Codeigniter onclick ajax call function, we have already discussed in php ajax like and unlike concept. like Voting concept, Now we are going to do that codeigniter. here i have implemented with Ip address, one ip address can like one time, if they are liking it again it will be unlike. Let see the code.
DATABASE DETAILS
Database name is : 2my4edge
i have given in download link is `test`
There are two tables, one is story table and another one is storylikes table.
1. story
2. storylikes
story table
CREATE TABLE `story` ( `id` int(11) NOT NULL, `title` varchar(250) NOT NULL, `link` varchar(255) NOT NULL, `description` text NOT NULL, `likes` int(11) NOT NULL DEFAULT '0', `date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
storylikes table
CREATE TABLE `storylikes` ( `id` int(11) NOT NULL, `storyid` int(11) NOT NULL, `ipaddress` varchar(200) NOT NULL, `date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
I hope, you know to configure codeigniter files, as we have discussed in our previous tutorials. So now lets directly go to Controllers.
CONTROLLER
Welcome.php
<?php defined('BASEPATH') OR exit('No direct script access allowed'); class Welcome extends CI_Controller { /** * Index Page for this controller. * * Maps to the following URL * http://example.com/index.php/welcome * - or - * http://example.com/index.php/welcome/index * - or - * Since this controller is set as the default controller in * config/routes.php, it's displayed at http://example.com/ * * So any other public methods not prefixed with an underscore will * map to /index.php/welcome/<method_name> * @see https://codeigniter.com/user_guide/general/urls.html */ public function __construct() { parent::__construct(); $this->load->helper('url'); /**LOADING HELPER TO AVOID PHP ERROR ****/ $this->load->model('Welcome_model','welcome'); /* Welcome_model as welcome */ } public function index() { $this->data['get_data']= $this->welcome->get_data(); $this->load->view('contents', $this->data, FALSE); } public function contents() { $this->data['get_data']= $this->welcome->get_data(); $this->load->view('contents', $this->data, FALSE); } public function savelikes() { $ipaddress=$_SERVER['REMOTE_ADDR']; $storyid=$this->input->post('Storyid'); $fetchlikes=$this->db->query('select likes from story where id="'.$storyid.'"'); $result=$fetchlikes->result(); $checklikes = $this->db->query('select * from storylikes where storyid="'.$storyid.'" and ipaddress = "'.$ipaddress.'"'); $resultchecklikes = $checklikes->num_rows(); if($resultchecklikes == '0' ){ if($result[0]->likes=="" || $result[0]->likes=="NULL") { $this->db->query('update story set likes=1 where id="'.$storyid.'"'); } else { $this->db->query('update story set likes=likes+1 where id="'.$storyid.'"'); } $data=array('storyid'=>$storyid,'ipaddress'=>$ipaddress); $this->db->insert('storylikes',$data); }else{ $this->db->delete('storylikes', array('storyid'=>$storyid, 'ipaddress'=>$ipaddress)); $this->db->query('update story set likes=likes-1 where id="'.$storyid.'"'); } $this->db->select('likes'); $this->db->from('story'); $this->db->where('id',$storyid); $query=$this->db->get(); $result=$query->result(); echo $result[0]->likes; } }
i have directly given all the model savelikes functions query also here, that is also possible way, if you want it in model, for that also you can change the code as you need.
MODEL
Welcome_model.php
<?php class Welcome_model extends CI_Model { public function __construct() { parent::__construct(); } public function get_data(){ $query=$this->db->query("SELECT st.* FROM story st ORDER BY st.id ASC"); return $query->result_array(); } }
VIEW
contents.php
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Display contents</title> <script src="//code.jquery.com/jquery-1.12.0.min.js"></script> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.6.3/css/font-awesome.css"> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.6.3/css/font-awesome.min.css"> <link rel="stylesheet" type="text/css" href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css"> <style type="text/css"> a:hover{ text-decoration: none; } h1 { color: #444; background-color: transparent; border-bottom: 1px solid #D0D0D0; font-size: 19px; font-weight: normal; margin: 0 0 14px 0; padding: 14px 15px 10px 15px; } #container { margin-top: 10px; border: 1px solid #D0D0D0; box-shadow: 0 0 8px #D0D0D0; } p { margin: 12px 15px 12px 15px; } i { cursor: pointer; text-decoration: none; } </style> </head> <body> <?php if(isset($get_data) && is_array($get_data) && count($get_data)): $i=1; foreach ($get_data as $key => $data) { ?> <div class="container" id="container"> <h1><?php echo $data['title']; ?></h1> <p><?php echo $data['description']; ?></p> <p><a onclick="javascript:savelike(<?php echo $data['id'];?>);"> <i class="fa fa-thumbs-up"></i> <span id="like_<?php echo $data['id'];?>"> <?php if($data['likes']>0){echo $data['likes'].' Likes';}else{echo 'Like';} ?> </span></a> </p> </div> <?php } endif; ?> <script type="text/javascript"> function savelike(storyid) { $.ajax({ type: "POST", url: "<?php echo site_url('Welcome/savelikes');?>", data: "Storyid="+storyid, success: function (response) { $("#like_"+storyid).html(response+" Likes"); } }); } </script> </body> </html>
I hope this post is really helpful to all codeigniter developers. keep visiting for more tutorials with live demos likes this. thank you.
RELATED POSTS :
Good one.
ReplyDeletenot working
ReplyDeletethanks
ReplyDeleteNot Working
ReplyDelete