php点赞性能如何实现?视频点赞功能实例详解
发布时间:2022-04-18 13:21:40 所属栏目:语言 来源:互联网
导读:php点赞功能如何实现?现在很多网站都有互动点赞的功能,那么点赞功能是怎么实现呢?下面分享一个php+js实现点赞功能的代码,有需要的盆友可以参考一下。 这里实现视频的点赞功能,是结合ajax和数据库实现的,数据库的格式为有四个字段:文章id,赞,踩,ip。
php点赞功能如何实现?现在很多网站都有互动点赞的功能,那么点赞功能是怎么实现呢?下面分享一个php+js实现点赞功能的代码,有需要的盆友可以参考一下。 这里实现视频的点赞功能,是结合ajax和数据库实现的,数据库的格式为有四个字段:文章id,赞,踩,ip。因为需要一个ip只能点赞一次,所以需要一个ip字段存储点赞的ip,这样便于判断该ip是否已经点赞过了; 我将点赞和踩的图片做成两个按钮;具体代码如下: <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.="点赞成功"; // $query=$this->db->query("select * from 表名 where nid='{$id}' and ip='{$ip}'"); // $data=$query->result_array()[0]; (编辑:南通站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |