zoukankan      html  css  js  c++  java
  • 数据的查询练习——根据某一问题,进行投票

    第一步:先构建“从数据库中查询方法”的类

    <?php
    class DBDA
    {
    	public $host="localhost";  //数据库地址
    	public $uid = "root";  //数据库用户名
    	public $pwd = "";   //密码
    	
    	//执行sql语句,返回相应的结果
    	//参数:$sql代表执行的sql语句;$type是sql语句类型0代表查询,1代表其他;$db代表操作的数据库
    	public function Query($sql,$type=0,$db="mydb")
    	{
    		//1.连接数据库
    		$dbconnect=new MySQLi($this->host,$this->uid,$this->pwd,$db);
    		//2.判断是否出错
    		!mysqli_connect_error() or die("连接失败!");
    		//3.执行sql语句
    		$result=$dbconnect->query($sql);
    		
    		if($type==0)
    		{
    			return $result->fetch_all();
    		}
    		else
    		{
    			return $result;
    		}
    	} 	
    }
    

      

    第二步:投票页面显示部分:

    <body>
    <form action="chuli.php" method="post">
    <?php
    
    include("DBDA.class.php");
    $db=new DBDA();
    
    $sql="select * from diaoyantimu limit 0,1";
    
    $attr=$db->Query($sql);
    $tmmc=$attr[0][1];
    $tmdh=$attr[0][0];
    
    echo "<div>题目名称:{$tmmc}</div>";
    
    $sql2="select * from diaoyanxuanxiang where timudaihao='{$tmdh}'";
    
    $attr2=$db->Query($sql2);
    
    echo "<div id='list'>";
    foreach($attr2 as $v)
    {
    	echo "<div>
    	      <input type='checkbox' name='xx[]' value='{$v[0]}'/><span>{$v[1]}</span>
    	      </div>";
    }
    
    ?>
          <input type="submit" value="提交"/>
    	  <input type="button" value="查看" id="check" onclick="show()"/>
    </div>
    </form>
    

      

    第三部分:处理问题后台运行程序

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

      

    第四部分:结果显示

    <div id="jieguo" style="display:none">
    <div>结果显示:</div>
    <?php
    $sqlsum="select sum(number) from diaoyanxuanxiang where timudaihao='{$tmdh}'";
    $attrsum=$db->Query($sqlsum);
    
      foreach($attr2 as $v)
      {
    	  $bfb=($v[2]/$attrsum[0][0])*100;
    	  $bfb=round($bfb,2);     //取小数点后2位
    	  
    	  echo "<div class='a'>
    	          <div id='x'>{$v[0]}.{$v[1]}</div>
    			  <div class='a1'id='x'>
    			     <div class='a2' style='{$bfb}%'></div>
    			  </div>
    			  <div id='x'>{$v[2]}人({$bfb}%)</div>
    	        </div>";
      }
    ?>
         
    <input type="button" value="返回" id="fanhui" onclick="fanhui()"/>
    </div>
    
    <script type="text/javascript">
    function show()
    {
    	document.getElementById("list").style.display="none";
    	document.getElementById("jieguo").style.display="block";
    }
    function fanhui()
    {
    	document.getElementById("list").style.display="block";
    	document.getElementById("jieguo").style.display="none";
    }
    
    </script>
    </body>
    </html>
    

      

  • 相关阅读:
    数据结构基础
    基于TCP的通信 客户端
    hduacm 5255
    uva 10668
    hduacm 5104
    uva 10491
    Hibernate之性能优化
    Hibernate基础知识
    Hibernate入门
    Struts2之Crud综合实例
  • 原文地址:https://www.cnblogs.com/zst062102/p/5471503.html
Copyright © 2011-2022 走看看