PHP 练习(投票)

1.建立数据库

 

表1:DiaoYanTiMu

 

表2:DiaoYanXuanXiang

 

2.页面

 

页面1:投票首页

复制代码
<!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"> #fanhui{ display:none} </style> </head> <body> <form action="tpchuli.php" method="post">
复制代码
<?php

$db = new  MySQLi("localhost","root","123","mydb");

$sql = "select * from diaoyantimu"; $result = $db->query($sql); $attr = $result->fetch_all(); foreach($attr as $v) { echo "<div>{$v[1]}</div>"; $sxx = "select * from diaoyanxuanxiang where timudaihao='{$v[0]}'"; $rxx = $db->query($sxx); $axx = $rxx->fetch_all(); $bs = 0; if(!empty($_GET["a"])) { $bs = $_GET["a"]; } $s = $bs==1?"none":"block"; echo "<div id='toupiao' style='display:{$s}'>"; foreach($axx as $v1) { echo "<div><input type='checkbox' name='tp[]' value='{$v1[0]}' />{$v1[1]}</div>"; } echo "</div>"; $s1 = $bs==0?"none":"block"; //显示投票结果 echo "<div id='jieguo' style='display:{$s1}'>"; foreach($axx as $v2) { $v2[2]; //当前选项的人数 //求总人数 $szrs = "select sum(numbers) from diaoyanxuanxiang where timudaihao='{$v[0]}'"; $rzrs = $db->query($szrs); $azrs = $rzrs->fetch_row(); $azrs[0]; //总人数 $bfb = ($v2[2]/$azrs[0])*100; echo "<div>{$v2[1]}<div style='width:100px; height:10px; border:1px solid black;'><div style='width:{$bfb}%; height:10px; </div></div>{$v2[2]} {$bfb}%</div>"; } echo "</div>"; } ?>
复制代码

 

<input type="submit" value="投票" /> <input type="button" id="jg" value="查看结果" onclick="showjg()" /> <input type="button" id="fanhui" value="返回" onclick="showtp()" /> </form> </body> <script type="text/javascript"> function showjg() { document.getElementById("jieguo").style.display="block"; document.getElementById("toupiao").style.display="none"; document.getElementById("jg").style.display="none"; document.getElementById("fanhui").style.display="block"; } function showtp() { document.getElementById("jieguo").style.display="none"; document.getElementById("toupiao").style.display="block"; document.getElementById("jg").style.display="block"; document.getElementById("fanhui").style.display="none"; } </script> </html>
复制代码

 

页面2:处理页面

复制代码
<?php
$attr = $_POST["tp"];

$str = implode("','",$attr); $sql = "update diaoyanxuanxiang set numbers = numbers+1 where ids in('{$str}')"; $db = new MySQLi("localhost","root","123","mydb"); $db->query($sql); header("location:toupiao.php?a=1");
复制代码

转载于:https://www.cnblogs.com/l5580/p/6065618.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值