Home >Backend Development >PHP Problem >How to achieve likes in php

How to achieve likes in php

coldplay.xixi
coldplay.xixiOriginal
2020-11-13 11:48:173483browse

php method to achieve likes: first get the article id, the code is [var id=$("#vote").attr('rel')]; then refresh the page after the like is successful and update the new The number of likes, the code is [window.location.reload()].

How to achieve likes in php

How to achieve likes in php:

I am currently working on a video website and need to achieve likes on videos I implemented the function by combining ajax and a database. The format of the database is that it has four fields: article id, like, dislike, and ip. Because an IP can only be liked once, an IP field is needed to store the liked IP, so that it is easy to judge whether the IP has been liked;

I will make two like and dislike pictures. button; the specific code is as follows:

<button style="margin-left:4px" id="vote" rel="<?php echo 文章id;?>">
<img src="点赞图片路径" alt="赞">
<span style="position:absolute;margin-top:6px;margin-left:2px;font-size:20px">
<span style="position:absolute;margin-top:-2px;margin-left:6px;font-size:20px">
<?php if(!$vnum){echo 0;}else{ echo 点赞次数;} ?>
</span>
</button>
<button style="margin-left:38px;margin-top:1px;position:absolute" id="dvote" rel="<?php echo 文章id;?>">
<img src="踩图片路径" alt="踩" >
<span style="position:absolute;margin-top:2px;margin-left:6px;font-size:20px">
<?php if(!$dnum){echo 0;}else{ echo 踩次数;} ?>
</span></button>

js program

<script type="text/javascript">
$(function(){
var id=$("#vote").attr(&#39;rel&#39;);//获取到文章id;
$("#vote").click(function(){
$.get("传到哪个页面?id="+id,function(r){
alert(r);
window.location.reload();//点赞成功后刷新页面更新新的点赞次数
})
})
$("#dvote").click(function(){
$.get("/news/dvote?id="+id,function(r){
alert(r);
window.location.reload();
})
})
})
</script>

I write it using the ci framework, so the vote method and dvote method under news.php represent likes and dislikes, specifically The code is as follows

public function vote(){
$id=$_GET[&#39;id&#39;];
$ip=getIP();
$getdata=$this->data_model;
$data=$getdata->get_vote_ip($id,$ip);
$msg="";
if(empty($data[&#39;ip&#39;]) || !$data[&#39;ip&#39;]){
  $data=array(&#39;nid&#39;=>$id,&#39;vote&#39;=>1,&#39;ip&#39;=>$ip);
  $re=$getdata->insert_vote($data);
  $msg.="点赞成功";
}else{
  $msg.="一个ip只能操作一次";
}
echo $msg;
}
public function dvote(){
$id=$_GET[&#39;id&#39;];
$ip=getIP();
$getdata=$this->data_model;
$data=$getdata->get_vote_ip($id,$ip);
//get_vote_ip($id,$ip),是在模型里面的查询该ip是否已经点赞过,具体代码 如下
//public function get_vote_ip($id,$ip){
// $query=$this->db->query("select * from 表名 where nid=&#39;{$id}&#39; and ip=&#39;{$ip}&#39;");
// $data=$query->result_array()[0];
// return $data;
// }
$msg="";
if(empty($data[&#39;ip&#39;]) || !$data[&#39;ip&#39;]){
  $data=array(&#39;nid&#39;=>$id,&#39;dvote&#39;=>0,&#39;ip&#39;=>$ip);
  $re=$getdata->insert_vote($data);
  $msg.="踩成功";
}else{
  $msg.="一个ip只能操作一次";
}
echo $msg;
}

After likes can be realized, the likes data needs to be updated. First, the like information of the article needs to be queried in the database

//获取点赞信息
public function get_vote($id){
$query=$this->db->query("select * from tx_vote where nid=&#39;{$id}&#39;");
$data=$query->result_array();
return $data;
}

Get the information and return it to the controller In it, the information of likes and dislikes is stored in the database in a loop, and then the new array lengths are calculated respectively to obtain the number of likes and dislikes. In this way, it can be output on the html page.

Related Free learning recommendations: php programming (video)

The above is the detailed content of How to achieve likes 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