欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

php+js實(shí)現(xiàn)點(diǎn)贊功能的示例詳解

 更新時(shí)間:2020年08月07日 17:02:13   作者:七秒鐘的記憶qing  
這篇文章主要介紹了php+js實(shí)現(xiàn)點(diǎn)贊功能的示例詳解,文章通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧

最近在做一個(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)文章

最新評(píng)論