国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 數據庫 > MySQL > 正文

PHP+MySql+jQuery實現的-頂-和-踩-投票功能

2020-03-22 18:23:38
字體:
來源:轉載
供稿:網友
本文實例為大家分享了基于PHP+jQuery+MySql實現紅藍(頂踩)投票代碼,供大家參考,具體內容如下數據庫操作: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;
.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} html' target='_blank'>jquery:$(function(){ //鼠標滑向和離開投票按鈕時,變換背景樣式 $("#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"); }); //初始化數據 getdata("do.php",1); //單擊“頂”時 $("#dig_up").click(function(){ getdata("do.php action=like",1); }); //單擊“踩”時 $("#dig_down").click(function(){ getdata("do.php action=unlike",1); });
//通過控制寬度來顯示百分比進度條效果 $("#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");//連接數據庫 $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){//還沒有頂過 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'] = '操作失敗,請重試'; echo json_encode($arr); }else{ $msg = $type==1 '您已經頂過了':'您已經踩過了'; $arr['success'] = 0; $arr['msg'] = $msg; echo json_encode($arr);
$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); 以上就是本文的全部內容,希望對大家學習php程序設計有所幫助。PHP教程

鄭重聲明:本文版權歸原作者所有,轉載文章僅為傳播更多信息之目的,如作者信息標記有誤,請第一時間聯系我們修改或刪除,多謝。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 梓潼县| 凌源市| 神木县| 廉江市| 台北县| 永胜县| 乌鲁木齐市| 房山区| 华阴市| 光泽县| 田东县| 台东县| 曲沃县| 威远县| 贵德县| 阿克苏市| 陇南市| 西乡县| 桐梓县| 正安县| 莒南县| 东丰县| 海城市| 潞西市| 灌云县| 皮山县| 舒兰市| 石台县| 吴川市| 揭阳市| 龙陵县| 镇坪县| 攀枝花市| 荔浦县| 会泽县| 湘潭县| 高平市| 淮阳县| 镶黄旗| 武陟县| 瑞丽市|