PHP语言 -- 投票练习题
Test.php
<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> <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='titile'>{$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";
}
//题目选项 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);
//查所有选项
//$resultx 里已经查出
for($j=0;$j<count($resultx);$j++)
{
$rs = $result[$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>";
echo "<span>{$resultx}</span>"
echo "</div>";
echo "</div>";
?> <div style="margin-left:20px">
<input id="tj" type="submit" value="提交" style="float:left;<?php echo $xian ?>;margin:0px 0px 0px 10px" />
<input id="fh" type ="button" style="float:left;<?php echo $xianshi ?>display:none;margin:0px 0px 0px 10px" onclick="ShowTP()" value="返回"/>
<input type="button" onclick="ShowJieGuo()" value="显示结果" style="float:left;display:block;margin:0px 0px 0px 10px" />
</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 ShowJieGuo()
{
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>
ChuLi.php
<?php include("DBDA.php"); $db = new DBDA(); $attr = $_POST["opt"]; for($i=0;$i<count($attr);$i++) { $sql = "update DiaoYanXuanXiang set Nambers = Numbers+1 where Ids={$attr[$i]}"; $db->Query($sql,0); } header("location:Test.php?bs=1")