php+mysql+ajax點贊和踩的功能

來源:互聯網
上載者:User

標籤:style   blog   http   io   ar   color   os   sp   java   

嫌麻煩的也可以:點擊下載


HTML:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><script src="common/js/jquery.js" type="text/javascript"></script><script language="JavaScript" type="text/javascript">jQuery(function($) {//這兩個是滑鼠放上去的效果$(".zhan").hover(function(){ $(this).children("b").toggle();}); $(".cai").hover(function(){ $(this).children("b").toggle();});  }); </script><script type="text/javascript"> <!--這兩個點擊事件可以改進的,因為事件問題,所以就弄了兩個點擊事件  $(document).ready(function(e) {//點贊(給力)$('.zhan').click(function(){var zhan=$(this);var left = parseInt($(this).offset().left)+10, top =  parseInt($(this).offset().top)-10, obj=$(this);$(this).append('<div id="zhan"><b>+1<\/b></\div>');$('#zhan').css({'position':'absolute','z-index':'1', 'color':'#C30','left':left+'px','top':top+'px'}).animate({top:top-10,left:left+10},'slow',function(){$(this).fadeIn('fast').remove();var Num = parseInt(obj.find('span').text());Num++;obj.find('span').text(Num);});$.ajax({  url:"ajax.php?action=zan",  type:"post",  data:"answer_id="+$(this).attr("alt"),  success:function(datas){  zhan.unbind('click');  zhan.parent().parent().siblings().children().children().unbind('click');zhan.parent().parent().siblings().children().children().attr("class","evaluate-inner");  zhan.attr("class","evaluate-inner"); }  }); }); //踩(不給力) $('.cai').click(function(){var cai=$(this);var left = parseInt($(this).offset().left)+10, top =  parseInt($(this).offset().top)-10, obj=$(this);$(this).append('<div id="cai"><b>-1<\/b></\div>');$('#cai').css({'position':'absolute','z-index':'1', 'color':'#7DAAD5','left':left+'px','top':top+'px'}).animate({top:top-10,left:left+10},'slow',function(){$(this).fadeIn('fast').remove();var Num = parseInt(obj.find('span').text());Num--;obj.find('span').text(Num);}); $.ajax({  url:"ajax.php?action=cai",  type:"post",  data:"answer_id="+$(this).attr("alt"),  success:function(datas){  cai.unbind('click');  cai.parent().parent().siblings().children().children().unbind('click');cai.parent().parent().siblings().children().children().attr("class","evaluate-inner");  cai.attr("class","evaluate-inner"); }  }); }); }); //--></script></head><body><!--給力開始--><span data-evaluate="0" id="evaluate-1908165795" class="evaluate" alog-action="qb-zan-btn"> <span  class="evaluate-outer"><!-----判斷對應的回答的id是否在自己建立的儲存贊和踩資料的php檔案,如果贊或者踩存在一個的話就不能再單擊----><span class="evaluate-inner {OVOV:if $answer_datas.ans_id|in_array:$zan_list or $answer_datas.ans_id|in_array:$cai_list/}{OVOV:else/}zhan{OVOV:/if/}" alt="{OVOV:$answer_datas.ans_id/}"> <b  style="display:none" class="evaluate-tip evaluate-num-fixed">給力</b> <b  class="evaluate-num evaluate-num-fixed" ><span>{OVOV:$answer_datas.goods/}</span></b> </span> </span> </span><!--不給力開始--><span data-evaluate="0" id="evaluate-bad-1908165795" class="evaluate evaluate-bad" alog-action="qb-evaluate-outer"> <span class="evaluate-outer"> <span class="evaluate-inner {OVOV:if $answer_datas.ans_id|in_array:$zan_list or $answer_datas.ans_id|in_array:$cai_list/}{OVOV:else/}cai{OVOV:/if/}" alt="{OVOV:$answer_datas.ans_id/}"> <b  style="display: none;" class="evaluate-tip evaluate-num-fixed">不給力</b> <b  class="evaluate-num evaluate-num-fixed" style="display: inline-block;"><span>{OVOV:$answer_datas.bads/}</span></b> </span> </span> </span></div></div><!--評論回答開始--></body></html>



HTML對應的php檔案


<?php$action=$_GET['action'];//擷取get方式傳過來的actionrequire_once("array/ovovsys_zan.php");switch($action){case 'detail':/*****贊開始****/$zan_list = json_decode(str_replace("'",'"',$zan_json),true);$zan_lists=array();foreach($zan_list as $key=>$value){if($value['uid']==$uid){ $zan_lists[]=$value['answer_id'];}} $smarty->assign('zan_list',$zan_lists);//查詢當前登入的會員的uid對應的點贊的回答 /*****贊結束****/ /*****踩開始****/$cai_list = json_decode(str_replace("'",'"',$cai_json),true);$cai_lists=array();foreach($cai_list as $key=>$value){if($value['uid']==$uid){ $cai_lists[]=$value['answer_id'];}} $smarty->assign('cai_list',$cai_lists);//查詢當前登入的會員的uid對應的踩的回答 /*****踩結束****/$smarty->display('自己的靜態頁面');exit();break;}?>


ajax檔案:

<?php require_once("array/ovovsys_zan.php");//這裡引用array檔案夾中的數組檔案$action=trim($_GET['action']);if(empty($action)){echo '<font color=red>參數錯誤!</font>';exit();}switch($action){ case 'zan'://點贊(給力)//$zan_json是數組檔案中設定的陣列變數,存的是json資料----1$zan_array = json_decode(str_replace("'",'"',$zan_json),true);//擷取當前的會員的uid-----2$user_answer['uid']=$uid;//擷取對應的贊的回答的id----2$user_answer['answer_id']=$_POST['answer_id'];//將會員的uid和答案的id一塊的數組放到指定的數組中----3$zan_array[]=$user_answer;$config_path = 'shuzu/ovovsys_zan.php';if(!file_exists($config_path)){$Base->ErrorMsg('檔案錯誤:shuzu/ovovsys_zan.php 不存在!');exit();}//將指定的數群組轉換成json資料,並且將雙引號轉換成單引號,不然放到數組檔案中後會出錯----4$zan_new_json = str_replace('"',"'",json_encode($zan_array));$fp = @fopen($config_path,'r+');if(!$content = @fread($fp,filesize($config_path))){$Base->ErrorMsg('無法讀取shuzu/ovovsys_zan.php檔案!');exit();}//執行替換,將數組檔案中對應的json資料----5$content = str_replace("\$zan_json = \"{$zan_json}\"","\$zan_json = \"{$zan_new_json}\"",$content);$fp = @fopen($config_path,'w+'); @fwrite($fp,$content); //修改對應的資料表中的資料----5$Db->ThisQuery("update `".$db_prefix."ask_answers` set `goods`=`goods`+1 where `ans_id`=".$user_answer['answer_id']."");exit();break; case 'cai'://踩(不給力)$cai_array = json_decode(str_replace("'",'"',$cai_json),true);$user_answer['uid']=$uid;$user_answer['answer_id']=$_POST['answer_id'];$cai_array[]=$user_answer;$config_path = 'shuzu/ovovsys_zan.php';if(!file_exists($config_path)){$Base->ErrorMsg('檔案錯誤:shuzu/ovovsys_zan.php 不存在!');exit();}$cai_new_json = str_replace('"',"'",json_encode($cai_array));$fp = @fopen($config_path,'r+');if(!$content = @fread($fp,filesize($config_path))){$Base->ErrorMsg('無法讀取shuzu/ovovsys_zan.php檔案!');exit();}$content = str_replace("\$cai_json = \"{$cai_json}\"","\$cai_json = \"{$cai_new_json}\"",$content);$fp = @fopen($config_path,'w+'); @fwrite($fp,$content); $Db->ThisQuery("update `".$db_prefix."ask_answers` set `bads`=`bads`-1 where `ans_id`=".$user_answer['answer_id']."");exit();break;}?>


對應的資料檔案(因為建立資料庫時沒有建立踩和贊的再斷,所以這裡就用php檔案存變數的方式儲存了一下答案對應的贊和踩的資料,其中包含使用者的id和答案的id)

<?php$zan_json = "";$cai_json = "";?>你沒看錯,就是兩個變數

如果嫌複製著麻煩的話,也可以(點擊下載壓縮檔),親,不要積分的喲!



php+mysql+ajax點贊和踩的功能

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.