5月8 调研投票练习题
要求操作数据库:因此自己要先创建数据库,数据库的类型及其要求如下图所示:
需要用之前封装好的类:在test0506下DBDA.class.php
<?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")//经常操作的数据库名称 { //造连接对象 $dbconnect = new MySQLi($this->host,$this->uid,$this->pwd,$db); //判断连接是否有误 !mysqli_connect_error() or die("连接失败"); //执行SQL语句 $result = $dbconnect->query($sql); if($type==0)//查询时肯定是返回数组的 { return $result->fetch_all(); } else//增删改时只看看执行是否成功即可 { return $result; } } }
在上一级的类的加载:include("../test0506/DADB.php");
<title>投票</title> <style type="text/css"> #list{ width:250px; height:200px} #jieguo{ width:250px; height:200px} .x{ float:left} </style> </head> <body> <form action="chuli.php" method="post"> <?php include("../test0506/DBDA.class.php"); $db = new DBDA(); $sql = "select * from diaoyantimu limit 0,1"; $attr = $db->Query($sql); //var_dump($attr); $tmmc = $attr[0][1]; $tmdh = $attr[0][0]; echo "<div>题目名称:{$tmmc}</div>"; $sqlxx = "select * from DiaoYanXuanXiang where TiMuDaiHao = '{$tmdh}'"; $attrxx = $db->Query($sqlxx); //var_dump($attrxx); //echo "<div>题目代号:{$tmdh}</div>" echo "<div id='list'>"; foreach($attrxx as $v) { echo "<div> <input type='checkbox' value='{$v[0]}' name='name[]' /> <span>{$v[1]}</span> </div>"; } ?> <input type="submit" value="提交" /> <input type="button" value="查看结果" id="check" onclick="ShowJieGuo()" /> </form> </div> <div id="jieguo" style="display:none"> <?php $sqlsum = "select sum(Numbers) from DiaoYanXuanXiang where TiMuDaiHao = '{$tmdh}'"; $attrsum = $db->Query($sqlsum);//所有的人数 //var_dump($attrsum);//$attrsum[0][0] foreach($attrxx as $v) { //选项名称 $name = $v[1]; //投票人数 $shumu = $v[2]; $bfb = ($shumu/$attrsum[0][0])*100;//得票的百分比 $bfb = round($bfb,2); echo "<div style='width:250px; height:30px'> <span class='x'>{$name}</span> <div class='x' style='width:100px; height:8px; border:1px solid red'> <div style='width:{$bfb}%; height:8px; background-color:red'></div> </div> <span class='x'>{$shumu} </span> <span class='x'>{$bfb}%</span> </div>"; } ?> <input type="button" value="返回" id="fanhui" onclick="ShowList()"/> </div> </body> <script type="text/javascript"> function ShowJieGuo() { document.getElementById("list").style.display = "none"; document.getElementById("jieguo").style.display = "block"; } function ShowList() { document.getElementById("list").style.display = "block"; document.getElementById("jieguo").style.display = "none"; } </script> </html>
处理页面:chuli.php 依旧需要类的加载
<?php $attr = $_POST["name"]; include("../test0506/DBDA.class.php"); $db = new DBDA(); foreach($attr as $v) { $sql = "update DiaoYanXuanXiang set Numbers = Numbers+1 where Ids = '{$v}'"; $db->Query($sql,1); } header("location:toupiao.php");
需要注意的:checkbox是可以多选的
另一个方法是在自己练习的基础上的完善:没有加载类,是页面的跳转,不建议使用
主页面------DiaoYan.php
<title>调研练习</title> </head> <body> <form action="DYchuli.php" method="post"> <?php //造连接对象 $db = new MySQLi("localhost","root","","mydb"); //判断连接是否成功 !mysqli_connect_error() or die("连接失败"); //写SQL语句 $sql = "select * from diaoyantimu"; //执行SQL语句 $result = $db->query($sql); //从结果集中读取数据 $attr = $result->fetch_all(); //var_dump($attr); $tmmc = $attr[0][1]; $tmdh = $attr[0][0]; echo "<div>题目名称:{$tmmc}</div>"; $sqlxx = "select * from DiaoYanXuanXiang where TiMuDaiHao = '{$tmdh}'"; $resultxx = $db->query($sqlxx); $attrxx = $resultxx->fetch_all(); //var_dump($attrxx); foreach($attrxx as $v) { echo "<div><input type='checkbox' value='{$v[0]}' name='xx[]'>{$v[1]}</div>"; } ?> <br /> <input type="submit" value="提交" /> <a href="tijiao.php"><input type="button" value="查看" /></a> </form> </body> </html>
显示效果
处理页面------DYchuli.php
<?php $attr = $_POST["xx"];//选项ids代号 $db = new MySQLi("localhost","root","","mydb"); !mysqli_connect_error() or die("连接失败"); foreach($attr as $v) { $sql = "update DiaoYanXuanXiang set Numbers = Numbers+1 where Ids = '{$v}'"; $result = $db->query($sql); if($result) { header("location:tijiao.php"); } else { echo "投票失败"; } }
结果显示的页面-------tijiao.php
<title>无标题文档</title> <style type="text/css"> .x{ float:left} </style> </head> <body> <?php //造连接对象 $db = new MySQLi("localhost","root","","mydb"); !mysqli_connect_error() or die("连接失败"); $sql = "select * from diaoyantimu"; $result = $db->query($sql); $attr = $result->fetch_all(); //var_dump($attr); $tmmc = $attr[0][1]; $tmdh = $attr[0][0]; echo "<div>题目名称:{$tmmc}</div>"; $sqlxx = "select * from DiaoYanXuanXiang where TiMuDaiHao = '{$tmdh}'"; $resultxx = $db->query($sqlxx); $attrxx = $resultxx->fetch_all(); //var_dump($attrxx); $sqlsum = "select sum(Numbers) from DiaoYanXuanXiang where TiMuDaiHao = '{$tmdh}' "; $resultsum = $db->query($sqlsum); $attrsum = $resultsum->fetch_all(); //var_dump($attrsum); $sum = $attrsum[0][0];//总人数 foreach($attrxx as $v) { //选项名称 $name = $v[1]; //投票人数 $ren = $v[2]; if($sum==0) { echo $bfb=0; } else { $bfb = ($ren/$sum)*100; $bfb = round($bfb,2); } echo "<div style='width:350px; height:30px'> <span class='x'>{$name}</span> <div class='x' style='width:100px; height:8px; border:1px solid red'> <div style='width:{$bfb}%; height:8px; background-color:red'></div> </div> <span class='x'>{$ren} </span> <span class='x'>{$bfb}%</span> </div>"; } ?> <br /> <form action="DYchuli.php" method="post"> <a href="DiaoYan.php"><input type="button" value="返回" /></a> </form> </body> </html>
页面显示效果: