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:
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="/static/imghwm/default1.png" data-src="点赞图片路径" class="lazy" 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="/static/imghwm/default1.png" data-src="踩图片路径" class="lazy" 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('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>
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['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; }
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='{$id}'"); $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!

The article compares ACID and BASE database models, detailing their characteristics and appropriate use cases. ACID prioritizes data integrity and consistency, suitable for financial and e-commerce applications, while BASE focuses on availability and

The article discusses securing PHP file uploads to prevent vulnerabilities like code injection. It focuses on file type validation, secure storage, and error handling to enhance application security.

Article discusses best practices for PHP input validation to enhance security, focusing on techniques like using built-in functions, whitelist approach, and server-side validation.

The article discusses strategies for implementing API rate limiting in PHP, including algorithms like Token Bucket and Leaky Bucket, and using libraries like symfony/rate-limiter. It also covers monitoring, dynamically adjusting rate limits, and hand

The article discusses the benefits of using password_hash and password_verify in PHP for securing passwords. The main argument is that these functions enhance password protection through automatic salt generation, strong hashing algorithms, and secur

The article discusses OWASP Top 10 vulnerabilities in PHP and mitigation strategies. Key issues include injection, broken authentication, and XSS, with recommended tools for monitoring and securing PHP applications.

The article discusses strategies to prevent XSS attacks in PHP, focusing on input sanitization, output encoding, and using security-enhancing libraries and frameworks.

The article discusses the use of interfaces and abstract classes in PHP, focusing on when to use each. Interfaces define a contract without implementation, suitable for unrelated classes and multiple inheritance. Abstract classes provide common funct


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver Mac version
Visual web development tools