首先我们需要知道这个功能逻辑流程是什么,其实很简单。首先没点赞的时候显示默认的HTML样式,当点赞就用PHP+AJAX读取数据库内的字段,返回判断是否已经存在,存在代表已经点赞,不存在就写入,然后更改HTML样式。
下面详细讲解,首先需要建立一个数据库,分为两个数据表。一个进行对点赞数的统计,一个进行不同用户的点赞记录。
写PHP接口,首先连接这个数据库。
$con = new mysqli('localhost','root','','test'); if (!$con) { die('连接数据库失败,失败原因:' . mysqli_error()); }else { // echo "连接成功"; }
对用户是否点赞进行判断(操作页面)
对数据库的信息进行提取
//假设用户编号为1 $uId="1"; //假设赞编号为1 $zanId="1"; //查找赞id为1的点赞数 $count=mysqli_query($con, "SELECT count FROM zanCount WHERE zanId=$zanId "); $countResult=mysqli_fetch_array($count); $countZan=$countResult['count']; //查找改用户是否对赞id为1 点赞 $uIdLike=mysqli_query($con, "SELECT * FROM zanRecord WHERE uId=$uId "); $result=mysqli_fetch_array($uIdLike);
对用户是否点赞进行判断,并输出不同的html
//点赞 if (isset($result)) { $showZan.=<<<html <div class="dolikeDIV" id="dolikeDIV"> <button id="dolike" οnclick="zanDel()"></button> <span id="zan">$countZan</span> </div> html; } //没点赞 else { $showZan.=<<<html <div class="dolikeDIV" id="dolikeDIV"> <button id="donolike" οnclick="zan()"></button> <span id="zan">$countZan</span> </div> html; } echo $showZan; ?>
#dolike, #donolike { width:30px; height:30px; margin-left:20px; float:left;} #donolike { background:url(./images/nolike.png); background-size:30px 30px; } #dolike { background:url(./images/like.png); background-size:30px 30px; }
调用的ajax方法
传递需要的数据,这里传递的时zanId 和uId
记得引入jq文件
点赞
function zan() { $.ajax({ type:"POST", url:"./likeSever.php", data:{'zanId':$("#zanId").val(),'uId':$("#uId").val()}, success:function(text){ $("#dolikeDIV").html(text); } }); }
取消点赞
function zanDel() { $.ajax({ type:"POST", url:"./disSever.php", data:{'zanId':$("#zanId").val(),'uId':$("#uId").val()}, success:function(text){ $("#dolikeDIV").html(text); } }); }
点赞处理
//更新赞总数的数据 mysqli_query($con,"UPDATE zanCount SET count = count+1 WHERE zanId=$zanId"); //添加一条点赞记录 mysqli_query($con,"INSERT INTO zanRecord(zanId,uId) VALUES($zanId, $uId); "); //查找赞的总数 @$count=mysqli_query($con, "SELECT count FROM zanCount WHERE zanId=$zanId "); @$countResult=mysqli_fetch_array($count); @$countZan=$countResult['count']; //更改输出的html $show=""; $show=<<<html <button id="dolike" οnclick="zanDel()"></button> <span id="zan">$countZan</span> html; echo $show;
取消点赞处理
//更新赞总数的数据 mysqli_query($con,"UPDATE zanCount SET count = count-1 WHERE zanId=$zanId"); //添加一条点赞记录 mysqli_query($con,"DELETE FROM zanRecord WHERE zanId=$zanId AND uId=$uId "); //查找赞的总数 @$count=mysqli_query($con, "SELECT count FROM zanCount WHERE zanId=$zanId "); @$countResult=mysqli_fetch_array($count); @$countZan=$countResult['count']; //更新html $show=""; $show.=<<<html <button id="donolike" οnclick="zan()"></button> <span id="zan">$countZan</span> html; //更新赞总数的数据 mysqli_query($con,"UPDATE zanCount SET count = count-1 WHERE zanId=$zanId"); //添加一条点赞记录 mysqli_query($con,"DELETE FROM zanRecord WHERE zanId=$zanId AND uId=$uId "); //查找赞的总数 @$count=mysqli_query($con, "SELECT count FROM zanCount WHERE zanId=$zanId "); @$countResult=mysqli_fetch_array($count); @$countZan=$countResult['count']; //更新html $show=""; $show.=<<<html <button id="donolike" οnclick="zan()"></button> <span id="zan">$countZan</span> html;