1. 做一个类DBDA 把数据库内容封装进去
<?php class DBDA{ public $host="localhost"; //服务器地址 public $uid="root"; //用户名 public $pwd="5270437"; //密码 public $dbname="ceshi"; //数据库名称 /* 执行一条SQL语句的方法 @param sql 要执行的SQL语句 @param type SQL语句的类型,0代表查询 1代表增删改 @return 如果是查询语句返回二维数组,如果是增删改返回true或false */ public function query($sql,$type=0){ $db = new MySQLi($this->host,$this->uid,$this->pwd,$this->dbname); $result = $db->query($sql); if($type){ return $result; }else{ return $result->fetch_all(); } } }
2.做一个数据访问页面
<!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="chakantoupiaojieguo.php" method="post"> <?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 timudaihao=‘{$v[0]}‘"; $arrxx = $db->query($sqlxx); foreach($arrxx as $vxx){ echo "<div><input type=‘checkbox‘ name = ‘ck[]‘ value=‘{$vxx[0]}‘/>{$vxx[1]}</div>"; } } ?><input type="submit" value="投票" /> </form> </body> </html>
3.做一个处理投票的页面
<?php $arr = $_POST["ck"]; //var_dump($arr); require_once "./DBDA.class.php"; $db = new DBDA(); $str = implode("‘,‘",$arr); $sql = "update diaoyanxuanxiang set numbers = numbers+1 where ids in(‘{$str}‘) "; $reslut = $db->query($sql,1); if($reslut){ header("location:toupiaochakan.php"); }else{ echo "投票失败!"; } ?>
4.投票的查看页面跳出百分比
<!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:200px; height:10px; border:1px solid #F00} .nei{height:10px; float:left; background-color:#F00} </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 "<div>{$v[1]}</div>"; $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; echo "<div>{$vxx[1]}<div class=‘wai‘><div class=‘nei‘ style=‘width:{$bfb}%‘></div></div>{$bfb}%{$vxx[2]}</div>"; } } ?> </body> </html>
原文地址:https://www.cnblogs.com/palpitate/p/8316116.html
时间: 2024-10-14 15:59:51