久久久久久AV无码免费看大片,亚洲一区精品人人爽人人躁,国产成人片无码免费爱线观看,亚洲AV成人无码精品网站,为什么晚上搞的时候要盖被子

用PHP+JQuery+MySql實(shí)現(xiàn)投票功能

時(shí)間:2016-12-29 16:21:34 類型:PHP
字號(hào):    

      我們?cè)诖髮W(xué)的時(shí)候接觸過(guò)辯論賽,有“正方”和“反方”代表隊(duì),他們的觀點(diǎn)往往是爭(zhēng)鋒相對(duì)的,而今天要給大家介紹的一個(gè)具體的實(shí)戰(zhàn)案例就是關(guān)于雙方觀點(diǎn)對(duì)抗的一個(gè)投票場(chǎng)景功能的實(shí)現(xiàn),通過(guò)前后臺(tái)的數(shù)據(jù)交互,直觀展示雙方投票數(shù)以及所占比例,這個(gè)在很多地方都運(yùn)用非常廣泛,所以很有必要來(lái)學(xué)習(xí)一下。

要實(shí)現(xiàn)這種功能,主要需要運(yùn)用到的知識(shí)有:HTML+CSS,Javascript+Jquery、PHP技術(shù)等,下面一一給大家介紹


HTML

我們需要在頁(yè)面中展示紅藍(lán)雙方的觀點(diǎn),以及對(duì)應(yīng)的投票數(shù)和比例,以及用于投票交互的手型圖片,本例以#red和#blue分別表示紅藍(lán)雙方。.redhand和.bluehand用來(lái)做手型投票按鈕,.redbar和.bluebar展示紅藍(lán)雙方比例調(diào),#red_num和#blue_num展示雙方投票數(shù)。

<div class="vote"> 
    <div class="votetitle">您對(duì)Helloweba提供的文章的看法?</div> 
    <div class="votetxt">非常實(shí)用<span>完全看不懂</span></div> 
    <div class="red" id="red"> 
        <div class="redhand"></div> 
        <div class="redbar" id="red_bar"> 
            <span></span> 
            <p id="red_num"></p> 
        </div> 
    </div> 
    <div class="blue" id="blue"> 
        <div class="bluehand"></div> 
        <div class="bluebar" id="blue_bar"> 
            <span></span> 
            <p id="blue_num"></p> 
        </div> 
    </div>  </div> 
CSS


使用CSS將頁(yè)面美化,加載背景圖片,確定相對(duì)位置等,你可以直接復(fù)制以下代碼,在自己的項(xiàng)目中稍作修改即可。



.vote{width:288pxheight:220pxmargin:60px auto 20px auto;position:relative
.votetitle{width:100%;height:62pxbackground:url(icon.png) no-repeat 0 30pxfont-size:15px
.red{position:absoluteleft:0top:90pxheight:80px;}  
.blue{position:absoluteright:0top:90pxheight:80px;} 
.votetxt{line-height:24px.votetxt span{float:right
.redhand{position:absoluteleft:0;width:36pxheight:36pxbackground:url(icon.png) no-repeat -1px -38px;cursor:pointer
.bluehand{position:absoluteright:0;width:36pxheight:36pxbackground:url(icon.png) no-repeat -41px -38px;cursor:pointer
.grayhand{width:34pxheight:34pxbackground:url(icon.png) no-repeat -83px -38px;cursor:pointer
.redbar{position:absoluteleft:42pxmargin-top:8px;}  
.bluebar{position:absoluteright:42pxmargin-top:8px; }  
.redbar span{display:blockheight:6pxbackground:redwidth:100%;border-radius:4px;}  
.bluebar span{display:blockheight:6pxbackground:#09fwidth:100%;border-radius:4pxposition:absoluteright:0
.redbar p{line-height:20pxcolor:red;}  
.bluebar p{line-height:20pxcolor:#09ftext-align:rightmargin-top:6px
JQuery
當(dāng)點(diǎn)擊手型按鈕時(shí),利用jQuery的$.getJSON()向后臺(tái)php發(fā)送Ajax請(qǐng)求,如果請(qǐng)求成功,將會(huì)得到后臺(tái)返回的json數(shù)據(jù),jQuery再將json數(shù)據(jù)進(jìn)行處理。以下函數(shù):getdata(url,sid),傳遞了兩個(gè)參數(shù),url是請(qǐng)求的后臺(tái)php地址,sid表示當(dāng)前投票主題ID,我們?cè)谠摵瘮?shù)中,返回的json數(shù)據(jù)有紅藍(lán)雙方的投票數(shù),以及雙方比例,根據(jù)比例計(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); 
            //藍(lán)方投票數(shù) 
            $("#blue_num").html(data.blue); 
            $("#blue").css("width",data.blue_percent*100+"%"); 
            var blue_bar_w = w*data.blue_percent; 
            //藍(lán)方比例條寬度 
            $("#blue_bar").css("width",blue_bar_w); 
        }else{ 
            alert(data.msg); 
        } 
    });  } 
當(dāng)頁(yè)面初次加載時(shí),即調(diào)用getdata(),然后點(diǎn)擊給紅方投票或給藍(lán)方投票同樣調(diào)用getdata(),只是傳遞的參數(shù)不一樣。注意本例中的參數(shù)sid我們?cè)O(shè)置為1,是根據(jù)數(shù)據(jù)表中的id設(shè)定的,開(kāi)發(fā)者可以根據(jù)實(shí)際項(xiàng)目讀取準(zhǔn)確的id。

$(function(){ 
    //獲取初始數(shù)據(jù) 
    getdata("vote.php",1); 
    //紅方投票 
    $(".redhand").click(function(){ 
        getdata("vote.php?action=red",1); 
    }); 
    //藍(lán)方投票 
    $(".bluehand").click(function(){ 
        getdata("vote.php?action=blue",1); 
    });  }); 
PHP
前端請(qǐng)求了后臺(tái)的vote.php,vote.php將根據(jù)接收的參數(shù),連接數(shù)據(jù)庫(kù),調(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'){//藍(lán)方投票 
    vote(0,$id,$ip); 
}else{//默認(rèn)返回初始數(shù)據(jù) 
    echo jsons($id); 
} 
函數(shù)vote($type,$id,$ip)用來(lái)做出投票動(dòng)作,$type表示投票方,$id表示投票主題的id,$ip表示用戶當(dāng)前ip。首先根據(jù)用戶當(dāng)前IP,查詢投票記錄表votes_ip中是否已經(jīng)存在當(dāng)前ip記錄,如果存在,則說(shuō)明用戶已投票,否則更新紅方或藍(lán)方的投票數(shù),并將當(dāng)前用戶投票記錄寫(xiě)入到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){//還沒(méi)有投票 
        if($type==1){//紅方 
            $sql = "update votes set likes=likes+1 where id=".$id; 
        }else{//藍(lán)方 
            $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{ 
        $arr['success'] = 0; 
        $arr['msg'] = '已經(jīng)投票過(guò)了'; 
        echo json_encode($arr); 
    } 
} 
函數(shù)jsons($id)通過(guò)查詢當(dāng)前id的投票數(shù),計(jì)算比例并返回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); 
} 
MYSQL
最后,貼上Mysql數(shù)據(jù)表,votes表用來(lái)記錄紅藍(lán)雙方的投票總數(shù),votes_ip表則用來(lái)存放用戶的投票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