练习------投票
一 、题目要求:
二 、做法
【1】建立数据库
表名:diaoyantimu
表名: diaoyanxuanxiang
【2】封装类文件
1 <?php 2 class DBDA 3 { 4 public $fuwuqi="localhost"; //服务器地址 5 public $yonghuming="root";//用户名 6 public $mima="";//密码 7 8 public $dbconnect;//连接对象 9 10 //操作数据库的方法 11 12 //$sql代表需要执行的SQL语句 13 //$type代表SQL语句的类型,1代表查询,2代表增删改 14 //$shujukuming代表数据库的名称 15 //如果是查询,返回二维数组 16 //如果是增删改,返回true或false 17 18 function Query($sql,$type=1,$shujukuming="toupiao") 19 { 20 //造连接对象 21 $this->dbconnect = new MySQLi($this->fuwuqi,$this->yonghuming,$this->mima,$shujukuming); 22 23 //判断是否出错 24 if(!mysqli_connect_error()) 25 { 26 //如果连接成功,执行SQL语句 27 $result = $this->dbconnect->query($sql); 28 29 //根据语句类型判断 30 if($type==1) 31 { 32 //如果是查询语句,返回二维数组 33 return $result->fetch_all(); 34 } 35 else 36 { 37 //如果是其他语句,返回true或false 38 return $result; 39 } 40 41 } 42 else 43 { 44 return"连接失败"; 45 46 } 47 48 49 } 50 51 } 52 53 54 55 56 ?>
【3】首页
1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 2 <html xmlns="http://www.w3.org/1999/xhtml"> 3 <head> 4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 5 <title>无标题文档</title> 6 7 <style type="text/css"> 8 .x 9 { 10 float:left; 11 } 12 #top 13 { 14 500px; 15 height:200px; 16 } 17 #bottom 18 { 19 500px; 20 height:200px; 21 display:none; 22 } 23 #fanhui 24 { 25 display:none; 26 } 27 </style> 28 29 30 31 </head> 32 <body> 33 <form action="chuli.php" method="post"> 34 <?php 35 include("DBDA.class.php"); 36 $db=new DBDA; 37 $stm="select * from diaoyantimu limit 0,1";//取第一条数据 38 $atm = $db->Query($stm); 39 40 echo "<div>题目名称:{$atm[0][1]}</div>";//显示题目 41 $sxx="select * from diaoyanxuanxiang where timudaihao='{$atm[0][0]}'"; 42 $axx=$db->Query($sxx); 43 44 echo "<div id='top'>"; 45 46 foreach($axx as $v)//$v代表的是一维数组 47 { 48 echo "<div><input type='checkbox' value='{$v[0]}' name='xx[]'/>{$v[1]}</div>"; 49 } 50 51 echo "</div>"; 52 53 ?> 54 55 <div id="bottom"> 56 <?php 57 58 $sum="select sum(Numbers) from diaoyanxuanxiang where timudaihao='{$atm[0][0]}'"; 59 $asum=$db->Query($sum); 60 $total=$asum[0][0]; 61 62 63 foreach($axx as $v) 64 { 65 $bfb=($v[2]/$total)*100;//$total 提取的是数据库里的总值 所以数据库要有数Numbers 66 67 echo "<div><span class='x'>{$v[1]}</span> 68 69 <div class='x' style='border:1px solid blue;120px; height:12px'> 70 71 <div style='background-color:red;height:12px;{$bfb}%'></div> 72 73 </div> 74 75 76 <span class='x'> {$v[2]} </span> 77 <span class='x'>{$bfb}%</span> 78 </div> 79 <div style='clear:both'></div>";//div的换行 80 81 } 82 ?> 83 84 </div> 85 86 <div id="anniu"> 87 <input type="submit" value="提交"/> 88 <input type="button" value="查看" onclick="ShowResult()"/> 89 </div> 90 <div id="fanhui"><input type="button" value="返回" onclick="Show()" /></div> 91 92 </form> 93 94 </body> 95 96 <script type="text/javascript"> 97 function ShowResult() 98 { 99 document.getElementById("top").style.display="none"; 100 document.getElementById("bottom").style.display="block"; 101 document.getElementById("fanhui").style.display="block"; 102 document.getElementById("anniu").style.display="none"; 103 } 104 function Show() 105 { 106 document.getElementById("top").style.display="block"; 107 document.getElementById("bottom").style.display="none"; 108 document.getElementById("fanhui").style.display="none"; 109 document.getElementById("anniu").style.display="block"; 110 } 111 112 </script> 113 114 </html>
【4】处理页面--------chuli.php
1 <?php 2 $ids = $_POST["xx"]; //要接收传过来的值 谁传的? name='xx[] 3 include("DBDA.class.php"); 4 $db = new DBDA(); 5 6 foreach($ids as $v)//$v就是主键值 7 { 8 $sql = "update diaoyanxuanxiang set Numbers = Numbers+1 where Ids='{$v}'"; 9 $db->Query($sql,0);//不是查询语句,所以是0 10 } 11 12 header("location:shouye.php");
点查看