练习:投票
主页面
1 <?php 2 include("./dbda.class.php"); 3 $db = new dbda(); 4 $sql = "select * from diaoyantimu limit 0,1"; 5 $attr = $db->Query($sql); 6 echo "<div>{$attr[0][1]}</div>"; 7 8 ?> 9 <div id="tp"> 10 <form action="TiJiao.php" method="post"> 11 <div> 12 <?php 13 $sql1 = "select * from diaoyanxuanxiang"; 14 $att = $db->Query($sql1); 15 foreach($att as $v) 16 { 17 echo"<input type='checkbox' value='{$v[0]}' name='xx[]'/>{$v[1]}<br />"; 18 } 19 20 ?> 21 </div> 22 <div><input type="submit" value="提交" /> <input type="button" value="查看结果" onclick="show()" /></div> 23 </form> 24 </div> 25 26 <div style="display:none;" id="jieguo"> 27 <?php 28 $sql2 = "select sum(Numbers) from diaoyanxuanxiang where TiMuDaiHao='{$attr[0][0]}'"; 29 $sumnumbers = $db->Query($sql2); 30 foreach($att as $v1) 31 { 32 $bfb = ($v1[2]/$sumnumbers[0][0])*100; 33 $bfb = round($bfb,2); 34 echo "<div class='ff'>$v1[1] </div> 35 <div class='ff'>{$v1[2]} ({$bfb}%)</div> 36 <div class='ff' style=' width:100px; height:10px; border:1px solid red'><div style='height:10px; width:{$bfb}%; background-color:red'></div></div> <br />"; 37 } 38 ?> 39 <input type="button" value="返回" onclick="showfh()" /> 40 </div> 41
JS页面:控制页面隐藏与显示
1 <script type="text/javascript"> 2 function show() 3 { 4 document.getElementById("tp").style.display="none"; 5 document.getElementById("jieguo").style.display="block"; 6 } 7 function showfh() 8 { 9 document.getElementById("tp").style.display="block"; 10 document.getElementById("jieguo").style.display="none"; 11 } 12 13 </script>
封装数据库类
1 <?php 2 class dbda 3 { 4 public $host = "localhost"; //数据库地址 5 public $uid = "root"; //数据库用户名 6 public $pwd = ""; //数据库密码 7 //执行sql语句 8 //参数:sql代表要执行的sql语句 type是sql语句的类型0代表查询1代表其他 db代表要操作的数据库 9 public function Query($sql,$type=0,$db="mydb") 10 { 11 //1.造连接对象 12 $dbconnect = new MySQLi($this->host,$this->uid,$this->pwd,$db); 13 !mysqli_connect_error() or die("连接失败!"); 14 $result = $dbconnect->query($sql); 15 if($type ==0) 16 { 17 return $result->fetch_all(); 18 } 19 else 20 { 21 return $result; 22 } 23 } 24 }