php unlike,PHP结合jQuery实现的评论顶、踩功能
當(dāng)我們?yōu)g覽網(wǎng)頁時(shí),我們想對(duì)網(wǎng)頁內(nèi)容如文章、評(píng)論中的觀點(diǎn)持贊同或反對(duì)意見時(shí),可以通過點(diǎn)擊網(wǎng)頁中的“頂”和“踩”來進(jìn)行投票。而整個(gè)交互過程,開發(fā)者可以通過ajax異步來實(shí)現(xiàn),從而提高用戶體驗(yàn)。
本文結(jié)合實(shí)例,講解使用PHP+MySql+jQuery實(shí)現(xiàn)的“頂”和“踩”投票功能,通過記錄用戶IP,判斷用戶的投票行為是否有效,該實(shí)例也可以擴(kuò)展到投票系統(tǒng)中。如果您具備PHP、MySql和jQuery相關(guān)基礎(chǔ)知識(shí),那么請(qǐng)繼續(xù)往下閱讀。
準(zhǔn)備
我們先要準(zhǔn)備為整個(gè)實(shí)例運(yùn)行所需的mysql數(shù)據(jù)表,實(shí)例中需要兩個(gè)表,votes表用來記錄對(duì)應(yīng)文章或評(píng)論的用戶投票數(shù),并且我們默認(rèn)寫入一條id為1的數(shù)據(jù)以便演示,votes_ip表用來記錄用戶每次投票的IP,程序根據(jù)用戶IP決定投票是否有效。
CREATE TABLE IF NOT EXISTS `votes` (
`id` int(10) NOT NULL AUTO_INCREMENT,
`likes` int(10) NOT NULL DEFAULT '0',
`unlikes` int(10) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
INSERT INTO `votes` (`id`, `likes`, `unlikes`) VALUES
(1, 30, 10);
CREATE TABLE IF NOT EXISTS `votes_ip` (
`id` int(10) NOT NULL,
`vid` int(10) NOT NULL,
`ip` varchar(40) NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
HTML
在頁面中,有兩個(gè)分別表示“頂”和“踩”的按鈕,即#dig_up和#dig_down,按鈕上記錄了投票的票數(shù)以及分別所占的百分比,非常直觀的對(duì)比投票結(jié)果。
很好,很強(qiáng)大!
太差勁了!
CSS
我們必須使用CSS來美化頁面,我們使用一張圖diggs.png來定位不同的按鈕背景,通過設(shè)置position來定位各元素之間的位置關(guān)系。
.digg{width:420px; height:120px; margin:80px auto 20px auto; position:relative}
#dig_up,#dig_down{width:200px; height:48px; margin:10px; position:relative;
border:1px solid #d3d3d3; padding-left:42px; cursor:pointer}
.digup{background:url(diggs.png) no-repeat 4px 2px;}
.digup_on{background:url(diggs.png) no-repeat 4px -49px;}
.digdown{background:url(diggs.png) no-repeat 4px -102px;}
.digdown_on{background:url(diggs.png) no-repeat 4px -154px;}
#num_up,#num_down{position:absolute; right:6px; top:18px; font-size:20px;}
#dig_up p{height:24px; line-height:24px; color:#360}
#dig_down p{height:24px; line-height:24px; color:#f30}
.bar{width:100px; height:12px; line-height:12px; border:1px solid #f0f0f0;
position:relative; text-align:center}
.bar span{display:block; height:12px; }
.bar i{position:absolute; top:0; left:104px;}
#bar_up span{background:#360}
#bar_down span{background:#f60}
#msg{position:absolute; right:20px; top:40px; font-size:18px; color:#f00}
jQuery
本示例還依賴于jQuery,因此一定不能忘了在頁面中先載入jquery庫文件。
首先,jQuery要處理的是鼠標(biāo)分別滑向兩個(gè)投票按鈕時(shí)變換的背景圖片,通過addClass()和removeClass()來實(shí)現(xiàn)。
$(function(){
//鼠標(biāo)滑向和離開投票按鈕時(shí),變換背景樣式
$("#dig_up").hover(function(){
$(this).addClass("digup_on");
},function(){
$(this).removeClass("digup_on");
});
$("#dig_down").hover(function(){
$(this).addClass("digdown_on");
},function(){
$(this).removeClass("digdown_on");
});
//初始化數(shù)據(jù)
getdata("do.php",1);
//單擊“頂”時(shí)
$("#dig_up").click(function(){
getdata("do.php?action=like",1);
});
//單擊“踩”時(shí)
$("#dig_down").click(function(){
getdata("do.php?action=unlike",1);
});
});
然后,我們初始化數(shù)據(jù),就是頁面加載后,要顯示初始的已經(jīng)投票的結(jié)果,包括各投票數(shù)和所占百分比。我們將獲取數(shù)據(jù)的操作寫在一個(gè)自定義函數(shù)getdata()中,通過傳遞不同的請(qǐng)求地址和id參數(shù)來完成數(shù)據(jù)的載入。函數(shù)getdata()中,向URL發(fā)送一個(gè)ajax請(qǐng)求,根據(jù)后臺(tái)處理的返回結(jié)果,如果投票成功則更改頁面中相應(yīng)的元素內(nèi)容,包括投票數(shù)和所占百分比。
function getdata(url,sid){
$.getJSON(url,{id:sid},function(data){
if(data.success==1){//投票成功
$("#num_up").html(data.like);
//通過控制寬度來顯示百分比進(jìn)度條效果
$("#bar_up span").css("width",data.like_percent);
$("#bar_up i").html(data.like_percent);
$("#num_down").html(data.unlike);
$("#bar_down span").css("width",data.unlike_percent);
$("#bar_down i").html(data.unlike_percent);
}else{//投票失敗
$("#msg").html(data.msg).show().css({'opacity':1,'top':'40px'})
.animate({top:'-50px',opacity:0}, "slow");
}
});
}
PHP
數(shù)據(jù)的獲取都是通過do.php來完成,do.php根據(jù)前端頁面?zhèn)鬟f的參數(shù),連接數(shù)據(jù)庫,根據(jù)條件判斷分別進(jìn)入“頂”、“踩”和初始數(shù)據(jù)處理模塊,以下是do.php模塊代碼結(jié)構(gòu):
include_once("connect.php");//連接數(shù)據(jù)庫
$action = $_GET['action'];
$id = 1;
$ip = get_client_ip();//獲取ip
if($action=='like'){//頂
likes(1,$id,$ip);
}elseif($action=='unlike'){//踩
likes(0,$id,$ip);
}else{
echo jsons($id);
}
函數(shù)likes()用來處理“頂”和“踩”投票模塊,首先是判斷用戶IP是否已經(jīng)投票過了,如果已經(jīng)投票則直接返回相應(yīng)提示,如果用戶IP沒有投票記錄則更新votes表,將對(duì)應(yīng)的投票數(shù)加1,然后向votes_ip表中插入該用戶的IP記錄,如果操作成功,則調(diào)用jsons()輸出投票后的投票數(shù)和所占百分比等數(shù)據(jù),否則輸入操作失敗的提示信息。
function likes($type,$id,$ip){
$ip_sql=mysql_query("select ip from votes_ip where vid='$id' and ip='$ip'");
$count=mysql_num_rows($ip_sql);
if($count==0){//還沒有頂過
if($type==1){//頂
$sql = "update votes set likes=likes+1 where id=".$id;
}else{//踩
$sql = "update votes set unlikes=unlikes+1 where id=".$id;
}
mysql_query($sql);
$sql_in = "insert into votes_ip (vid,ip) values ('$id','$ip')";
mysql_query($sql_in);
if(mysql_insert_id()>0){
echo jsons($id);
}else{
$arr['success'] = 0;
$arr['msg'] = '操作失敗,請(qǐng)重試';
echo json_encode($arr);
}
}else{
$msg = $type==1?'您已經(jīng)頂過了':'您已經(jīng)踩過了';
$arr['success'] = 0;
$arr['msg'] = $msg;
echo json_encode($arr);
}
}
函數(shù)jsons()用來讀取votes表中相應(yīng)id的投票數(shù),并計(jì)算百分比,最后將這些信息以json格式輸出,供前端頁面使用。
function jsons($id){
$query = mysql_query("select * from votes where id=".$id);
$row = mysql_fetch_array($query);
$like = $row['likes'];
$unlike = $row['unlikes'];
$arr['success']=1;
$arr['like'] = $like;
$arr['unlike'] = $unlike;
$like_percent = round($like/($like+$unlike),3)*100;
$arr['like_percent'] = $like_percent.'%';
$arr['unlike_percent'] = (100-$like_percent).'%';
return json_encode($arr);
}
該實(shí)例可以應(yīng)用到常見的"贊"、同意和反對(duì)評(píng)論、投票系統(tǒng)等場(chǎng)景中,運(yùn)用了Ajax原理實(shí)現(xiàn)的前后端交互功能。do.php中還有個(gè)函數(shù)get_client_ip()用來獲取用戶真實(shí)IP,這個(gè)函數(shù)之前我有文章也發(fā)過,已打包在代碼中歡迎下載,在此不貼出來了。
以上所述就是本文的全部?jī)?nèi)容了,希望大家能夠喜歡。
總結(jié)
以上是生活随笔為你收集整理的php unlike,PHP结合jQuery实现的评论顶、踩功能的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java path util,Java
- 下一篇: matlab双重for训话,Questi