首页 > 编程语言 >php--点赞功能的实现

php--点赞功能的实现

时间:2023-10-13 11:47:24浏览次数:28  
标签:function -- ip 点赞 vote php data id

最近在做一个视频网站,需要实现视频的点赞功能,我是结合ajax和数据库实现的,数据库的格式为有四个字段:文章id,赞,踩,ip。因为需要一个ip只能点赞一次,所以需要一个ip字段存储点赞的ip,这样便于判断该ip是否已经点赞过了;

我将点赞和踩的图片做成两个按钮;具体代码如下:https://www.52mwyd.com/sh/

<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程序

<script type="text/javascript">
$(function(){
var id=$("#vote").attr('rel');//获取到文章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>

我是用ci框架写,所以在news.php下面的vote方法和dvote方法代表的是赞和踩,具体代码如下

public function vote(){
$id=$_GET['id'];
$ip=getIP();
$getdata=$this->data_model;
$data=$getdata->get_vote_ip($id,$ip);
$msg="";
if(empty($data['ip']) || !$data['ip']){
  $data=array('nid'=>$id,'vote'=>1,'ip'=>$ip);
  $re=$getdata->insert_vote($data);
  $msg.="点赞成功";
}else{
  $msg.="一个ip只能操作一次";
}
echo $msg;
}

public function dvote(){
$id=$_GET['id'];
$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='{$id}' and ip='{$ip}'");
// $data=$query->result_array()[0];

// return $data;
// }


$msg="";
if(empty($data['ip']) || !$data['ip']){
  $data=array('nid'=>$id,'dvote'=>0,'ip'=>$ip);
  $re=$getdata->insert_vote($data);
  $msg.="踩成功";
}else{
  $msg.="一个ip只能操作一次";
}
echo $msg;
}

 

点赞可以实现以后,就是需要将点赞数据进行更新,首先需要在数据库查询该篇文章所以的点赞信息

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

获取信息返回到  控制器里面将赞和踩的信息循环分别存入到数据库中然后分别计算新的数组长度就可以获取赞和踩的次数了,这样的再html页面输出就可以了

生活_美文阅读网

标签:function,--,ip,点赞,vote,php,data,id
From: https://www.cnblogs.com/zx8868/p/17761717.html

相关文章

  • 使用NPOI修改Excel数据
    读取excel文件阿松大读取excel文件读取excel文件读取excel文件publicvoidSignIn(){Useruser=_userService.GetUserByName(Username);if(user!=null&&user.Password==Password){#region记住登录信息//将当前的配置序列化为j......
  • springboot2.4下使用JUnit依赖注入失败的解决方案
    首先在pom.xml下引入JUnit必须的包:<dependency><groupId>junit</groupId><artifactId>junit</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId&......
  • 做过destoon和discuz之后的总结。
    做过了destoon和discuz这两种相对复杂一点的模版二次开发以后,总想写点总结,对再次学习其他模版有所启迪。1、给我的印象,PHP模版,大都是include各种文件,而且include的类型也不只一种,如:includetemplate是用模版引擎解析模版, includelibfile是加载后台文件(discuz中的),当我们接触一......
  • java学习日记day02
    java学习日记day02冯诺伊曼体系)cmd指令......
  • 备忘录模式
      ......
  • MyBatis 动态 SQL 最全教程,这样写 SQL 太爽了!
    动态SQL是MyBatis的强大特性之一。在JDBC或其它类似的框架中,开发人员通常需要手动拼接SQL语句。根据不同的条件拼接SQL语句是一件极其痛苦的工作。例如,拼接时要确保添加了必要的空格,还要注意去掉列表最后一个列名的逗号。而动态SQL恰好解决了这一问题,可以根据场景动态......
  • docker中使用systemctl方法
    想在docker中使用Flexmonster,但是在配置环境后,发现Flexmonster需要使用systemctl来管理服务,然而在docker容器中没有systemctl可用,于是开始折腾之旅!以下是解决办法:1、下载systemctlwgethttps://raw.githubusercontent.com/gdraheim/docker-systemctl-replacement/master/files/d......
  • Linux下配置安装PHP环境
    参考别人的做法,遇到问题上网查,下面就是安装步骤.一、安装Apache2.2.221、到官网下载  https://www.52mwyd.com/2、解压   tar -zxvfhttpd-2.2.22.tar.gz3、建立目标文件夹(注意以下所有操作都时在root用户下执行的)   mkdir/usr/local/apache2   也就是说等下......
  • 06 模板语法
    template:模板主要讲的是vue的基本使用语法1.文本插值和js的结合使用2.原始HTML......
  • 数据分析基础:数据可视化+数据分析报告
    数据分析是指通过对大量数据进行收集、整理、处理和分析,以发现其中的模式、趋势和关联,并从中提取有价值的信息和知识。数据可视化和数据分析报告是数据分析过程中非常重要的两个环节,它们帮助将数据转化为易于理解和传达的形式,提供决策支持和洞察力。在接下来的说明中,我将详细......