php+js實(shí)現(xiàn)點(diǎn)贊功能的示例詳解
最近在做一個(gè)視頻網(wǎng)站,需要實(shí)現(xiàn)視頻的點(diǎn)贊功能,我是結(jié)合ajax和數(shù)據(jù)庫(kù)實(shí)現(xiàn)的,數(shù)據(jù)庫(kù)的格式為有四個(gè)字段:文章id,贊,踩,ip。因?yàn)樾枰粋€(gè)ip只能點(diǎn)贊一次,所以需要一個(gè)ip字段存儲(chǔ)點(diǎn)贊的ip,這樣便于判斷該ip是否已經(jīng)點(diǎn)贊過(guò)了;
我將點(diǎn)贊和踩的圖片做成兩個(gè)按鈕;具體代碼如下:
<button style="margin-left:4px" id="vote" rel="<?php echo 文章id;?>"> <img src="點(diǎn)贊圖片路徑" 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 點(diǎn)贊次數(shù);} ?> </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 踩次數(shù);} ?> </span> </button>
js程序
<script type="text/javascript"> $(function(){ var id=$("#vote").attr('rel');//獲取到文章id; $("#vote").click(function(){ $.get("傳到哪個(gè)頁(yè)面?id="+id,function(r){ alert(r); window.location.reload();//點(diǎn)贊成功后刷新頁(yè)面更新新的點(diǎn)贊次數(shù) }) }) $("#dvote").click(function(){ $.get("/news/dvote?id="+id,function(r){ alert(r); window.location.reload(); }) }) }) </script>
我是用ci框架寫(xiě),所以在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.="點(diǎn)贊成功"; }else{ $msg.="一個(gè)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),是在模型里面的查詢?cè)搃p是否已經(jīng)點(diǎn)贊過(guò),具體代碼 如下 //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.="一個(gè)ip只能操作一次"; } echo $msg; }
點(diǎn)贊可以實(shí)現(xiàn)以后,就是需要將點(diǎn)贊數(shù)據(jù)進(jìn)行更新,首先需要在數(shù)據(jù)庫(kù)查詢?cè)撈恼滤缘狞c(diǎn)贊信息
//獲取點(diǎn)贊信息 public function get_vote($id){ $query=$this->db->query("select * from tx_vote where nid='{$id}'"); $data=$query->result_array(); return $data; }
獲取信息返回到 控制器里面將贊和踩的信息循環(huán)分別存入到數(shù)據(jù)庫(kù)中然后分別計(jì)算新的數(shù)組長(zhǎng)度就可以獲取贊和踩的次數(shù)了,這樣的再html頁(yè)面輸出就可以了
到此這篇關(guān)于php+js實(shí)現(xiàn)點(diǎn)贊功能的示例的文章就介紹到這了,更多相關(guān)php+js實(shí)現(xiàn)點(diǎn)贊功能內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
php中ob_get_length緩沖與獲取緩沖長(zhǎng)度實(shí)例
這篇文章主要介紹了php中ob_get_length緩沖與獲取緩沖長(zhǎng)度的方法,實(shí)例分析了緩沖輸出的用法與相關(guān)技巧,具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2014-11-11php打開(kāi)本地exe程序,js打開(kāi)本地exe應(yīng)用程序,并傳遞相關(guān)參數(shù)方法
下面小編就為大家分享一篇php打開(kāi)本地exe程序,js打開(kāi)本地exe應(yīng)用程序,并傳遞相關(guān)參數(shù)方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-02-02PHP+APACHE實(shí)現(xiàn)網(wǎng)址偽靜態(tài)
這篇文章主要介紹了PHP+APACHE通過(guò)使用mod rewrite模塊實(shí)現(xiàn)網(wǎng)址偽靜態(tài)的相關(guān)資料,需要的朋友可以參考下2015-02-02PHP關(guān)于htmlspecialchars、strip_tags、addslashes的解釋
這篇文章主要介紹了PHP關(guān)于htmlspecialchars、strip_tags、addslashes的解釋,需要的朋友可以參考下2014-07-07