• php投票练习


    一、题目要求:建立如下两个数据库,实现投票和%的统计结果:

    、具体编写方法:

    (1)、建立数据库:

    里面的蓝色背景的表格名称就是我们所需的表格!

    表格内容如下:

    表名:diaoyantimu

    表名:diaoyanxuanxiang

    (2)、封装类文件:

    <?php
    class DBDA
    {
        public $fuwuqi="localhost";
        public $yonghuming="root";
        public $mima="root";
        
        public $dbconnect;
        
        function Query($sql,$type=1,$shujukuming="test")
        {
           
            $this->dbconnect = new MySQLi($this->fuwuqi,$this->yonghuming,$this->mima,$shujukuming);
            
            
            if(!mysqli_connect_error())
            {
               
                $result = $this->dbconnect->query($sql);
                
               
                if($type==1)
                {
                  
                   return $result->fetch_all();    
                }
                else
                {
                    
                   return $result;    
                }
            
            }
            else
            {
                return"连接失败";
                
            }
            
            
        }
        
    }
    
    ?>

    (3)、投票页面(shouye.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">
    .x
    {
        float:left;
    }
    #top
    {
        width:500px;
        height:200px;
    }
    #bottom
    {
        width:500px;
        height:200px;
        display:none;
    }
    #fanhui
    {
        display:none;
    }
    </style>
    
    
    
    </head>
    <body>
    <form action="chuli.php" method="post">
    <?php
    include("DBDA.class.php");
    $db=new DBDA;
    $stm="select * from diaoyantimu limit 0,1";
    $atm = $db->Query($stm);
    
    echo "<div>题目名称:{$atm[0][1]}</div>";
    $sxx="select * from diaoyanxuanxiang where timudaihao='{$atm[0][0]}'";
    $axx=$db->Query($sxx);
    
    echo "<div id='top'>";
    
    foreach($axx as $v)
    {
    echo "<div><input type='checkbox' value='{$v[0]}' name='xx[]'/>{$v[1]}</div>";    
    }
    
    echo "</div>";
    
    ?>
    
    <div id="bottom">
    <?php
    
    $sum="select sum(Numbers) from diaoyanxuanxiang where timudaihao='{$atm[0][0]}'";
    $asum=$db->Query($sum);
    $total=$asum[0][0];
    
    
    foreach($axx as $v)
    {
        $bfb=($v[2]/$total)*100;
        
      echo "<div><span class='x'>{$v[1]}</span>
      
      <div class='x' style='border:1px solid blue;120px; height:12px'>
      
     <div style='background-color:red;height:12px;{$bfb}%'></div>
      
      </div>
    
      
      <span class='x'>&nbsp;{$v[2]}&nbsp;</span>
        <span class='x'>{$bfb}%</span>
      </div>
      <div style='clear:both'></div>";
    }
    ?>
    
    </div>
     
    <div id="anniu">
    <input type="submit" value="提交"/>
    <input type="button" value="查看" onclick="ShowResult()"/>
    </div>
    <div id="fanhui"><input type="button" value="返回" onclick="Show()" /></div>
    
    </form>
    
    </body>
    
    <script type="text/javascript">
    function ShowResult()
    {
        document.getElementById("top").style.display="none";
        document.getElementById("bottom").style.display="block";
        document.getElementById("fanhui").style.display="block";
        document.getElementById("anniu").style.display="none";
    }
    function Show()
    {
        document.getElementById("top").style.display="block";
        document.getElementById("bottom").style.display="none";
        document.getElementById("fanhui").style.display="none";
        document.getElementById("anniu").style.display="block";
    }
    
    </script>
    
    </html>

    (4)、投票处理页面(chuli.php):

    <?php
    $ids = $_POST["xx"];
    include("DBDA.class.php");
    $db = new DBDA();
    
    foreach($ids as $v)
    {
        $sql = "update diaoyanxuanxiang set Numbers = Numbers+1 where Ids='{$v}'";
        $db->Query($sql,0);
    }
    
    header("location:shouye.php");

    显示结果如下:

  • 相关阅读:
    以后面试官再问你三次握手和四次挥手,直接把这一篇文章丢给他
    聊聊面试中常问的GC机制
    四面快手、终拿Offer,想告诉你的一些事情
    深入浅出14个Java并发容器
    Dubbo 在 K8s 下的思考
    一文带你深入浅出Spring 事务原理
    如何高效选择一款消息队列?
    当面试官要你介绍一下MQ时,该怎么回答?
    淘宝双11促销背后采用什么架构技术来实现网站的负载均衡
    Android 更改按钮样式 Button Styles
  • 原文地址:https://www.cnblogs.com/axj1993/p/6478192.html
Copyright © 2020-2023  润新知