实现投票,显示人数百分比的功能

Posted 年少不上班

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了实现投票,显示人数百分比的功能相关的知识,希望对你有一定的参考价值。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<style type="text/css">
*{
    margin:0px auto;
    padding:0px;
    }
.title
{
    height:50px;
    margin:20px 0px 0px 20px;
}
.list
{
    width:300px;
    height:200px;
    margin-left:20px;
}
.xx
{
    width:300px;
    height:30px;
}
.jieguo
{
    width:300px;
    height:200px;
    margin-left:20px;
}
.xxnr
{
    width:300px;
    height:30px;
}
</style>
</head>

<body>
    <form action="ChuLi.php" method="post">
        
    <?php
        include("DBDA.php");
        $db = new DBDA();
        
        $sql = "select * from DiaoYanTiMu";
        
        $result = $db->Query($sql);
        
        //题目标题
        echo "<div class=‘title‘>{$result[0][1]}</div>";
        
        $code = $result[0][0];
        
        $sqlx = "select * from DiaoYanXuanXiang where TiMuDaiHao = {$code}";
        $resultx = $db->Query($sqlx);
        
        $xian = "";
        if(@$_GET["bs"]==1)
        {
            $xian = "display:none";
        }
        else
        {
            $xian="display:block";
        }
        
        //题目选项的DIV
        echo "<div class=‘list‘ style=‘{$xian}‘>";
        
        for($i=0;$i<count($resultx);$i++)
        {
            echo "<div class=‘xx‘>";
            echo "<input type=‘checkbox‘ value=‘{$resultx[$i][0]}‘ name=‘opt[]‘ />";
            echo "<span>{$resultx[$i][1]}</span>";
            echo "</div>";
        }

        echo "</div>";
        
        //下面是投票结果的DIV
        $xianshi = "";
        if(@$_GET["bs"]==1)
        {
            $xianshi = "display:block";
        }
        else
        {
            $xianshi="display:none";
        }
        echo "<div class=‘jieguo‘ style=‘{$xianshi}‘>";
        
        //求总人数
        $sqlcount = "select sum(Numbers) from DiaoYanXuanXiang";
        $attrcount = $db->Query($sqlcount);
        
        for($j=0;$j<count($resultx);$j++)
        {
            $rs = $resultx[$j][2];
            if($attrcount[0][0]==0)
            {
                $bfb = 0;
            }
            else
            {
                $bfb = ($rs/$attrcount[0][0])*100;
            }
            
            echo "<div class=‘xxnr‘>";
        
            echo "<span style=‘float:left‘>{$resultx[$j][1]}</span>";
            echo "<div style=‘float:left;margin:10px 0px 0px 10px;width:100px; height:4px; border:1px solid #000‘>
            <div style=‘width:{$bfb}%; height:4px;margin-left:0px; background-color:#666‘></div>
            </div>";
            echo "<span style=‘float:left; margin-left:10px‘>{$resultx[$j][2]}</span>";
            
            echo "</div>";
        }
        

        
        
        echo "</div>";
        
    ?>
        
    <div style="margin-left:20px; width:200px">
        <input id="tj" type="submit" style="float:left;<?php echo $xian; ?>;margin:0px 0px 0px 10px" value="提交" />
        <input id="fh" type="button" style="float:left;<?php echo $xianshi;?>;margin:0px 0px 0px 10px" onclick="ShowTP()" value="返回" />
        <input type="button" style="float:left;display:block;margin:0px 0px 0px 10px" onclick="ShowJieGuo()" value="显示结果" />
    </div>
    </form>
</body>

<script type="text/javascript">
function ShowJieGuo()
{
    var list = document.getElementsByClassName("list");
    var jieguo = document.getElementsByClassName("jieguo");
    var tj = document.getElementById("tj");
    var fh = document.getElementById("fh");
    
    fh.style.display="block";
    tj.style.display="none";
    list[0].style.display="none";
    jieguo[0].style.display ="block";
}
function ShowTP()
{
    var list = document.getElementsByClassName("list");
    var jieguo = document.getElementsByClassName("jieguo");
    var tj = document.getElementById("tj");
    var fh = document.getElementById("fh");
    
    fh.style.display="none";
    tj.style.display="block";
    list[0].style.display="block";
    jieguo[0].style.display ="none";
}
</script>
</html>

 

 

Chuli.php

<?php

include("DBDA.php");
$db = new DBDA();

$attr = $_POST["opt"];



for($i=0;$i<count($attr);$i++)
{    
    $sql = "update DiaoYanXuanXiang set Numbers = Numbers+1 where Ids = {$attr[$i]}";
    
    $db->Query($sql,0);
}

header("location:Test.php?bs=1");

 

以上是关于实现投票,显示人数百分比的功能的主要内容,如果未能解决你的问题,请参考以下文章

PHP+Mysql实现网站顶和踩投票功能实例

C语言实例第05期,实现投票统计功能

Oracle里SQL语句的百分比运算

php结合mysql和jquery做简单的投票小程序

投票系统例题

计算我的查询中的人数