投票练习

<body><div style="margin-left:200px; margin-top:100px; " >
<?php
include "./tp.class.php";
$db = new TP();
$sql = "select * from tpz where Ids = 1";
$attr = $db->Query($sql);
?>
<h1><?php echo $attr[0][1];?></h1>
<div style=" display:block;" id="Q">
<form action="tj.php" method="post">
<?php
$sql1 = "select Idr,options from tp";
$attr1 = $db->Query($sql1);
foreach($attr1 as $v)
{

echo "<input type='checkbox' value={$v[0]} name='tp[]'/>".$v[1]."<br><br>";
}
?>
<br />
<br />
<div><input type="submit" value="提交" />&nbsp;&nbsp;&nbsp;<input type="button" value="查看结果" onclick="tj()" /></div>
</form>
</div>
</div>

<div style="margin-left:200px; margin-top:100px;">
<div style="display:none;" id="H">
<form action="main.php" method="post">
<?php
$sql1 = "select * from tp where Timudaihao = 1";
$attr1 = $db->Query($sql1);
$sql2 = "select sum(numbers) from tp";
$c = $db->Query($sql2);
foreach($attr1 as $v)
{
$b = $v[2]/$c[0][0]*100;
$b1 = number_format($b,2);
echo "<table>
<tr>
<td style='100px'>{$v[1]}</td>
<td><div style='border:0px solid;background-color:#EDEDED; 200px; height:10px;' >
<div style='background-color:#FF0000; {$b1}%; height:10px;'></div>
</div></td>
<td style='100px'>{$v[2]}&nbsp;({$b1}%)</td>
</tr>
</table>";
}
?>
<br />
<br />
<div><input type="button" value="返回" onclick="fh()" /></div>
</form>
</div>
</div>
<script>
function tj()
{
document.getElementById("Q").style.display = "none";
document.getElementById("H").style.display = "block";
}
function fh()
{
document.getElementById("Q").style.display = "block";
document.getElementById("H").style.display = "none";
}
</script>
</body>

处理:

<?php
include "tp.class.php";
$db = new TP();
$tp = $_POST["tp"];
if(!empty($tp))
{
foreach ($tp as $v)
{
$sql1 = "select numbers from tp where Idr= {$v[0]}";
$b1 = $db->Query($sql1);
$b2 = $b1[0][0]+1;
$sql = "update tp set numbers={$b2} where Idr = {$v[0]}";
$a = $db->Query($sql,1);
}
if($a)
{
header("location:main.php");
}
else
{
echo "失败!";
}
}
else
{
header("location:main.php");
}

类:

<?php
class TP
{
public $host = "localhost";
public $uid = "root";
public $pwd = "";

public function Query($sql,$type=0,$db="news")
{
$d = new MySQLi($this->host,$this->uid,$this->pwd,$db);
!mysqli_connect_error() or die("连接失败!");
$result = $d->query($sql);
if($type==0)
{
return $result->fetch_all();
}
else
{
return $result;
}
}
}

原文地址:https://www.cnblogs.com/1116zsc/p/5470543.html