記錄每個贊的點(diǎn)贊用戶,以及對贊的數(shù)量統(tǒng)計(jì)
首先判斷用戶是否點(diǎn)贊。根據(jù)是否點(diǎn)贊,載入不同的html,調(diào)用不同的方法
已點(diǎn)贊
如果已點(diǎn)贊,顯示已點(diǎn)贊的html,進(jìn)行取消點(diǎn)贊操作
相關(guān)學(xué)習(xí)推薦:php編程(視頻)
未點(diǎn)贊
如果未點(diǎn)贊,顯示未點(diǎn)贊的html,進(jìn)行點(diǎn)贊操作
對于不同操作,對數(shù)據(jù)庫進(jìn)行增加或減少操作。同時(shí)對于不同用戶的點(diǎn)贊,進(jìn)行增加記錄或刪除記錄操作。通過控制不同按鈕的背景,來顯示不同的效果。通過記錄不同用戶的用戶id和贊的id之間的關(guān)系,進(jìn)行不同點(diǎn)贊的限制。
效果演示
當(dāng)用戶id為1時(shí),進(jìn)行點(diǎn)贊,點(diǎn)贊數(shù)加1
$con = new mysqli('localhost','root','','test'); if (!$con) { die('連接數(shù)據(jù)庫失敗,失敗原因:' . mysqli_error()); }else { // echo "連接成功"; }對用戶是否點(diǎn)贊進(jìn)行判斷(操作頁面)
對數(shù)據(jù)庫的信息進(jìn)行提取
//假設(shè)用戶編號為1 $uid="1"; //假設(shè)贊編號為1 $zanid="1"; //查找贊id為1的點(diǎn)贊數(shù) $count=mysqli_query($con, "select count from zancount where zanid=$zanid "); $countresult=mysqli_fetch_array($count); $countzan=$countresult['count']; //查找改用戶是否對贊id為1 點(diǎn)贊 $uidlike=mysqli_query($con, "select * from zanrecord where uid=$uid "); $result=mysqli_fetch_array($uidlike);對用戶是否點(diǎn)贊進(jìn)行判斷,并輸出不同的html
//點(diǎn)贊if (isset($result)) { $showzan.=<<<html <p class="dolikep" id="dolikep"> <button id="dolike" οclick="zandel()"></button> <span id="zan">$countzan</span> </p>html; } //沒點(diǎn)贊 else { $showzan.=<<<html <p class="dolikep" id="dolikep"> <button id="donolike" οclick="zan()"></button> <span id="zan">$countzan</span> </p>html; } echo $showzan; ?>css樣式
#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; }調(diào)用的ajax方法
傳遞需要的數(shù)據(jù),這里傳遞的時(shí)zanid 和uid
記得引入jq文件
點(diǎn)贊
function zan(){ $.ajax({ type:"post", url:"./likesever.php", data:{'zanid':$("#zanid").val(),'uid':$("#uid").val()}, success:function(text){ $("#dolikep").html(text); } }); }取消點(diǎn)贊
function zandel(){ $.ajax({ type:"post", url:"./dissever.php", data:{'zanid':$("#zanid").val(),'uid':$("#uid").val()}, success:function(text){ $("#dolikep").html(text); } }); }處理代碼
點(diǎn)贊處理
//更新贊總數(shù)的數(shù)據(jù) mysqli_query($con,"update zancount set count = count 1 where zanid=$zanid"); //添加一條點(diǎn)贊記錄 mysqli_query($con,"insert into zanrecord(zanid,uid) values($zanid, $uid); "); //查找贊的總數(shù) @$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" οclick="zandel()"></button> <span id="zan">$countzan</span>html; echo $show;取消點(diǎn)贊處理
//更新贊總數(shù)的數(shù)據(jù) mysqli_query($con,"update zancount set count = count-1 where zanid=$zanid"); //添加一條點(diǎn)贊記錄 mysqli_query($con,"delete from zanrecord where zanid=$zanid and uid=$uid "); //查找贊的總數(shù) @$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" οclick="zan()"></button> <span id="zan">$countzan</span>html;點(diǎn)贊的圖片
圖片自己畫的,有點(diǎn)不太美觀