2019-12-04 php实现投票功能 转载

image.png

image.png

image.png

image.png

数据库设计
image.png

image.png

代码
index.php
<?php
include("../inc/common.inc.php");
include("../inc/function.php");
if(arr=arr as sql = "update vote_option set number = number+1 where id = '{r=sql);//1代表$sql的类型
}
header ("location:index.php");
}
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gbk" />
<title>投票</title>
<style>
.x
{

float:left;}

</style>
</head>

<body>

<form action="" method="post">
<?php
//从调研题目表中找出题目代号和名称
sql="select * from vote_title";arr=ms->qrow(sql);
tmmc=arr['title'];
tmdh=arr['id'];
echo "<div><h2>".tmmc.":</h2></div>"; //从调研选项表中输出选项内容:sqlxx="select * from vote_option where title='".tmmc."'";arrxx=ms->qrows(sqlxx);
echo "<div id='list'>";
foreach (arrxx asv)
{
echo "<div><input type='checkbox' value='{v[0]}' name='xx[]'>{v[1]}</div><br />";
}

?>
<input type="submit" value="提交">
<a href="result.php"><input type="button" value="查看结果" id="check" ></a>
</form>
</body>
</html>
result.php
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>查看结果</title>
<style>
.x
{
float:left;}
</style>
</head>
<body>

<?php
include("../inc/common.inc.php");
include("../inc/function.php");
//从调研题目表中找出题目代号和名称
sql="select top 1 * from vote_title";arr=ms->qrow(sql);
tmmc=arr['title'];
echo "<div><h2>{tmmc}:</h2></div>"; //从调研选项表中输出选项内容:sqlxx="select * from vote_option where title='{tmmc}'";arrxx=ms->qrows(sqlxx);

//计算总人数:
sqlzs="select sum(number) as sum from vote_option where title='{tmmc}'";
zrs=ms->qrow($sqlzs);

foreach (arrxx asv)
{
name=v['option'];//调研项目名称
number=v['number'];//选择该项的人数
//判断总人数是否为0
if(zrs['sum']==0) {bfb = 0;
}
else
{
bfb = (number/$zrs['sum'])*100;//求百分比
}

bfb=round(bfb,2); //取小数点后两位
echo "<div>
<span class='x'>{name} </span> <div class='x' style='width:200px; height:10px; background-color:#808080'> <div style='width:{bfb}%; height:10px; background-color:#FF8040'> </div>
</div>
<span class='x'>人数:{number} </span> <span class='x'>百分比{bfb}%</span><br />
</div><br />";
}
?>
<br />
<a href="index.php"><input type="button" value="返回"></a>
</body>
</html>

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。