zoukankan      html  css  js  c++  java
  • 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;100px; height:4px; border:1px solid #000'>
                <div style='{$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; 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>
    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

    方法二:

  • 相关阅读:
    SpringMVC请求静态资源
    Spring视图和视图解析器
    @ModelAttribute运行流程
    SpringMVC模型数据处理
    SpringMVC简单映射请求参数介绍
    队列和栈的问题
    非比较排序——计数排序、基数排序、桶排序
    递归
    对数器的使用
    常见的比较排序
  • 原文地址:https://www.cnblogs.com/sjxx/p/5331032.html
Copyright © 2011-2022 走看看