数据访问 投票习题
<!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> </head> <body <h1>投票页面</h1> <form action="tp.php"> <?php require_once"../DBDA.class.php"; $db = new DBDA(); $sql = "select * from diaoyantimu"; $arr = $db->query($sql); foreach($arr as $v){ echo"<div>{$v[1]}</div>"; $sqlxx = "select * from diaoyanxuanxiang where timudiaoyan='{$v[0]}'"; $arrxx = $db->query($sqlxx); foreach($arrxx as $vxx){ echo "<div><input type='checkbox' name='ck[]' value='{$vxx[0]}'/>{$vxx[1]}</div>"; } } ?> <br> <input type="submit" value="提交"/> </form> </body> </html>
执行代码
<?php $arr = $_POST["ck"]; require_once"../DBDA.class.php"; $db = new DBDA(); $str = implode("','",$arr); $sql = "update diaoyanxuanxiang set number = number+1 where ids in('{$str}')"; $reslut = $db->query($sql,1); if($reslut){ header("location:jieguo.php"); }else{ echo "投票失败!"; }
显示执行代码
<!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"> .wai{ width:300px; height:30px; background:#090; } .nei{ height:30px; background-color:#F60; } </style> </head> <body> <h1>投票结果显示</h1> <?php require_once "../DBDA.class.php";//加载数据访问类 $db = new DBDA(); $sql = "select * from diaoyantimu"; $arr = $db -> query($sql); foreach($arr as $v){ echo "<h2>{$v[1]}:</h2>"; $sqlall = "select sum(number) from diaoyanxuanxiang where timudiaoyan= '{$v[0]}'"; $arrall = $db->query($sqlall);//获取二维数组。 $sqlxx = "select * from diaoyanxuanxiang where timudiaoyan = '{$v[0]}'"; $arrxx = $db->query($sqlxx); foreach($arrxx as $vxx){ $vxx[2];//投该选项的数量 $arrall[0][0];//获取二维数组中的唯一的一个值 $bfb = ($vxx[2]/$arrall[0][0])*100;//百分比 $bfb1 = ROUND($bfb,2); echo "<div><font size='5' color='#090'>{$vxx[1]}</font></div> <div class='wai'> <div class='nei' style='width:{$bfb1}%;'></div> <font color='#F60'>百分比:{$bfb1}%票数:{$vxx[2]}</font> </div><br>"; } } ?> </body> </html>