PHP+MySql+jQuery實(shí)現(xiàn)的"頂"和"踩"投票功能
本文實(shí)例為大家分享了基于PHP+jQuery+MySql實(shí)現(xiàn)紅藍(lán)(頂踩)投票代碼,供大家參考,具體內(nèi)容如下
數(shù)據(jù)庫(kù)操作:
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;
div:
<div class="digg"> <div id="dig_up" class="digup"> <span id="num_up"></span> <p>很好,很強(qiáng)大!</p> <div id="bar_up" class="bar"><span></span><i></i></div> </div> <div id="dig_down" class="digdown"> <span id="num_down"></span> <p>太差勁了!</p> <div id="bar_down" class="bar"><span></span><i></i></div> </div> <div id="msg"></div> </div>
css:
.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:
$(function(){ //鼠標(biāo)滑向和離開(kāi)投票按鈕時(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); }); }); --------------------------------------- function getdata(url,sid){ $.getJSON(url,{id:sid},function(data){ if(data.success==1){//投票成功 $("#num_up").html(data.like); //通過(guò)控制寬度來(lái)顯示百分比進(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:
include_once("connect.php");//連接數(shù)據(jù)庫(kù) $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); } ------------------------------------ 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){//還沒(méi)有頂過(guò) 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)頂過(guò)了':'您已經(jīng)踩過(guò)了'; $arr['success'] = 0; $arr['msg'] = $msg; echo json_encode($arr); } } -----------php------------------------- 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); }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家學(xué)習(xí)php程序設(shè)計(jì)有所幫助。
- 基于jQuery實(shí)現(xiàn)仿搜狐辯論投票動(dòng)畫(huà)代碼(附源碼下載)
- jquery彩色投票進(jìn)度條簡(jiǎn)單實(shí)例演示
- 基于PHP+jQuery+MySql實(shí)現(xiàn)紅藍(lán)(頂踩)投票代碼
- jQuery基于ajax實(shí)現(xiàn)帶動(dòng)畫(huà)效果無(wú)刷新柱狀圖投票代碼
- PHP結(jié)合jQuery實(shí)現(xiàn)紅藍(lán)投票功能特效
- 基于jquery的多彩百分比 動(dòng)態(tài)進(jìn)度條 投票效果顯示效果實(shí)現(xiàn)代碼
- 基于jQuery的投票系統(tǒng)顯示結(jié)果插件
- 基于Jquery與WebMethod投票功能實(shí)現(xiàn)代碼
- 基于jquery實(shí)現(xiàn)彩色投票進(jìn)度條代碼解析
相關(guān)文章
一個(gè)PHP驗(yàn)證碼類(lèi)代碼分享(已封裝成類(lèi))
驗(yàn)證碼的用途就不用多說(shuō)了,之前也寫(xiě)了一篇關(guān)于PHP驗(yàn)證碼的文章,PHP 驗(yàn)證碼的實(shí)現(xiàn)。但是沒(méi)有封裝成類(lèi)。下面就介紹一個(gè)PHP的一個(gè)驗(yàn)證碼類(lèi)。2011-07-07PHP使用preg_split()分割特殊字符(元字符等)的方法分析
這篇文章主要介紹了PHP使用preg_split()分割特殊字符(元字符等)的方法,結(jié)合具體實(shí)例形式分析了php正則分割的操作技巧與注意事項(xiàng),需要的朋友可以參考下2017-02-02php中array_multisort對(duì)多維數(shù)組排序的方法
這篇文章主要介紹了php中array_multisort對(duì)多維數(shù)組排序的方法,涉及php遍歷數(shù)組及使用array_multisort進(jìn)行數(shù)組排序的相關(guān)技巧,需要的朋友可以參考下2015-07-07PHP排序算法之冒泡排序(Bubble Sort)實(shí)現(xiàn)方法詳解
這篇文章主要介紹了PHP排序算法之冒泡排序(Bubble Sort)實(shí)現(xiàn)方法,參照大話數(shù)據(jù)結(jié)構(gòu)中的算法,結(jié)合實(shí)例形式較為詳細(xì)的分析了冒泡排序的原理與相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2018-04-04用PHP實(shí)現(xiàn)Ftp用戶的在線管理的代碼
用PHP實(shí)現(xiàn)Ftp用戶的在線管理的代碼...2007-03-03詳解phpmyadmin相關(guān)配置與錯(cuò)誤解決
這篇文章主要介紹了詳解phpmyadmin相關(guān)配置與錯(cuò)誤解決的相關(guān)資料,希望通過(guò)本文能幫助到大家,讓大家遇到這種問(wèn)題可以解決,需要的朋友可以參考下2017-10-10