學(xué)習(xí)php mysql ajax 局部刷新點(diǎn)贊/取消點(diǎn)贊功能

發(fā)布時(shí)間:2024-02-14
記錄每個贊的點(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)不太美觀
上一個:三款白領(lǐng)美體瘦身茶
下一個:普洱茶該如何保存?

華為云服務(wù)器怎么調(diào)中文模式
手機(jī)安裝不了美團(tuán)怎么辦(小米手機(jī)安裝不了應(yīng)用商店外的軟件)
南城漢代?;韬顕z址公園開園時(shí)間 需要預(yù)約嗎
陸生和附生的掌類植物有何不同?
綠茶松糕的做法
北京菊花展在哪個公園 北京菊花展展區(qū)地址
舟形毛蟲
高壓介質(zhì)損耗測試儀在電力測試中用途
兩字母域名FI.cn被區(qū)塊鏈終端拿下
新版 球囊直徑與充盈壓力關(guān)系測試儀 自動測量結(jié)構(gòu)
十八禁 网站在线观看免费视频_2020av天堂网_一 级 黄 色 片免费网站_绝顶高潮合集Videos