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

首頁 > 數(shù)據(jù)庫 > MySQL > 正文

基于PHP+jQuery+MySql實現(xiàn)紅藍(頂踩)投票代碼

2020-03-22 19:45:35
字體:
供稿:網(wǎng)友
查看演示 下載源碼這是一個非常實用的投票實例,應(yīng)用在雙方觀點對抗投票場景。用戶可以選擇支持代表自己觀點的一方進行投票,本文以紅藍雙方投票為例,通過前后臺交互,直觀展示紅藍雙方投票數(shù)和所占比例,應(yīng)用非常廣泛。本文是一篇綜合知識應(yīng)用類文章,需要您具備PHP、jQuery、html' target='_blank'>MySQL以及html和css方面的基本知識。HTML我們需要在頁面中展示紅藍雙方的觀點,以及對應(yīng)的投票數(shù)和比例,以及用于投票交互的手型圖片,本例以#red和#blue分別表示紅藍雙方。.redhand和.bluehand用來做手型投票按鈕,.redbar和.bluebar展示紅藍雙方比例調(diào),#red_num和#blue_num展示雙方投票數(shù)。
div div 您對Helloweba提供的文章的看法? /div div 非常實用 span 完全看不懂 /span /div div id="red" div /div div id="red_bar" span /span p id="red_num" /p /div /div div id="blue" div /div div id="blue_bar" span /span p id="blue_num" /p /div /div /div CSS使用CSS將頁面美化,加載背景圖片,確定相對位置等等,你可以直接復(fù)制以下代碼,在自己的項目中稍作修改即可。
.vote{width:288px; height:220px; margin:60px auto 20px auto;position:relative} .votetitle{width:100%;height:62px; background:url(icon.png) no-repeat 0 30px; font-size:15px} .red{position:absolute; left:0; top:90px; height:80px;} .blue{position:absolute; right:0; top:90px; height:80px;} .votetxt{line-height:24px} .votetxt span{float:right} .redhand{position:absolute; left:0;width:36px; height:36px; background:url(icon.png) no-repeat -1px -38px;cursor:pointer} .bluehand{position:absolute; right:0;width:36px; height:36px; background:url(icon.png) no-repeat -41px -38px;cursor:pointer} .grayhand{width:34px; height:34px; background:url(icon.png) no-repeat -83px -38px;cursor:pointer} .redbar{position:absolute; left:42px; margin-top:8px;} .bluebar{position:absolute; right:42px; margin-top:8px; } .redbar span{display:block; height:6px; background:red; width:100%;border-radius:4px;} .bluebar span{display:block; height:6px; background:#09f; width:100%;border-radius:4px; position:absolute; right:0} .redbar p{line-height:20px; color:red;} .bluebar p{line-height:20px; color:#09f; text-align:right; margin-top:6px} jQuery當(dāng)點擊手型按鈕時,利用jQuery的$.getJSON()向后臺php發(fā)送Ajax請求,如果請求成功,將會得到后臺返回的json數(shù)據(jù),jQuery再將json數(shù)據(jù)進行處理。以下函數(shù):getdata(url,sid),傳遞了兩個參數(shù),url是請求的后臺php地址,sid表示當(dāng)前投票主題ID,我們在該函數(shù)中,返回的json數(shù)據(jù)有紅藍雙方的投票數(shù),以及雙方比例,根據(jù)比例計算比例條的寬度,異步交互展示投票效果。function getdata(url,sid){ $.getJSON(url,{id:sid},function(data){ if(data.success==1){ var w = 208; //定義比例條的總寬度 //紅方投票數(shù) $("#red_num").html(data.red); $("#red").css("width",data.red_percent*100+"%"); var red_bar_w = w*data.red_percent-10; //紅方比例條寬度 $("#red_bar").css("width",red_bar_w); //藍方投票數(shù) $("#blue_num").html(data.blue); $("#blue").css("width",data.blue_percent*100+"%"); var blue_bar_w = w*data.blue_percent; //藍方比例條寬度 $("#blue_bar").css("width",blue_bar_w); }else{ alert(data.msg); }); } 當(dāng)頁面初次加載時,即調(diào)用getdata(),然后點擊給紅方投票或給藍方投票同樣調(diào)用getdata(),只是傳遞的參數(shù)不一樣。注意本例中的參數(shù)sid我們設(shè)置為1,是根據(jù)數(shù)據(jù)表中的id設(shè)定的,開發(fā)者可以根據(jù)實際項目讀取準(zhǔn)確的id。
$(function(){ //獲取初始數(shù)據(jù) getdata("vote.php",1); //紅方投票 $(".redhand").click(function(){ getdata("vote.php action=red",1); }); //藍方投票 $(".bluehand").click(function(){ getdata("vote.php action=blue",1); }); }); PHP前端請求了后臺的vote.php,vote.php將根據(jù)接收的參數(shù),連接數(shù)據(jù)庫,調(diào)用相關(guān)函數(shù)。
include_once("connect.php"); $action = $_GET['action']; $id = intval($_GET['id']); $ip = get_client_ip();//獲取ip if($action=='red'){//紅方投票 vote(1,$id,$ip); }elseif($action=='blue'){//藍方投票 vote(0,$id,$ip); }else{//默認(rèn)返回初始數(shù)據(jù) echo jsons($id); }函數(shù)vote($type,$id,$ip)用來做出投票動作,$type表示投票方,$id表示投票主題的id,$ip表示用戶當(dāng)前ip。首先根據(jù)用戶當(dāng)前IP,查詢投票記錄表votes_ip中是否已經(jīng)存在當(dāng)前ip記錄,如果存在,則說明用戶已投票,否則更新紅方或藍方的投票數(shù),并將當(dāng)前用戶投票記錄寫入到votes_ip表中以防重復(fù)投票。
function vote($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{ $arr['success'] = 0; $arr['msg'] = '已經(jīng)投票過了'; echo json_encode($arr); } 函數(shù)jsons($id)通過查詢當(dāng)前id的投票數(shù),計算比例并返回json數(shù)據(jù)格式供前端調(diào)用。
function jsons($id){ $query = mysql_query("select * from votes where id=".$id); $row = mysql_fetch_array($query); $red = $row['likes']; $blue = $row['unlikes']; $arr['success']=1; $arr['red'] = $red; $arr['blue'] = $blue; $red_percent = round($red/($red+$blue),3); $arr['red_percent'] = $red_percent; $arr['blue_percent'] = 1-$red_percent; return json_encode($arr); } 文中還涉及到獲取用戶真實IP的函數(shù):get_client_ip(),點擊這里可以看相關(guān)代碼:http://www.phpstudy.net/article/58610.htmMySQL最后,貼上Mysql數(shù)據(jù)表,votes表用來記錄紅藍雙方的投票總數(shù),votes_ip表則用來存放用戶的投票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; PHP+MySql+jQuery實現(xiàn)頂和踩投票功能本文結(jié)合實例,講解使用PHP+MySql+jQuery實現(xiàn)的“頂”和“踩”投票功能,通過記錄用戶IP,判斷用戶的投票行為是否有效,該實例也可以擴展到投票系統(tǒng)中。首先我們在頁面上放置“頂”和“踩”的按鈕,即#dig_up和#dig_down,按鈕上分別記錄了投票的票數(shù)以及所占的百分比。 div div id="dig_up" span id="num_up" /span p 很好,很強大! /p div id="bar_up" span /span i /i /div /div div id="dig_down" span id="num_down" /span p 太差勁了! /p div id="bar_down" span /span i /i /div /div div id="msg" /div /div $(function(){ //當(dāng)鼠標(biāo)懸浮和離開兩個按鈕時,切換按鈕背景樣式 $("#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("ajax.php",1); //單擊“頂”時 $("#dig_up").click(function(){ getdata("ajax.php action=like",1); }); //單擊“踩”時 $("#dig_down").click(function(){ getdata("ajax.php action=unlike",1); }); });以上內(nèi)容實現(xiàn)了基于PHP+jQuery+MySql實現(xiàn)紅藍(頂踩)投票代碼,希望大家喜歡。PHP教程

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

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 盈江县| 平安县| 丰镇市| 二连浩特市| 监利县| 松桃| 祁连县| 东明县| 淄博市| 府谷县| 河南省| 咸丰县| 屏南县| 贞丰县| 凌海市| 江都市| 饶河县| 宁武县| 临夏市| 太保市| 淮滨县| 邯郸市| 乃东县| 穆棱市| 江北区| 鸡泽县| 濉溪县| 桃江县| 台前县| 谢通门县| 荆门市| 孟津县| 樟树市| 漯河市| 永春县| 邢台市| 凉城县| 江源县| 莱芜市| 湛江市| 济南市|