php练习题:投票

  通过连接数据库,对数据库的增删改来实现一个投票的进行与结果的显示:

 

方法一:

主页面

<!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" οnclick="ShowTP()" value="返回" />
        <input type="button" style="float:left;display:block;margin:0px 0px 0px 10px" οnclick="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>
View Code

处理页面,接收投得的票数 并作出处理

<?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");
View Code

建立访问数据库的类,封装用于引用

<?php

class DBDA
{
    public $host = "localhost"; //服务器地址
    public $uid = "root"; //数据库的用户名
    public $pwd = "123"; //数据库的密码
    
    //执行SQL语句,返回相应结果的函数
    //$sql是要执行的SQL语句
    //$type是SQL语句的类型,0代表增删改,1代表查询
    //$db代表要操作的数据库
    public function Query($sql,$type=1,$db="mydb")
    {
        //造连接对象
        $conn = new MySQLi($this->host,$this->uid,$this->pwd,$db);
        
        //判断连接是否成功
        !mysqli_connect_error() or die("连接失败!");
        
        //执行SQL语句
        $result = $conn->query($sql);
        
        //判断SQL语句类型
        if($type==1)
        {
            //如果是查询语句返回结果集的二维数组
            return $result->fetch_all();
        }
        else
        {
            //如果是其他语句,返回true或false
            return $result;
        }
    }
    
}
View Code

 

方法二:

 

转载于:https://www.cnblogs.com/sjxx/p/5331032.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值