php做投票題目
阿新 • • 發佈:2018-01-19
left 結果 百分比 eight blog cal org lec ram
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>
php做投票題目