数据访问类---投票
一、制作投票页面
<body> <form action="toupiaochuli.php" method="post"> <?php require_once "DBDB.class.php";//加载数据访问类 $db = new DBDB(); $sql = "select * from diaoyantimu"; $arr = $db -> query($sql); foreach($arr as $v){ echo "<h2>{$v[1]}:</h2>"; $sqlxx = "select * from diaoyanxuanxiang where timudaihao = '{$v[0]}'"; $arrxx = $db->query($sqlxx); foreach($arrxx as $vxx){ echo "<input type='checkbox' name='ck[]' value='{$vxx[0]}'/>{$vxx[1]}<br>"; //ck[] 一个空数组 } } ?> <br /> <input type="submit" value="投票"> </form> </body>
二、做执行代码
<?php $arr = $_POST["ck"]; require_once "DBDB.class.php"; $db = new DBDB(); $str = implode("','",$arr);//拼接字符串 $sql = "update diaoyanxuanxiang set numbers = numbers+1 where ids in('$str')"; //ck[]数组中所有的索引组合成一个数组用','拼接 $result = $db->query($sql,1); if($result){ header("location:jieguo.php"); }else{ echo"投票失败"; }
三、显示投票结果
<style type="text/css"> .wai{ width:300px; height:30px; background:#090; } .nei{ height:30px; background-color:#F60; } </style> <body> <h1>投票结果显示</h1> <?php require_once "DBDB.class.php";//加载数据访问类 $db = new DBDB(); $sql = "select * from diaoyantimu"; $arr = $db -> query($sql); foreach($arr as $v){ echo "<h2>{$v[1]}:</h2>"; $sqlall = "select sum(numbers) from diaoyanxuanxiang where timudaihao = '{$v[0]}'"; $arrall = $db->query($sqlall);//获取二维数组。 $sqlxx = "select * from diaoyanxuanxiang where timudaihao = '{$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>