標籤:需要 image content cti dia nbsp clu check utf-8
要求大概是這樣的,有一個題目,題目下面是複選框,要求點完複選框提交後會變成進度條,各選項的進度條百分比,和投票數量
首先還是要在資料庫建兩張表,如下:
要完成這個題目,需要建兩個頁面
<!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><form action="toupiaojg.php" method="post"> <?phpinclude("./fengzhuang.php"); //引入資料庫,目前的目錄下$db = new fengzhuang(); //造對象$sql = "select * from diaoyantimu"; //sql語句 查詢$attr = $db->Query($sql); //返回二維數組echo "{$attr[0][1]}<br>"; //輸出題目名稱$code = $attr[0][0]; $sxx = "select * from diaoyanxuanxiang where timudaihao=‘{$code}‘"; //查詢所有選項$axx = $db->Query($sxx);foreach($axx as $v) { echo "<input type=‘checkbox‘ value=‘{$v[0]}‘ name=‘xx[]‘ />$v[1]<br>"; //輸出所有選項內容,在它前面加複選框,以數組方式提交}?><input type="submit" value="投票" /></form></body></html>
<!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><?phpinclude("./fengzhuang.php");$db = new fengzhuang();$attr = $_POST["xx"]; //取數組foreach($attr as $v) //用迴圈{ $sql = "update diaoyanxuanxiang set numbers=numbers+1 where ids=‘{$v}‘"; //寫sql語句,每次修改一個 $db->Query($sql,0); //調用執行}//顯示結果$sql = "select * from diaoyanxuanxiang"; $shuzu = $db->Query($sql);$szs = "select sum(numbers) from diaoyanxuanxiang";$azs = $db->Query($szs);$zs = $azs[0][0];foreach($shuzu as $v) //遍曆顯示{ $bfb = ($v[2]/$zs)*100; //選項人數除總數 ,再乘100 echo "{$v[1]}:{$v[2]}({$bfb}%)<div style=‘width:200px;height:10px; border:1px solid red;‘><div style=‘ width:{$bfb}%; height:10px‘></div></div>";} //輸出標題,輸出進度條?></body></html>
代碼如上,也有注釋,運行起來的結果就是這樣:
選中某項或某幾項點擊投票後,就會出現進度條
進度條的變化隨著你的選項而變化
運用php做投票題,例題